001    /*-----------------------------------------------------------------------
002     *  Copyright (C) 2006
003     *  Associated Universities, Inc. Washington DC, USA.
004     *  This program is free software; you can redistribute it and/or
005     *  modify it under the terms of the GNU General Public License as
006     *  published by the Free Software Foundation; either version 2 of
007     *  the License, or (at your option) any later version.
008     *
009     *  This program is distributed in the hope that it will be useful,
010     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *  GNU General Public License for more details.
013     *
014     *  Correspondence concerning this software should be addressed as follows:
015     *         Internet email: switz@nrao.edu
016     *         Postal address: SSS Software
017     *                         National Radio Astronomy Observatory
018     *                         Post Office Box 0
019     *                         Socorro, NM 87801  USA
020     *-----------------------------------------------------------------------*/
021    package edu.nrao.sss.model.parameter;
022    
023    import java.io.Serializable;
024    import java.math.BigDecimal;
025    import java.util.Date;
026    
027    import edu.nrao.sss.validation.ValidationPurpose;
028    import edu.nrao.sss.xml.XMLTransform;
029    
030    /**
031     * A Parameter consists of an Entity, an optional Band, a Name, the Value of the
032     * parameter, the name of the person who last updated it, the version of the
033     * parameter (a date), and the date it is to take effect.
034     * 
035     * <p>
036     * <b>Version Info:</b>
037     * <table style="margin-left:2em">
038     * <tr>
039     * <td>$Revision$</td>
040     * <tr>
041     * <td>$Date$</td>
042     * <tr>
043     * <td>$Author$</td>
044     * </table>
045     * </p>
046     * 
047     * @author Stephan Witz
048     * @since 2007-10-22
049     */
050    public class Parameter implements Serializable {
051    
052            private static final long serialVersionUID = 1L;
053            public final static String ENTITY_FIELD = "entity";
054            public final static String BAND_FIELD = "band";
055            public final static String PARAMETERNAME_FIELD = "parameterName";
056            public final static String VALUE_FIELD = "value";
057            public final static String MODIFIEDBY_FIELD = "modifiedBy";
058            public final static String VERSION_FIELD = "version";
059            public final static String EFFECTIVEDATE_FIELD = "effectiveDate";
060    
061            private String band;
062            private Date effectiveDate;
063            private String entity;
064            private int hashCode;
065            private int id;
066    
067            private String modifiedBy;
068            private String parameterName;
069            private boolean update;
070            private String value;
071            private Date version;
072    
073            public Parameter() {
074                    this.id = this.hashCode = 0;
075                    update = false;
076            }
077    
078            public Parameter(Parameter parameter) {
079                    clone(parameter);
080            }
081    
082            public Parameter(String modifiedBy, String parameterName, String entity,
083                            String band, String value) {
084                    this.id = this.hashCode = 0;
085                    update = false;
086                    setModifiedBy(modifiedBy);
087                    setParameterName(parameterName);
088                    setEntity(entity);
089                    setBand(band);
090                    setValue(value);
091                    setEffectiveDate(new Date());
092                    setVersion(new Date());
093            }
094    
095            // Need add check for different entity
096            public void addValue(Parameter parameter) throws IllegalArgumentException,
097                            IllegalStateException {
098                    ParameterRule thisRule = getParameterRule();
099                    if (!new ParameterValidator().validate(this,
100                                    ValidationPurpose.CERTIFY_READY_TO_USE).isEmpty())
101                            throw new IllegalStateException();
102                    if (!new ParameterValidator().validate(parameter,
103                                    ValidationPurpose.CERTIFY_READY_TO_USE).isEmpty())
104                            throw new IllegalArgumentException();
105                    if (!getParameterRule().equals(parameter.getParameterRule())
106                                    || (getBand() != null && !getBand().equals(parameter.getBand())))
107                            throw new IllegalArgumentException();
108    
109                    switch (thisRule.getValueType()) {
110                    case DOUBLE:
111                            if (getValue() != null && parameter.getValue() != null) {
112                                    setValue(""
113                                                    + getValueAsBigDecimal().add(
114                                                                    parameter.getValueAsBigDecimal()));
115                            }
116                            break;
117                    case DECIMAL_INT:
118                            if (getValue() != null && parameter.getValue() != null)
119                                    setValue(""
120                                                    + (getValueAsDecimalInt() + parameter
121                                                                    .getValueAsDecimalInt()));
122                            break;
123                    case OCTAL_INT:
124                    case STRING:
125                            throw new IllegalArgumentException();
126                    }
127            }
128    
129            public void clone(Parameter parameter) {
130                    this.setValue(parameter.getValue());
131                    this.setBand(parameter.getBand());
132                    this.setEntity(parameter.getEntity());
133                    this.setParameterName(parameter.getParameterName());
134                    this.setVersion(parameter.getVersion());
135                    this.setEffectiveDate(parameter.getEffectiveDate());
136                    this.setModifiedBy(parameter.getModifiedBy());
137            }
138    
139            public boolean equals(Object obj) {
140                    if (null == obj)
141                            return false;
142                    if (!(obj instanceof Parameter))
143                            return false;
144                    else {
145                            Parameter mObj = (Parameter) obj;
146                            return (this.getId() == mObj.getId());
147                    }
148            }
149    
150            public String getBand() {
151                    return band;
152            }
153    
154            public Date getEffectiveDate() {
155                    return effectiveDate;
156            }
157    
158            public String getEntity() {
159                    return entity;
160            }
161    
162            public EntityType getEntityType() {
163                    EntityType result = null;
164                    for (EntityType e : EntityType.values())
165                            if (entity != null && e.getPattern().matcher(getEntity()).matches())
166                                    result = e;
167                    return result;
168            }
169    
170            public int getId() {
171                    return id;
172            }
173    
174            public String getModifiedBy() {
175                    return modifiedBy;
176            }
177    
178            public String getParameterName() {
179                    return parameterName;
180            }
181    
182            public ParameterRule getParameterRule() {
183                    ParameterRule result = null;
184                    EntityType eType = getEntityType();
185                    if (eType != null && parameterName != null)
186                            result = ParameterRules.getInstance().getParameterRule(
187                                            parameterName, eType);
188                    return result;
189            }
190    
191            public String getValue() {
192                    return value;
193            }
194    
195            public BigDecimal getValueAsBigDecimal() {
196                    return new BigDecimal(getValue());
197            }
198    
199            public int getValueAsDecimalInt() throws NumberFormatException {
200                    return Integer.parseInt(getValue(), 10);
201            }
202    
203            public double getValueAsDouble() throws NumberFormatException {
204                    return Double.parseDouble(getValue());
205            }
206    
207            public int getValueAsOctalInt() throws NumberFormatException {
208                    return Integer.parseInt(getValue(), 8);
209            }
210    
211            public Date getVersion() {
212                    return version;
213            }
214    
215            public int hashCode() {
216                    if (this.hashCode == Integer.MIN_VALUE) {
217                            return (int) this.getId();
218                    }
219                    return this.hashCode;
220            }
221    
222            public boolean isNew() {
223                    return (this.hashCode == Integer.MIN_VALUE) ? false : true;
224            }
225    
226            public boolean isUpdate() {
227                    return update;
228            }
229    
230            public void setBand(String band) {
231                    if (band == null)
232                            this.band = null;
233                    else {
234                            this.band = band.trim();
235                            this.band = this.band.replaceFirst("MHZ", "MHz");
236                            this.band = this.band.replaceFirst("GHZ", "GHz");
237                    }
238            }
239    
240            public void setEffectiveDate(Date effectiveDate) {
241                    this.effectiveDate = effectiveDate;
242            }
243    
244            public void setEntity(String entity) {
245                    this.entity = (entity == null) ? null : entity.trim().toUpperCase();
246            }
247    
248            public void setId(int id) {
249                    if (id < 0) {
250                            this.id = 0;
251                            this.hashCode = 0;
252                    } else {
253                            this.id = id;
254                            this.hashCode = Integer.MIN_VALUE;
255                    }
256            }
257    
258            public void setModifiedBy(String modifiedBy) {
259                    this.modifiedBy = (modifiedBy == null) ? null : modifiedBy.trim()
260                                    .toUpperCase();
261            }
262    
263            public void setParameterName(String parameterName) {
264                    this.parameterName = (parameterName == null) ? null : parameterName
265                                    .trim().toUpperCase();
266            }
267    
268            public void setUpdate(boolean update) {
269                    this.update = update;
270            }
271    
272            public void setValue(String value) {
273                    update = false;
274                    if (value == null) {
275                            this.value = null;
276                    } else {
277                            if (value.startsWith("$")) {
278                                    this.value = value.substring(1).trim();
279                                    update = true;
280                            } else {
281                                    this.value = value.trim();
282                            }
283                    }
284            }
285    
286            public void setVersion(Date version) {
287                    this.version = version;
288            }
289    
290            public String toString() {
291                    StringBuilder result = new StringBuilder();
292                    result.append("\nid:             [" + id + "]");
293                    result.append("\nentity:         [" + entity + "]");
294                    result.append("\nname:           [" + parameterName + "]");
295                    result.append("\nband:           [" + band + "]");
296                    result.append("\nvalue:          [" + value + "]");
297                    result.append("\nmodified by:    [" + modifiedBy + "]");
298                    result.append("\nversion:        [" + version + "]");
299                    result.append("\neffective date: [" + effectiveDate + "]");
300                    result.append("\nupdate:         [" + update + "]");
301                    return result.toString();
302            }
303    
304            public String toXML() {
305                    StringBuilder result = new StringBuilder();
306                    result.append("<parameter id=\""+id+"\">\n");
307                    result.append("<entity>" + entity + "</entity>\n");
308                    if (band != null)
309                            result.append("<band>" + band + "</band>\n");
310                    result.append("<name>" + parameterName + "</name>\n");
311                    result.append("<value>" + value + "</value>\n");
312                    result.append("<modifiedBy>" + modifiedBy + "</modifiedBy>\n");
313                    result
314                                    .append("<version>" + XMLTransform.dateToXMLDateTime(version)
315                                                    + "</version>\n");
316                    result.append("<effectiveDate>" + XMLTransform.dateToXMLDateTime(effectiveDate)
317                                    + "</effectiveDate>\n");
318                    result.append("</parameter>\n");
319                    return result.toString();
320            }
321    }