001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049: package org.jaffa.tools.patternmetaengine;
050:
051: import java.io.File;
052: import java.io.FileOutputStream;
053: import java.io.IOException;
054: import java.io.FileNotFoundException;
055: import java.util.List;
056: import java.util.Iterator;
057: import java.util.ArrayList;
058: import java.util.Properties;
059:
060: import javax.xml.bind.JAXBContext;
061: import javax.xml.bind.Validator;
062: import javax.xml.bind.ValidationException;
063: import javax.xml.bind.JAXBException;
064: import javax.xml.bind.Marshaller;
065: import javax.xml.bind.util.ValidationEventCollector;
066: import javax.xml.bind.ValidationEvent;
067:
068: import org.apache.log4j.Logger;
069: import org.jaffa.util.StringHelper;
070: import org.jaffa.datatypes.Defaults;
071: import org.jaffa.tools.patternmetaengine.domain.ApplicationBuilder;
072: import org.jaffa.tools.patternmetaengine.domain.Module;
073: import org.jaffa.patterns.library.domain_creator_1_1.domain.Field;
074: import org.jaffa.patterns.library.domain_creator_1_1.domain.Root;
075: import org.jaffa.patterns.library.object_lookup_meta_1_0.domain.*;
076:
077: /**
078: *
079: * @author PaulE
080: */
081: public class BuildObjectLookup_1 implements IBuilder {
082:
083: /** Set up Logging for Log4J */
084: private static Logger log = Logger
085: .getLogger(BuildObjectLookup.class);
086:
087: private ApplicationBuilder m_app = null;
088: private Module m_module = null;
089: private Root m_domain = null;
090:
091: private ObjectLookupMeta m_lookup = null;
092: private Properties m_labels = null;
093:
094: private ComponentRegistry m_compReg = null;
095:
096: /** Creates a new instance of BuildObjectLookup */
097: public BuildObjectLookup_1(
098: ApplicationBuilder app,
099: ComponentRegistry comps,
100: Module module,
101: org.jaffa.patterns.library.domain_creator_1_1.domain.Root domain,
102: Properties labels) {
103:
104: log.debug("Create Lookup For - " + domain.getDomainObject());
105: m_compReg = comps;
106: m_app = app;
107: m_module = module;
108: m_domain = domain;
109: m_labels = labels;
110: }
111:
112: /** Causes the meta data object to be build ready for saving.
113: * @param fullPackage If true then the .applications. and .modules. package names
114: * are used. If this is false, these are ommited for a much more condensed package
115: * naming convention
116: */
117: public void buildPhase1(boolean fullPackage) throws Exception {
118: ObjectFactory objFactory = new ObjectFactory();
119: try {
120: m_lookup = objFactory.createObjectLookupMeta();
121: m_lookup
122: .setPatternTemplate("patterns/library/object_lookup_1_0/ObjectLookupPattern.xml"); // Mand
123: m_lookup.setApplication(m_app.getApplicationName()); // Mand
124: m_lookup.setModule(m_module.getName()); // Mand
125: m_lookup
126: .setComponent(m_domain.getDomainObject() + "Lookup"); // Mand
127: StringBuffer sb = new StringBuffer();
128: sb.append(m_app.getPackagePrefix());
129: sb.append(".");
130: if (fullPackage)
131: sb.append("applications.");
132: sb.append(m_app.getApplicationName());
133: sb.append(".");
134: if (fullPackage)
135: sb.append("modules.");
136: sb.append(m_module.getName());
137: m_lookup.setBasePackage(sb.toString().toLowerCase()); // Mand
138: m_lookup.setDomainObject(m_domain.getDomainObject()); // Mand
139: m_lookup.setDomainPackage(m_domain.getDomainPackage()); // Mand
140:
141: // Create Labels for the screen titles
142: String labelDomain = "label."
143: + StringHelper
144: .getUpper1(m_app.getApplicationName())
145: + "."
146: + StringHelper.getUpper1(m_module.getName())
147: + "."
148: + StringHelper
149: .getUpper1(m_domain.getDomainObject());
150: String labelId = "title."
151: + StringHelper
152: .getUpper1(m_app.getApplicationName())
153: + "."
154: + StringHelper.getUpper1(m_module.getName())
155: + "."
156: + StringHelper
157: .getUpper1(m_domain.getDomainObject())
158: + "Lookup.";
159: // Criteia Title
160: m_lookup.setCriteriaTitle(labelId + "criteria"); // Opt
161: m_labels.put(m_lookup.getCriteriaTitle(), "[" + labelDomain
162: + "] Criteria");
163: // Results Title
164: m_lookup.setResultsTitle(labelId + "results"); // Opt
165: m_labels.put(m_lookup.getResultsTitle(), "[" + labelDomain
166: + "] List");
167:
168: // Add All Fields as Criteria Fields
169: List fields = m_domain.getFields().getField();
170: if (fields == null || fields.isEmpty()) {
171: log
172: .error("Domain Object "
173: + m_domain.getDomainObject()
174: + " has no fields, this is needed to build a valid lookup");
175: } else {
176:
177: // Create Criteria Fields
178: CriteriaFields cfields = objFactory
179: .createCriteriaFields();
180: m_lookup.setCriteriaFields(cfields);
181: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
182: Field fld = (Field) it1.next();
183:
184: CriteriaField cfld = objFactory
185: .createCriteriaField();
186: cfields.getCriteriaField().add(cfld);
187:
188: cfld.setName(reservedName(fld.getName()));
189:
190: String dt = Defaults.getDataType(fld.getDataType());
191: if (dt == null)
192: throw new Exception(
193: "Can't Translate Java Class "
194: + fld.getDataType()
195: + " to a supported Data Type");
196: cfld.setDataType(dt);
197:
198: cfld.setDisplay(true);
199: cfld.setDomainField(fld.getName());
200: }
201:
202: // Create Results Fields
203: ResultsFields rfields = objFactory
204: .createResultsFields();
205: m_lookup.setResultsFields(rfields);
206: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
207: Field fld = (Field) it1.next();
208:
209: ResultsField rfld = objFactory.createResultsField();
210: rfields.getResultsField().add(rfld);
211:
212: rfld.setName(reservedName(fld.getName()));
213: String dt = Defaults.getDataType(fld.getDataType());
214: if (dt == null)
215: throw new Exception(
216: "Can't Translate Java Class "
217: + fld.getDataType()
218: + " to a supported Data Type");
219: rfld.setDataType(dt);
220:
221: rfld.setDisplay(true);
222: rfld.setLabel("[" + labelDomain + "."
223: + fld.getName() + "]");
224: rfld.setWidth("300px");
225: rfld.setDomainField(fld.getName());
226: }
227:
228: // Create Key Fields
229: KeyFields kfields = objFactory.createKeyFields();
230: m_lookup.setKeyFields(kfields);
231: for (Iterator it1 = fields.iterator(); it1.hasNext();) {
232: Field fld = (Field) it1.next();
233: if (fld.getPrimaryKey().equalsIgnoreCase("t")) {
234:
235: // This is a key field
236: KeyField kfld = objFactory.createKeyField();
237: kfields.getKeyField().add(kfld);
238:
239: kfld.setResultsFieldName(reservedName(fld
240: .getName()));
241: String dt = Defaults.getDataType(fld
242: .getDataType());
243: if (dt == null)
244: throw new Exception(
245: "Can't Translate Java Class "
246: + fld.getDataType()
247: + " to a supported Data Type");
248: kfld.setDataType(dt);
249: kfld
250: .setFieldNameInTargetComponent(reservedName(fld
251: .getName()));
252: }
253: }
254: }
255:
256: } catch (JAXBException e) {
257: log.error("Failed to create Lookup Object");
258: }
259: }
260:
261: /** Saves the generated meta data to the prespecified location as an XML file
262: * NOTE: assumes that the build(..) method has been called!
263: */
264: public boolean save() {
265: String filename = m_app.getOutputRoot()
266: + m_app.getOutputLookups() + m_domain.getDomainObject()
267: + "Lookup.xml";
268: File file = new File(filename);
269: File path = new File(file.getParent());
270: if (!path.exists())
271: path.mkdirs();
272:
273: // Create output stream
274: FileOutputStream out = null;
275: try {
276: try {
277: out = new FileOutputStream(file.getPath());
278: } catch (FileNotFoundException e) {
279: log.error("Failed to open output stream !", e);
280: return false;
281: }
282:
283: try {
284: // create a JAXBContext capable of handling classes generated into the package
285: JAXBContext jc = JAXBContext
286: .newInstance("org.jaffa.patterns.library.object_lookup_meta_1_0.domain");
287: // create a Validator
288: Validator v = jc.createValidator();
289: ValidationEventCollector valErrors = new ValidationEventCollector();
290: v.setEventHandler(valErrors);
291: // validate the content tree
292: if (!v.validateRoot(m_lookup)) {
293: log.error("Failed to validate Structure !");
294: JAXBHelper.showErrors(log, valErrors);
295: return false;
296: }
297:
298: // Write out XML document to file
299: Marshaller m = jc.createMarshaller();
300:
301: //m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
302: //m.marshal(m_lookup, out);
303: JAXBHelper
304: .marshalWithDocType(
305: m,
306: m_lookup,
307: out,
308: "Root",
309: "-//JAFFA//DTD Object Lookup Meta 1.0//EN",
310: "http://jaffa.sourceforge.net/DTD/object-lookup-meta_1_0.dtd");
311:
312: } catch (ValidationException e) {
313: log.error("Failed to validate Structure !", e);
314: return false;
315: } catch (JAXBException e) {
316: log
317: .error(
318: "Failed to marshal xml to output stream !",
319: e);
320: return false;
321: }
322: } finally {
323: if (out != null)
324: try {
325: out.close();
326: } catch (IOException e) {
327: }
328: }
329: return true;
330: }
331:
332: private static List reservedAttr = null;
333: static {
334: reservedAttr = new ArrayList();
335: reservedAttr.add("component");
336: }
337:
338: private String reservedName(String name) {
339: if (reservedAttr.contains(name.toLowerCase()))
340: return name + "1";
341: else
342: return name;
343: }
344:
345: public String getApplication() {
346: return m_lookup.getApplication();
347: }
348:
349: public String getComponentControllerClass() {
350: return m_lookup.getComponent() + "Component";
351: }
352:
353: public String getComponentControllerPackage() {
354: return m_lookup.getBasePackage() + ".components."
355: + m_lookup.getComponent().toLowerCase() + ".ui";
356: }
357:
358: public String getComponentName() {
359: return getModule() + "."
360: + StringHelper.getUpper1(m_lookup.getComponent());
361: }
362:
363: public String getComponentType() {
364: return "Lookup";
365: }
366:
367: public String getDomain() {
368: return m_lookup.getDomainPackage() + "."
369: + m_lookup.getDomainObject();
370: }
371:
372: public String getModule() {
373: return StringHelper.getUpper1(m_lookup.getModule());
374: }
375:
376: public String getName() {
377: return StringHelper.getUpper1(m_lookup.getComponent());
378: }
379:
380: /** Causes the meta data object to be build ready for saving.
381: * @param fullPackage If true then the .applications. and .modules. package names
382: * are used. If this is false, these are ommited for a much more condensed package
383: * naming convention
384: */
385: public void buildPhase2(boolean fullPackage) throws Exception {
386: ObjectFactory objFactory = new ObjectFactory();
387:
388: // Find The Viewer Component To Link To
389: IBuilder vcomp = m_compReg.findComponent(this .getDomain(),
390: "Viewer");
391: if (vcomp != null) {
392: Viewer viewer = objFactory.createViewer();
393: m_lookup.setViewer(viewer);
394: viewer.setClassName(vcomp.getComponentControllerClass());
395: viewer.setComponentName(vcomp.getComponentName());
396: viewer.setPackage(vcomp.getComponentControllerPackage());
397: }
398:
399: // Find The Maintenance Component To Link To
400: IBuilder mcomp = m_compReg.findComponent(this .getDomain(),
401: "Maintenance");
402: if (mcomp != null) {
403: // Updator
404: Updator updator = objFactory.createUpdator();
405: m_lookup.setUpdator(updator);
406: updator.setClassName(mcomp.getComponentControllerClass());
407: updator.setComponentName(mcomp.getComponentName());
408: updator.setPackage(mcomp.getComponentControllerPackage());
409: // Creator
410: Creator creator = objFactory.createCreator();
411: m_lookup.setCreator(creator);
412: creator.setClassName(mcomp.getComponentControllerClass());
413: creator.setComponentName(mcomp.getComponentName());
414: creator.setPackage(mcomp.getComponentControllerPackage());
415: // Creator
416: Deletor deletor = objFactory.createDeletor();
417: m_lookup.setDeletor(deletor);
418: deletor.setClassName(mcomp.getComponentControllerClass());
419: deletor.setComponentName(mcomp.getComponentName());
420: deletor.setPackage(mcomp.getComponentControllerPackage());
421: }
422:
423: }
424:
425: }
|