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-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package designlibrary.core;
016:
017: import java.io.PrintWriter;
018: import java.util.Properties;
019:
020: import com.metaboss.enterprise.bs.BSException;
021: import com.metaboss.javatemplate.JavaTemplate;
022: import com.metaboss.javatemplate.JavaTemplateContext;
023: import com.metaboss.javatemplate.JavaTemplateException;
024: import com.metaboss.sdlctools.models.metabossmodel.datadictionarymodel.DataType;
025: import com.metaboss.sdlctools.services.codegeneration.CodeGenerationStylesheetAccessor;
026: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDatatypeStylesheet;
027: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDocumentationStylesheet;
028:
029: /** The basic password datatype. */
030: public final class PasswordField implements JavaTemplate {
031: /* This method is called to perform the template merge with the values supplied in context */
032: public void mergeTemplate(PrintWriter pOutputWriter,
033: JavaTemplateContext pContext) throws JavaTemplateException {
034: // Access mandatory elements of the context
035: DataType lDatatype = (DataType) pContext
036: .getMandatoryObject("DataType");
037: STDocumentationStylesheet lDocumentationStylesheet = null;
038: STDatatypeStylesheet lDataTypeStylesheet = null;
039: try {
040: lDocumentationStylesheet = CodeGenerationStylesheetAccessor
041: .getDocumentationStylesheet();
042: lDataTypeStylesheet = CodeGenerationStylesheetAccessor
043: .getDatatypeStylesheet(lDatatype);
044: } catch (BSException e) {
045: throw new JavaTemplateException(
046: "Unable to obtain Datatype stylesheet for "
047: + lDatatype.getRef(), e);
048: }
049: String lDatatypeName = lDataTypeStylesheet.getNormalisedName();
050: Properties lTypetemplateProperties = lDataTypeStylesheet
051: .getTypetemplateProperties();
052: String lEmptyString = lTypetemplateProperties.getProperty(
053: "EmptyString", "");
054: String lConcealedString = lTypetemplateProperties.getProperty(
055: "ConcealedString", "xxxxxx");
056: int lMaxLength = Integer.parseInt(lTypetemplateProperties
057: .getProperty("MaxLength", "20"));
058: int lMinLength = Integer.parseInt(lTypetemplateProperties
059: .getProperty("MinLength", "8"));
060:
061: pOutputWriter.println(lDocumentationStylesheet
062: .getCopyrightCommentInJava());
063: pOutputWriter.println("package "
064: + lDataTypeStylesheet.getPackageName() + ";");
065: pOutputWriter.println();
066: pOutputWriter.println("import java.io.Serializable;");
067: pOutputWriter
068: .println("import com.metaboss.enterprise.datatypes.DataType;");
069: pOutputWriter
070: .println("import com.metaboss.enterprise.datatypes.DataTypeTranslationMetadata;");
071: pOutputWriter
072: .println("import com.metaboss.enterprise.datatypes.DataTypeValidationException;");
073: pOutputWriter
074: .println("import com.metaboss.enterprise.datatypes.DefaultDataTypeTranslationMetadataImplementation;");
075: pOutputWriter
076: .println("import com.metaboss.enterprise.datatypes.DataTypeInstanceCreationFailureException;");
077: pOutputWriter
078: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForEmptyInstanceException;");
079: pOutputWriter
080: .println("import com.metaboss.enterprise.datatypes.DataTypeInvalidOperationForConcealedInstanceException;");
081: pOutputWriter
082: .println("import com.metaboss.enterprise.datatypes.sqltranslation.SqlVarcharTranslator;");
083: pOutputWriter
084: .println("import com.metaboss.enterprise.datatypes.primitivetranslation.PrimitiveStringTranslator;");
085: pOutputWriter
086: .println("import com.metaboss.enterprise.datatypes.xmltranslation.XmlTextTranslator;");
087:
088: String lDescriptionHtmlText = lDataTypeStylesheet
089: .getDescriptionHtmlText();
090: pOutputWriter.println("/** " + lDescriptionHtmlText
091: + (lDescriptionHtmlText.endsWith(".") ? "" : "."));
092: pOutputWriter
093: .println(" * <p>This datatype is a password type, which means that it contains a sencitive string value (eg. password).");
094: pOutputWriter
095: .println(" * The string gets encrypted / decrypted on transmission. This datatype has following characteristics:</p>");
096: pOutputWriter.println(" * <p><table border=\"0\">");
097: pOutputWriter
098: .println(" * <tr><td>Empty Value:</td><td> </td><td>'"
099: + lEmptyString + "'</td></tr>");
100: pOutputWriter
101: .println(" * <tr><td>Concealed Value:</td><td> </td><td>'"
102: + lConcealedString + "'</td></tr>");
103: pOutputWriter
104: .println(" * <tr><td>Minimum Length (characters):</td><td> </td><td>"
105: + Integer.toString(lMinLength) + "</td></tr>");
106: pOutputWriter
107: .println(" * <tr><td>Maximum Length (characters):</td><td> </td><td>"
108: + Integer.toString(lMaxLength) + "</td></tr>");
109: pOutputWriter.println(" * </table></p>");
110: pOutputWriter.println(" */");
111: pOutputWriter.println("public final class " + lDatatypeName
112: + " implements DataType, Serializable");
113: pOutputWriter.println("{");
114: pOutputWriter
115: .println(" // Static storage for translation metadata. Create once and return many times");
116: pOutputWriter
117: .println(" private static DataTypeTranslationMetadata sTranslationMetadata = null;");
118: pOutputWriter
119: .println(" // Static storage for the empty string constant.");
120: pOutputWriter
121: .println(" private static final String sEmptyString = \""
122: + lEmptyString + "\";");
123: pOutputWriter
124: .println(" // Static storage for the concealed string constant.");
125: pOutputWriter
126: .println(" private static final String sConcealedString = \""
127: + lConcealedString + "\";");
128: pOutputWriter
129: .println(" // State is stored here : 0 - normal, 1 - empty, 2 - concealed");
130: pOutputWriter.println(" private byte mState = 0;");
131: pOutputWriter.println(" // Actual value stored here");
132: pOutputWriter.println(" private String mValue;");
133:
134: pOutputWriter.println(" static");
135: pOutputWriter.println(" {");
136: pOutputWriter
137: .println(" SqlVarcharTranslator lSqlTranslator = new SqlVarcharTranslator()");
138: pOutputWriter.println(" {");
139: pOutputWriter
140: .println(" public String getSqlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
141: pOutputWriter.println(" {");
142: pOutputWriter.println(" " + lDatatypeName
143: + " lInstance = (" + lDatatypeName
144: + ")pDataTypeInstance;");
145: pOutputWriter
146: .println(" if (lInstance.isConcealed())");
147: pOutputWriter
148: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
149: pOutputWriter
150: .println(" if (lInstance.isEmpty())");
151: pOutputWriter
152: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
153: pOutputWriter
154: .println(" return lInstance.mValue;");
155: pOutputWriter.println(" }");
156: pOutputWriter
157: .println(" public DataType createFromSqlValue(String pSqlValue) throws DataTypeInstanceCreationFailureException");
158: pOutputWriter.println(" {");
159: pOutputWriter.println(" if (pSqlValue == null)");
160: pOutputWriter
161: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Sql value is null.\");");
162: pOutputWriter.println(" " + lDatatypeName
163: + " lValue = new " + lDatatypeName + "();");
164: pOutputWriter
165: .println(" lValue.mValue = pSqlValue;");
166: pOutputWriter.println(" return lValue;");
167: pOutputWriter.println(" }");
168: pOutputWriter.println(" };");
169: pOutputWriter
170: .println(" PrimitiveStringTranslator lPrimitiveTranslator = new PrimitiveStringTranslator()");
171: pOutputWriter.println(" {");
172: pOutputWriter
173: .println(" public String getPrimitiveValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
174: pOutputWriter.println(" {");
175: pOutputWriter.println(" " + lDatatypeName
176: + " lInstance = (" + lDatatypeName
177: + ")pDataTypeInstance;");
178: pOutputWriter
179: .println(" if (lInstance.isConcealed())");
180: pOutputWriter
181: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
182: pOutputWriter
183: .println(" if (lInstance.isEmpty())");
184: pOutputWriter
185: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
186: pOutputWriter
187: .println(" return lInstance.mValue;");
188: pOutputWriter.println(" }");
189: pOutputWriter
190: .println(" public DataType createFromPrimitiveValue(String pPrimitiveValue) throws DataTypeInstanceCreationFailureException");
191: pOutputWriter.println(" {");
192: pOutputWriter
193: .println(" if (pPrimitiveValue == null)");
194: pOutputWriter
195: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Primitive value is null.\");");
196: pOutputWriter.println(" " + lDatatypeName
197: + " lValue = new " + lDatatypeName + "();");
198: pOutputWriter
199: .println(" lValue.mValue = pPrimitiveValue;");
200: pOutputWriter.println(" return lValue;");
201: pOutputWriter.println(" }");
202: pOutputWriter.println(" };");
203: pOutputWriter
204: .println(" XmlTextTranslator lXmlTranslator = new XmlTextTranslator()");
205: pOutputWriter.println(" {");
206: pOutputWriter
207: .println(" public String getXmlValue(DataType pDataTypeInstance) throws ClassCastException, DataTypeInvalidOperationForEmptyInstanceException, DataTypeInvalidOperationForConcealedInstanceException");
208: pOutputWriter.println(" {");
209: pOutputWriter.println(" " + lDatatypeName
210: + " lInstance = (" + lDatatypeName
211: + ")pDataTypeInstance;");
212: pOutputWriter
213: .println(" if (lInstance.isConcealed())");
214: pOutputWriter
215: .println(" throw new DataTypeInvalidOperationForConcealedInstanceException();");
216: pOutputWriter
217: .println(" if (lInstance.isEmpty())");
218: pOutputWriter
219: .println(" throw new DataTypeInvalidOperationForEmptyInstanceException();");
220: pOutputWriter
221: .println(" return lInstance.mValue;");
222: pOutputWriter.println(" }");
223: pOutputWriter
224: .println(" public DataType createFromXmlValue(String pXmlValue) throws DataTypeInstanceCreationFailureException");
225: pOutputWriter.println(" {");
226: pOutputWriter.println(" if (pXmlValue == null)");
227: pOutputWriter
228: .println(" throw new DataTypeInstanceCreationFailureException(\"Supplied Xml value is null.\");");
229: pOutputWriter.println(" " + lDatatypeName
230: + " lValue = new " + lDatatypeName + "();");
231: pOutputWriter
232: .println(" lValue.mValue = pXmlValue;");
233: pOutputWriter.println(" return lValue;");
234: pOutputWriter.println(" }");
235: pOutputWriter.println(" };");
236: pOutputWriter
237: .println(" java.util.Properties lProperties = new java.util.Properties();");
238: if (lMinLength > 0)
239: pOutputWriter
240: .println(" lProperties.setProperty(\"minsize\",\""
241: + lMinLength + "\");");
242: pOutputWriter
243: .println(" lProperties.setProperty(\"maxsize\",\""
244: + lMaxLength + "\");");
245: pOutputWriter
246: .println(" sTranslationMetadata = new DefaultDataTypeTranslationMetadataImplementation(DataTypeTranslationMetadata.SQL_VARCHAR, lProperties, lSqlTranslator, DataTypeTranslationMetadata.PRIMITIVE_STRING, null, lPrimitiveTranslator, DataTypeTranslationMetadata.XML_TEXT, null, lXmlTranslator);");
247: pOutputWriter.println(" }");
248:
249: pOutputWriter
250: .println(" /** Returns this datatype's translation metadata.");
251: pOutputWriter
252: .println(" * See {@link com.metaboss.enterprise.datatypes.DataType DataType} for more details */");
253: pOutputWriter
254: .println(" public static DataTypeTranslationMetadata getTranslationMetadata()");
255: pOutputWriter.println(" {");
256: pOutputWriter.println(" return sTranslationMetadata;");
257: pOutputWriter.println(" }");
258:
259: pOutputWriter
260: .println(" /** Creates an instance of the datatype carrying an empty value */");
261: pOutputWriter.println(" public static " + lDatatypeName
262: + " createEmpty()");
263: pOutputWriter.println(" {");
264: pOutputWriter.println(" " + lDatatypeName
265: + " lValue = new " + lDatatypeName + "();");
266: pOutputWriter.println(" lValue.mState = 1;");
267: pOutputWriter.println(" return lValue;");
268: pOutputWriter.println(" }");
269:
270: pOutputWriter
271: .println(" /** Creates an instance of the datatype carrying concealed value */");
272: pOutputWriter.println(" public static " + lDatatypeName
273: + " createConcealed()");
274: pOutputWriter.println(" {");
275: pOutputWriter.println(" " + lDatatypeName
276: + " lValue = new " + lDatatypeName + "();");
277: pOutputWriter.println(" lValue.mState = 2;");
278: pOutputWriter.println(" return lValue;");
279: pOutputWriter.println(" }");
280:
281: pOutputWriter
282: .println(" /** Creates an instance of the datatype from given readable value */");
283: pOutputWriter
284: .println(" public static "
285: + lDatatypeName
286: + " createFromString( String pSourceString) throws DataTypeValidationException");
287: pOutputWriter.println(" {");
288: pOutputWriter.println(" // Perform validation");
289: pOutputWriter.println(" validateString(pSourceString);");
290: pOutputWriter.println(" // Create instance");
291: pOutputWriter
292: .println(" if (pSourceString.equals(sEmptyString))");
293: pOutputWriter.println(" return createEmpty();");
294: pOutputWriter
295: .println(" if (pSourceString.equals(sConcealedString))");
296: pOutputWriter.println(" return createConcealed();");
297: pOutputWriter
298: .println(" // Create instance (not encrypted yet at the moment)");
299: pOutputWriter.println(" " + lDatatypeName
300: + " lValue = new " + lDatatypeName + "();");
301: pOutputWriter.println(" lValue.mValue = pSourceString;");
302: pOutputWriter.println(" return lValue;");
303: pOutputWriter.println(" }");
304:
305: pOutputWriter
306: .println(" /** Validates proposed readable value */");
307: pOutputWriter
308: .println(" public static void validateString( String pProposedString) throws DataTypeValidationException");
309: pOutputWriter.println(" {");
310: pOutputWriter.println(" if (pProposedString == null)");
311: pOutputWriter
312: .println(" throw new DataTypeValidationException(\"Null initialisation string is not allowed for "
313: + lDatatypeName
314: + ". Use createEmpty() method to create an empty "
315: + lDatatypeName + "\");");
316: pOutputWriter
317: .println(" if (pProposedString.equals(sEmptyString))");
318: pOutputWriter.println(" return; // Done validation");
319: pOutputWriter
320: .println(" if (pProposedString.equals(sConcealedString))");
321: pOutputWriter
322: .println(" return; // Done validation");
323: pOutputWriter
324: .println(" int lLength = pProposedString.length();");
325: pOutputWriter.println(" if (lLength > " + lMaxLength
326: + ")");
327: pOutputWriter
328: .println(" throw new DataTypeValidationException(\"String is too long for "
329: + lDatatypeName
330: + ". Expected max length is "
331: + lMaxLength + " characters.\");");
332: if (lMinLength > 0) {
333: pOutputWriter.println(" if (lLength < " + lMinLength
334: + ")");
335: pOutputWriter
336: .println(" throw new DataTypeValidationException(\"String is too short for "
337: + lDatatypeName
338: + ". Expected min length is "
339: + lMinLength
340: + " characters.\");");
341: }
342: pOutputWriter.println(" }");
343:
344: pOutputWriter
345: .println(" /** Returns the value inside this instance of "
346: + lDatatypeName + " as readable string */");
347: pOutputWriter.println(" public String toString()");
348: pOutputWriter.println(" {");
349: pOutputWriter.println(" if (isEmpty())");
350: pOutputWriter.println(" return sEmptyString;");
351: pOutputWriter.println(" if (isConcealed())");
352: pOutputWriter.println(" return sConcealedString;");
353: pOutputWriter.println(" return mValue;");
354: pOutputWriter.println(" }");
355:
356: pOutputWriter
357: .println(" /** Indicates whether some other object is \"equal to\" this one");
358: pOutputWriter
359: .println(" * @return true if this object is the same as the obj argument; false otherwise. */");
360: pOutputWriter
361: .println(" public boolean equals(Object pOtherObject)");
362: pOutputWriter.println(" {");
363: pOutputWriter.println(" if (pOtherObject == null)");
364: pOutputWriter.println(" return false;");
365: pOutputWriter.println(" if ((pOtherObject instanceof "
366: + lDatatypeName + ") == false)");
367: pOutputWriter.println(" return false;");
368: pOutputWriter.println(" if (((" + lDatatypeName
369: + ")pOtherObject).isConcealed() || isConcealed())");
370: pOutputWriter
371: .println(" return false; // One of the instances is concealed");
372: pOutputWriter.println(" if (((" + lDatatypeName
373: + ")pOtherObject).isEmpty() && isEmpty())");
374: pOutputWriter
375: .println(" return true; // Both instances are empty.");
376: pOutputWriter.println(" if (((" + lDatatypeName
377: + ")pOtherObject).isEmpty() != isEmpty())");
378: pOutputWriter
379: .println(" return false; // One of the instances is empty, the other one is not");
380: pOutputWriter.println(" return ((" + lDatatypeName
381: + ")pOtherObject).mValue.equals(mValue);");
382: pOutputWriter.println(" }");
383:
384: pOutputWriter
385: .println(" /** Returns a hash code value for the object. This method is supported for the benefit of hashtables");
386: pOutputWriter
387: .println(" * @return instance's hash code or zero if this instance is empty or concealed */");
388: pOutputWriter.println(" public int hashCode()");
389: pOutputWriter.println(" {");
390: pOutputWriter
391: .println(" if (isEmpty() || isConcealed())");
392: pOutputWriter.println(" return 0;");
393: pOutputWriter.println(" return mValue.hashCode();");
394: pOutputWriter.println(" }");
395:
396: pOutputWriter
397: .println(" /** Returns true if the instance contains an empty value */");
398: pOutputWriter.println(" public boolean isEmpty()");
399: pOutputWriter.println(" {");
400: pOutputWriter.println(" return mState == 1;");
401: pOutputWriter.println(" }");
402:
403: pOutputWriter
404: .println(" /** Returns true if the instance contains concealed value */");
405: pOutputWriter.println(" public boolean isConcealed()");
406: pOutputWriter.println(" {");
407: pOutputWriter.println(" return mState == 2;");
408: pOutputWriter.println(" }");
409:
410: pOutputWriter
411: .println(" // Private access. External callers must use one of the static creators");
412: pOutputWriter.println(" private " + lDatatypeName + "()");
413: pOutputWriter.println(" {");
414: pOutputWriter.println(" }");
415: pOutputWriter.println("}");
416: }
417: }
|