001    /*
002     * CRIMSON
003     * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 
004     * University of Pennsylvania.
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License as
008     * published by the Free Software Foundation; either version 2 of the
009     * License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful, but
012     * WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019     * 02110-1301 USA.
020     *
021     * @(#)Model.java
022     */
023    
024    package edu.upenn.crimson;
025    
026    import edu.upenn.crimson.io.*;
027    
028    /**
029     * Contains model info.
030     *
031     * @author  Stephen Fisher
032     * @version $Id: Model.java,v 1.9 2007/05/16 18:55:58 fisher Exp $
033     */
034    
035    public class Model implements Cloneable { 
036    
037             /** 
038              * This is a unique name for the Model object.  It must conform to
039              * the Oracle table name definition: no more than 28 char long
040              * (plus "_T" or "_P"), no spaces or hyphens, and must begin with
041              * a character.
042              */
043             private String id = "";
044    
045             /** Notes specific to this Model object. */
046             private String notes = "";
047    
048             /** Brief description of this Model. */
049             private String details = "";
050    
051             /** Create a new Model object. */
052             public Model(String id) { this(id, true); }
053    
054             /** 
055              * Create a new Model object. If 'withUpdate' is true, then the
056              * new model will be saved to the database.
057              * @XXX Shouldn't allow new models if the database isn't open
058              * because the models can't be saved. Should throw an exception in
059              * this case.
060              */
061             public Model(String id, boolean withSave) {
062                      // if no ID, then create a random ID for this Model
063                      if (CrimsonUtils.isEmpty(id)) {
064                                    id = ObjectHandles.randomModelID("M_");
065                      } else {
066                                    if (ObjectHandles.containsModel(id)) id = ObjectHandles.randomModelID(id + "_");
067                      }
068    
069                      this.id = id.trim().toUpperCase();
070    
071                      // add the model to the relevant lists
072                      ObjectHandles.addModel(this);
073    
074                      if (withSave) Models.save(this);
075             }
076    
077        //--------------------------------------------------------------------------
078        // Setters and Getters
079    
080        /** Get the ID. */
081        public String getID() { return id; }
082    
083             /** 
084              * Sets the value of the notes field.  This will also update the
085              * value in the MODELS table.
086              */
087             public void setNotes(String notes) { setNotes(notes, true); }
088    
089             /** 
090              * Sets the value of the notes field.  This will also update the
091              * value in the MODELS table, if 'withSave' is true.
092              */
093             public void setNotes(String notes, boolean withSave) { 
094                      this.notes = notes; 
095                      if (withSave) Models.save(this);
096             }
097    
098             public String getNotes() { return notes; }
099    
100             /** 
101              * Sets the value of the details field.  This will also update the
102              * value in the MODELS table.
103              */
104             public void setDetails(String details) { setDetails(details, true); }
105    
106             /** 
107              * Sets the value of the details field.  This will also update the
108              * value in the MODELS table, if 'withSave' is true.
109              */
110             public void setDetails(String details, boolean withSave) { 
111                      this.details = details; 
112                      if (withSave) Models.save(this);
113             }
114    
115             public String getDetails() { return details; }
116    
117        //--------------------------------------------------------------------------
118        // Miscellaneous Methods
119       
120             /**
121              * Create a shallow clone (just clone the structure, not the
122              * Objects) of the existing object. This will randomly assign the
123              * clone's ID, based on the ID for the parent object.
124              */
125             public Object clone() {
126                      return clone(ObjectHandles.randomModelID(this.id + "_"));
127             }
128    
129             /**
130              * Create a shallow clone (just clone the structure, not the
131              * Objects) of the existing object.  Do not save the clone until
132              * all fields have been set.
133              */
134             public Object clone(String id) {
135                      Model modelItem = new Model(id, false);
136                      modelItem.setNotes(this.notes, false);
137                      modelItem.setDetails(this.details, true);
138                      return modelItem;
139             }
140    
141             /** Returns Model information for debugging purposes. */
142             public String toStringFull() {
143                      String out = "";
144                      out += "ID: " + id + "\n";
145                      out += "Notes: " + notes + "\n";
146                      out += "Details: " + details + "\n";
147                      return out;
148             }
149    
150             /** Returns Model information formatted for JLists. */
151             public String toString() { 
152                      String out = id;
153                      if (! CrimsonUtils.isEmpty(notes)) {
154                                    int len = (notes.length() < 20) ? notes.length() : 19;
155                                    out += ": " + notes.substring(0,len) + "...";
156                      }
157                      return out;
158             }
159    
160    } // Model.java
161