001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.objectmanager.data.db;
031
032import org.kepler.objectmanager.data.DataObjectDescription;
033import org.kepler.objectmanager.data.Domain;
034
035/**
036 * This object represents an Attribute. A Attribute stores information about a
037 * scalar value that is used in a Step in the pipeline.
038 */
039public class Attribute extends DataObjectDescription implements
040                DSTableFieldIFace {
041        /** unit type for standard stmml units **/
042        public static String STANDARDUNIT = "STANDARDUNIT";
043        /** unit type for custom units **/
044        public static String CUSTOMUNIT = "CUSTOMUNIT";
045
046        private String label;
047        private String unit;
048        private String unitType;
049        private String measurementScale;
050        // FIXME: unused
051    // private String precision;
052        private Domain domain;
053
054        /**
055         * Construct a Attribute.
056         */
057        public Attribute(String id, String name, String type) {
058                this(id, name, type, null, null, null);
059        }
060
061        /**
062         * Construct a Attribute.
063         */
064        public Attribute(String id, String name, String type, String description) {
065                this(id, name, type, description, null, null);
066        }
067
068        /**
069         * Construct a Attribute.
070         */
071        public Attribute(String id, String name, String type, String description,
072                        String unit, Domain dom) {
073                this(id, name, null, description, unit, null, type, null, dom);
074        }
075
076        /**
077         * Constructor for extra local params
078         * 
079         * @param name
080         *            the name of the attribute
081         * @param label
082         *            the label of the attribute
083         * @param definition
084         *            the definition of the attribute
085         * @param unit
086         *            the unit of the attribute
087         * @param unitType
088         *            the type of the attribute. defined by STANDARDUNIT or
089         *            CUSTOMUNIT
090         * @param storageType
091         *            the data type of the attribute
092         * @param measurementScale
093         *            the scale of the attribute
094         * @param precision
095         *            the number of precise numbers to the right of the decimal
096         */
097        public Attribute(String id, String name, String label, String definition,
098                        String unit, String unitType, String dataType,
099                        String measurementScale, Domain dom) {
100                super(id, name, dataType, definition);
101                if (label == null) {
102                        this.label = "";
103                } else {
104                        this.label = label;
105                }
106                if (unit == null) {
107                        this.unit = "";
108                } else {
109                        this.unit = unit;
110                }
111                if (unitType == null) {
112                        this.unitType = "";
113                } else {
114                        this.unitType = unitType;
115                }
116                if (measurementScale == null) {
117                        this.measurementScale = "";
118                } else {
119                        this.measurementScale = measurementScale;
120                }
121                this.name = getLegalDbFieldName(name);
122                domain = dom;
123        }
124
125        /**
126         * Return the unit for this Attribute
127         */
128        public String getUnit() {
129                return this.unit;
130        }
131
132        /**
133         * accessor method
134         */
135        public String getLabel() {
136                return label;
137        }
138
139        /**
140         * accessor method
141         */
142        public String getUnitType() {
143                return unitType;
144        }
145
146        /**
147         * accessor method
148         */
149        public String getMeasurementScale() {
150                return measurementScale;
151        }
152
153        /**
154         * public void setParent(Entity p) { parentTable = p; }
155         * 
156         * public Entity getParent() { return parentTable; }
157         */
158
159        /**
160         * Returns the domain.
161         * 
162         * @return Domain
163         */
164        public Domain getDomain() {
165                return domain;
166        }
167
168        /*
169         * public String toString() { StringBuffer sb = new StringBuffer();
170         * sb.append("NAME: ").append(name).append("\n");
171         * sb.append("id: ").append(id).append("\n");
172         * sb.append("dataType: ").append(dataType).append("\n");
173         * sb.append("description: ").append(description).append("\n");
174         * sb.append("label: ").append(label).append("\n");
175         * sb.append("definition: ").append(definition).append("\n");
176         * sb.append("unit: ").append(unit).append("\n");
177         * sb.append("unitType: ").append(unitType).append("\n");
178         * sb.append("measurementScale: ").append(measurementScale).append("\n");
179         * sb.append("precision: ").append(precision).append("\n"); return
180         * sb.toString(); }
181         */
182
183        /**
184         * Serialize the data item in XML format.
185         */
186        public String toXml() {
187                StringBuffer x = new StringBuffer();
188                x.append("<attribute id=\"");
189                x.append(getId());
190                x.append("\">\n");
191                appendElement(x, "attributeName", getName());
192                appendElement(x, "dataType", getDataType());
193                appendElement(x, "attributeDescription", getDefinition());
194                appendElement(x, "unit", getUnit());
195                x.append("</attribute>\n");
196
197                return x.toString();
198        }
199
200        /**
201         * Given an attribute name, return a legal database field name. This is the
202         * generic implementation, but child classes may need to override this with
203         * their own database-specific implementation.
204         * 
205         * @param attributeName
206         *            the attribute name
207         * @return legalName, a String containing a legal field name for this
208         *         attribute name
209         */
210        private String getLegalDbFieldName(String attributeName) {
211                return asLegalDbFieldName(attributeName);
212        }
213        
214        /**
215         * Given an attribute name, return a legal database field name. This is the
216         * generic implementation
217         * 
218         * @param attributeName
219         *            the attribute name
220         * @return legalName, a String containing a legal field name for this
221         *         attribute name
222         */
223        public static String asLegalDbFieldName(String attributeName) {
224                if (attributeName == null) {
225                        return null;
226                }
227                String legalName = attributeName;
228
229                char[] badChars = { ' ', '-', '.', '/' };
230                char goodChar = '_';
231
232                for (int i = 0; i < badChars.length; i++) {
233                        legalName = legalName.replace(badChars[i], goodChar);
234                }
235
236                return legalName;
237        }
238}