001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2004 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.hatmaker.crm.types;
016:
017: import java.io.Serializable;
018: import com.metaboss.enterprise.datatypes.DataType;
019: import com.metaboss.enterprise.datatypes.DataTypeTranslationMetadata;
020: import com.metaboss.enterprise.datatypes.DataTypeValidationException;
021: import com.metaboss.enterprise.datatypes.DefaultDataTypeTranslationMetadataImplementation;
022: import com.metaboss.enterprise.datatypes.DataTypeInstanceCreationFailureException;
023: import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForEmptyInstanceException;
024: import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForConcealedInstanceException;
025: import com.metaboss.enterprise.datatypes.sqltranslation.SqlVarcharTranslator;
026: import com.metaboss.enterprise.datatypes.primitivetranslation.PrimitiveStringTranslator;
027: import com.metaboss.enterprise.datatypes.xmltranslation.XmlTextTranslator;
028:
029: /** The phone number including area code datatype.
030: * This class represents an example of handcoded data type class.
031: * The DataTypes can be generated automatically from the template or
032: * just be hand coded and compiled together with the generated code. The requirements to the hand coded
033: * datatype classes are the same as for the templated ones. It is a good idea to start coding your
034: * data type class not from the scratch but from one of the generated classes */
035: public final class PhoneNo implements DataType, Serializable {
036: // Static storage for translation metadata. Create once and return many times
037: private static DataTypeTranslationMetadata sTranslationMetadata;
038: // Special value to carry empty value around
039: private static final String EMPTY_VALUE = "Empty";
040: // Special value to carry concealed value around
041: private static final String CONCEALED_VALUE = "Concealed";
042: /** Special value to show empty value in a toString() / createFromString() methods */
043: public static final String EMPTY_STRING = "";
044: /** Special value to show concealed value in a toString() / createFromString() methods */
045: public static final String CONCEALED_STRING = "(xxx)xxxxxxxx";
046: // Actual value stored here
047: private String mValue = null;
048:
049: static {
050: SqlVarcharTranslator lSqlTranslator = new SqlVarcharTranslator() {
051: public String getSqlValue(DataType pDataTypeInstance)
052: throws ClassCastException,
053: DataTypeInvalidOperationForEmptyInstanceException,
054: DataTypeInvalidOperationForConcealedInstanceException {
055: PhoneNo lInstance = (PhoneNo) pDataTypeInstance;
056: if (lInstance.isConcealed())
057: throw new DataTypeInvalidOperationForConcealedInstanceException();
058: if (lInstance.isEmpty())
059: throw new DataTypeInvalidOperationForEmptyInstanceException();
060: return lInstance.mValue;
061: }
062:
063: public DataType createFromSqlValue(String pSqlValue)
064: throws DataTypeInstanceCreationFailureException {
065: // Create instance
066: PhoneNo lValue = new PhoneNo();
067: lValue.mValue = pSqlValue;
068: return lValue;
069: }
070: };
071: PrimitiveStringTranslator lPrimitiveTranslator = new PrimitiveStringTranslator() {
072: public String getPrimitiveValue(DataType pDataTypeInstance)
073: throws ClassCastException,
074: DataTypeInvalidOperationForEmptyInstanceException,
075: DataTypeInvalidOperationForConcealedInstanceException {
076: PhoneNo lInstance = (PhoneNo) pDataTypeInstance;
077: if (lInstance.isConcealed())
078: throw new DataTypeInvalidOperationForConcealedInstanceException();
079: if (lInstance.isEmpty())
080: throw new DataTypeInvalidOperationForEmptyInstanceException();
081: return lInstance.mValue;
082: }
083:
084: public DataType createFromPrimitiveValue(
085: String pPrimitiveValue)
086: throws DataTypeInstanceCreationFailureException {
087: // Create instance
088: PhoneNo lValue = new PhoneNo();
089: lValue.mValue = pPrimitiveValue;
090: return lValue;
091: }
092: };
093: XmlTextTranslator lXmlTranslator = new XmlTextTranslator() {
094: public String getXmlValue(DataType pDataTypeInstance)
095: throws ClassCastException,
096: DataTypeInvalidOperationForEmptyInstanceException,
097: DataTypeInvalidOperationForConcealedInstanceException {
098: PhoneNo lInstance = (PhoneNo) pDataTypeInstance;
099: if (lInstance.isConcealed())
100: throw new DataTypeInvalidOperationForConcealedInstanceException();
101: if (lInstance.isEmpty())
102: throw new DataTypeInvalidOperationForEmptyInstanceException();
103: return lInstance.mValue;
104: }
105:
106: public DataType createFromXmlValue(String pXmlValue)
107: throws DataTypeInstanceCreationFailureException {
108: if (pXmlValue == null)
109: throw new DataTypeInstanceCreationFailureException(
110: "Supplied Xml value is null.");
111: // Create instance
112: PhoneNo lValue = new PhoneNo();
113: lValue.mValue = pXmlValue;
114: return lValue;
115: }
116: };
117: java.util.Properties lProperties = new java.util.Properties();
118: lProperties.setProperty("minsize", "8"); // number (8 digits)
119: lProperties.setProperty("maxsize", "10"); // Area code (2 digits) plus number (8 digits)
120: sTranslationMetadata = new DefaultDataTypeTranslationMetadataImplementation(
121: DataTypeTranslationMetadata.SQL_VARCHAR, lProperties,
122: lSqlTranslator,
123: DataTypeTranslationMetadata.PRIMITIVE_STRING, null,
124: lPrimitiveTranslator,
125: DataTypeTranslationMetadata.XML_TEXT, null,
126: lXmlTranslator);
127: }
128:
129: /** Returns this datatype's translation metadata.
130: * See {@link com.metaboss.enterprise.datatypes.DataType DataType} for more details */
131: public static DataTypeTranslationMetadata getTranslationMetadata() {
132: return sTranslationMetadata;
133: }
134:
135: /** Creates an instance of the datatype carrying an empty value */
136: public static PhoneNo createEmpty() {
137: PhoneNo lValue = new PhoneNo();
138: lValue.mValue = EMPTY_VALUE;
139: return lValue;
140: }
141:
142: /** Creates an instance of the datatype carrying concealed value */
143: public static PhoneNo createConcealed() {
144: PhoneNo lValue = new PhoneNo();
145: lValue.mValue = CONCEALED_VALUE;
146: return lValue;
147: }
148:
149: /** Creates an instance of the datatype from given readable value */
150: public static PhoneNo createFromString(String pSourceString)
151: throws DataTypeValidationException {
152: // Perform validation
153: validateString(pSourceString);
154: // Create instance
155: if (pSourceString.equals(EMPTY_STRING))
156: return createEmpty();
157: if (pSourceString.equals(CONCEALED_STRING))
158: return createConcealed();
159: // Create instance
160: PhoneNo lValue = new PhoneNo();
161: lValue.mValue = pSourceString;
162: return lValue;
163: }
164:
165: /** Validates proposed readable value */
166: public static void validateString(String pProposedString)
167: throws DataTypeValidationException {
168: if (pProposedString == null)
169: throw new DataTypeValidationException(
170: "Null initialisation string is not allowed for PhoneNo. Use createEmpty() method to create an empty PhoneNo");
171: int lLength = pProposedString.length();
172: if (lLength != 8 && lLength != 10)
173: throw new DataTypeValidationException(
174: "PhoneNo string is expected to be 8 or 10 characters long.");
175: }
176:
177: /** Returns the value inside this instance of PhoneNo as readable string */
178: public String toString() {
179: if (isEmpty())
180: return EMPTY_STRING;
181: if (isConcealed())
182: return CONCEALED_STRING;
183: return mValue;
184: }
185:
186: /** Indicates whether some other object is \"equal to\" this one
187: * @return true if this object is the same as the obj argument; false otherwise. */
188: public boolean equals(Object pOtherObject) {
189: if (pOtherObject == null)
190: return false;
191: if ((pOtherObject instanceof PhoneNo) == false)
192: return false;
193: if (((PhoneNo) pOtherObject).isConcealed() || isConcealed())
194: return false; // One of the instances is concealed
195: if (((PhoneNo) pOtherObject).isEmpty() && isEmpty())
196: return true; // Both instances are empty.
197: if (((PhoneNo) pOtherObject).isEmpty() != isEmpty())
198: return false; // One of the instances is empty, the other one is not
199: return ((PhoneNo) pOtherObject).mValue.equals(mValue);
200: }
201:
202: /** Returns a hash code value for the object. This method is supported for the benefit of hashtables */
203: public int hashCode() {
204: return mValue.hashCode();
205: }
206:
207: /** Returns true if the instance contains an empty value */
208: public boolean isEmpty() {
209: return mValue != null && mValue.equals(EMPTY_VALUE);
210: }
211:
212: /** Returns true if the instance contains concealed value */
213: public boolean isConcealed() {
214: return mValue != null && mValue.equals(CONCEALED_VALUE);
215: }
216:
217: // Private access. External callers must use one of the static creators
218: private PhoneNo() {
219: }
220: }
|