001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.ui;
018:
019: import com.sourcetap.sfa.util.StringHelper;
020:
021: import org.ofbiz.entity.GenericDelegator;
022: import org.ofbiz.entity.GenericEntityException;
023: import org.ofbiz.entity.GenericValue;
024: import org.ofbiz.entity.model.ModelEntity;
025: import org.ofbiz.entity.model.ModelField;
026: import org.ofbiz.entity.model.ModelUtil;
027: import org.ofbiz.base.util.Debug;
028:
029: import java.io.BufferedWriter;
030: import java.io.FileWriter;
031: import java.io.IOException;
032:
033: import java.util.*;
034:
035: /**
036: * DOCUMENT ME!
037: *
038: */
039: public class UISQLFileWriter {
040: public static final String module = UISQLFileWriter.class.getName();
041:
042: /* public static void main (String[] argument) {
043: if (argument.length != 3) {
044: Debug.logError("Usage:", module);
045: Debug.logError("UIGenerateSQL <party ID> <screen name like> <path>", module);
046: return;
047: }
048: String partyId = argument[0];
049: String screenName = argument[1];
050: String filePath = argument[2];
051: UIGenerateScreenSQL uIGenerateScreenSQL = new UIGenerateScreenSQL();
052: GenericDelegator delegator = new GenericDelegator("UIGenerateScreenSQLDelegator");
053: uIGenerateScreenSQL.writeUiScreenSql(delegator, partyId, screenName, filePath);
054: return;
055: }
056: */
057: public String writeUiScreenFile(GenericDelegator delegator,
058: String partyId, String screenId, String filePath) {
059: HashMap findHashMap = new HashMap();
060: ArrayList findOrder = null;
061: ModelEntity modelEntity = null;
062: GenericValue genericValue = null;
063: String entityName = "";
064: String resultString = "";
065:
066: try {
067: // Open the file, and replace any existing contents.
068: FileWriter tempFile = new FileWriter(filePath, false);
069: BufferedWriter outputFile = new BufferedWriter(tempFile);
070:
071: ////////////////////
072: // Write out the SQL for inserting the screen.
073: ////////////////////
074: List screenGVL = null;
075: findHashMap = new HashMap();
076: findHashMap.put("screenId", screenId);
077: findOrder = new ArrayList();
078: findOrder.add("screenId");
079: screenGVL = writeSqlInserts("UiScreen", findHashMap,
080: findOrder, delegator, outputFile);
081:
082: if (screenGVL.size() <= 0) {
083: return "No screen found with ID \"" + screenId + "\"";
084: } else {
085: resultString += (String.valueOf(screenGVL.size()) + " screens<BR>\n");
086: }
087:
088: ////////////////////
089: // Write out the SQL for inserting the screen sections.
090: ////////////////////
091: // Loop through the screens. (Should be only one.)
092: Iterator screenGVI = screenGVL.iterator();
093: List screenSectionGVL = new LinkedList();
094:
095: while (screenGVI.hasNext()) {
096: GenericValue screenGV = (GenericValue) screenGVI.next();
097:
098: findHashMap = new HashMap();
099: findHashMap.put("screenId", screenGV
100: .getString("screenId"));
101: findOrder = new ArrayList();
102: findOrder.add("sectionId");
103: screenSectionGVL.addAll(writeSqlInserts(
104: "UiScreenSection", findHashMap, findOrder,
105: delegator, outputFile));
106: }
107:
108: resultString += (String.valueOf(screenSectionGVL.size()) + " sections<BR>\n");
109: screenGVI = null;
110:
111: ////////////////////
112: // Write out the SQL for inserting the screen section entities and screen section infos (fields).
113: ////////////////////
114: Iterator screenSectionGVI = screenSectionGVL.iterator();
115: List screenSectionEntityGVL = new LinkedList();
116: List screenSectionInfoGVL = new LinkedList();
117:
118: while (screenSectionGVI.hasNext()) {
119: GenericValue screenSectionGV = (GenericValue) screenSectionGVI
120: .next();
121:
122: findHashMap = new HashMap();
123: findHashMap.put("sectionId", screenSectionGV
124: .getString("sectionId"));
125: findOrder = new ArrayList();
126: findOrder.add("entityId");
127: screenSectionEntityGVL.addAll(writeSqlInserts(
128: "UiScreenSectionEntity", findHashMap,
129: findOrder, delegator, outputFile));
130:
131: findHashMap = new HashMap();
132: findHashMap.put("sectionId", screenSectionGV
133: .getString("sectionId"));
134: findHashMap.put("partyId", partyId);
135: findOrder = new ArrayList();
136: findOrder.add("attributeId");
137: screenSectionInfoGVL.addAll(writeSqlInserts(
138: "UiScreenSectionInfo", findHashMap, findOrder,
139: delegator, outputFile));
140: }
141:
142: resultString += (String.valueOf(screenSectionEntityGVL
143: .size()) + " section entities<BR>\n");
144: resultString += (String
145: .valueOf(screenSectionInfoGVL.size()) + " section fields");
146: screenSectionGVI = null;
147:
148: outputFile.close();
149:
150: return resultString;
151: } catch (IOException e) {
152: Debug
153: .logError(
154: "[UISQLFileWriter.writeUiScreenSql]: IO Exception - Error was:",
155: module);
156: Debug.logError(e.getMessage(), module);
157:
158: return e.getMessage();
159: } catch (SecurityException se) {
160: Debug
161: .logError(
162: "[UISQLFileWriter.writeUiScreenSql]: Security Exception - Error was:",
163: module);
164: Debug.logError(se.getMessage(), module);
165:
166: return se.getMessage();
167: }
168: }
169:
170: /**
171: * DOCUMENT ME!
172: *
173: * @param delegator
174: * @param displayObjectId
175: * @param filePath
176: *
177: * @return
178: */
179: public String writeUiDisplayObjectFile(GenericDelegator delegator,
180: String displayObjectId, String filePath) {
181: HashMap findHashMap = new HashMap();
182: ArrayList findOrder = null;
183: ModelEntity modelEntity = null;
184: GenericValue genericValue = null;
185: String entityName = "";
186: String resultString = "";
187:
188: try {
189: // Open the file, and replace any existing contents.
190: FileWriter tempFile = new FileWriter(filePath, false);
191: BufferedWriter outputFile = new BufferedWriter(tempFile);
192:
193: ////////////////////
194: // Write out the SQL for inserting the display object.
195: ////////////////////
196: List uiDisplayObjectGVL = null;
197: findHashMap = new HashMap();
198: findHashMap.put("displayObjectId", displayObjectId);
199: findOrder = new ArrayList();
200: findOrder.add("displayObjectId");
201: uiDisplayObjectGVL = writeSqlInserts("UiDisplayObject",
202: findHashMap, findOrder, delegator, outputFile);
203:
204: if (uiDisplayObjectGVL.size() <= 0) {
205: return "No display object found with ID \""
206: + displayObjectId + "\"";
207: } else {
208: resultString += (String.valueOf(uiDisplayObjectGVL
209: .size()) + " display objects<BR>\n");
210: }
211:
212: ////////////////////
213: // Write out the SQL for inserting the display object attributes.
214: ////////////////////
215: // Loop through the display objects. (Should be only one).
216: Iterator displayObjectGVI = uiDisplayObjectGVL.iterator();
217: List uiDisplayObjectAttribGVL = new LinkedList();
218:
219: while (displayObjectGVI.hasNext()) {
220: GenericValue uiDisplayObjectGV = (GenericValue) displayObjectGVI
221: .next();
222:
223: findHashMap = new HashMap();
224: findHashMap.put("displayObjectId", uiDisplayObjectGV
225: .getString("displayObjectId"));
226: findOrder = new ArrayList();
227: findOrder.add("displayObjectId");
228: findOrder.add("displayAttribId");
229: uiDisplayObjectAttribGVL.addAll(writeSqlInserts(
230: "UiDisplayObjectAttrib", findHashMap,
231: findOrder, delegator, outputFile));
232: }
233:
234: resultString += (String.valueOf(uiDisplayObjectAttribGVL
235: .size()) + " display object attributes<BR>\n");
236: displayObjectGVI = null;
237:
238: outputFile.close();
239:
240: return resultString;
241: } catch (IOException e) {
242: Debug
243: .logError(
244: "[UISQLFileWriter.writeUiDisplayObjectSql]: IO Exception - Error was:",
245: module);
246: Debug.logError(e.getMessage(), module);
247:
248: return e.getMessage();
249: } catch (SecurityException se) {
250: Debug
251: .logError(
252: "[UISQLFileWriter.writeUiDisplayObjectSql]: Security Exception - Error was:",
253: module);
254: Debug.logError(se.getMessage(), module);
255:
256: return se.getMessage();
257: }
258: }
259:
260: /**
261: * DOCUMENT ME!
262: *
263: * @param delegator
264: * @param displayTypeId
265: * @param filePath
266: *
267: * @return
268: */
269: public String writeUiDisplayTypeFile(GenericDelegator delegator,
270: String displayTypeId, String filePath) {
271: HashMap findHashMap = new HashMap();
272: ArrayList findOrder = null;
273: ModelEntity modelEntity = null;
274: GenericValue genericValue = null;
275: String entityName = "";
276: String resultString = "";
277:
278: try {
279: // Open the file, and replace any existing contents.
280: FileWriter tempFile = new FileWriter(filePath, false);
281: BufferedWriter outputFile = new BufferedWriter(tempFile);
282:
283: ////////////////////
284: // Write out the SQL for inserting the display type.
285: ////////////////////
286: List uiDisplayTypeGVL = null;
287: findHashMap = new HashMap();
288: findHashMap.put("displayTypeId", displayTypeId);
289: findOrder = new ArrayList();
290: findOrder.add("displayTypeId");
291: uiDisplayTypeGVL = writeSqlInserts("UiDisplayType",
292: findHashMap, findOrder, delegator, outputFile);
293:
294: if (uiDisplayTypeGVL.size() <= 0) {
295: return "No display type found with ID \""
296: + displayTypeId + "\"";
297: } else {
298: resultString += (String
299: .valueOf(uiDisplayTypeGVL.size()) + " display types<BR>\n");
300: }
301:
302: ////////////////////
303: // Write out the SQL for inserting the display attributes.
304: ////////////////////
305: // Loop through the display types. (Should be only one).
306: Iterator displayTypeGVI = uiDisplayTypeGVL.iterator();
307: List uiDisplayAttribGVL = new LinkedList();
308:
309: while (displayTypeGVI.hasNext()) {
310: GenericValue displayTypeGV = (GenericValue) displayTypeGVI
311: .next();
312:
313: findHashMap = new HashMap();
314: findHashMap.put("displayTypeId", displayTypeGV
315: .getString("displayTypeId"));
316: findOrder = new ArrayList();
317: findOrder.add("displayTypeId");
318: findOrder.add("displayAttribId");
319: uiDisplayAttribGVL.addAll(writeSqlInserts(
320: "UiDisplayAttrib", findHashMap, findOrder,
321: delegator, outputFile));
322: }
323:
324: resultString += (String.valueOf(uiDisplayAttribGVL.size()) + " display attributes<BR>\n");
325: displayTypeGVI = null;
326:
327: outputFile.close();
328:
329: return resultString;
330: } catch (IOException e) {
331: Debug
332: .logError(
333: "[UISQLFileWriter.writeUiDisplayTypeSql]: IO Exception - Error was:",
334: module);
335: Debug.logError(e.getMessage(), module);
336:
337: return e.getMessage();
338: } catch (SecurityException se) {
339: Debug
340: .logError(
341: "[UISQLFileWriter.writeUiDisplayTypeSql]: Security Exception - Error was:",
342: module);
343: Debug.logError(se.getMessage(), module);
344:
345: return se.getMessage();
346: }
347: }
348:
349: /**
350: * DOCUMENT ME!
351: *
352: * @param delegator
353: * @param entityId
354: * @param filePath
355: *
356: * @return
357: */
358: public String writeUiEntityFile(GenericDelegator delegator,
359: String entityId, String filePath) {
360: HashMap findHashMap = new HashMap();
361: ArrayList findOrder = null;
362: ModelEntity modelEntity = null;
363: GenericValue genericValue = null;
364: String entityName = "";
365: String resultString = "";
366:
367: try {
368: // Open the file, and replace any existing contents.
369: FileWriter tempFile = new FileWriter(filePath, false);
370: BufferedWriter outputFile = new BufferedWriter(tempFile);
371:
372: ////////////////////
373: // Write out the SQL for inserting the UI entity.
374: ////////////////////
375: List uiEntityGVL = null;
376: findHashMap = new HashMap();
377: findHashMap.put("entityId", entityId);
378: findOrder = new ArrayList();
379: findOrder.add("entityId");
380: uiEntityGVL = writeSqlInserts("UiEntity", findHashMap,
381: findOrder, delegator, outputFile);
382:
383: if (uiEntityGVL.size() <= 0) {
384: return "No entity found with ID \"" + entityId + "\"";
385: } else {
386: resultString += (String.valueOf(uiEntityGVL.size()) + " UI entities<BR>\n");
387: }
388:
389: ////////////////////
390: // Write out the SQL for inserting the UI attributes.
391: ////////////////////
392: // Loop through the UI entities. (Should be only one).
393: Iterator uiEntityGVI = uiEntityGVL.iterator();
394: List uiAttributeGVL = new LinkedList();
395:
396: while (uiEntityGVI.hasNext()) {
397: GenericValue uiEntityGV = (GenericValue) uiEntityGVI
398: .next();
399:
400: findHashMap = new HashMap();
401: findHashMap.put("entityId", uiEntityGV
402: .getString("entityId"));
403: findOrder = new ArrayList();
404: findOrder.add("attributeId");
405: uiAttributeGVL.addAll(writeSqlInserts("UiAttribute",
406: findHashMap, findOrder, delegator, outputFile));
407: }
408:
409: resultString += (String.valueOf(uiAttributeGVL.size()) + " UI attributes<BR>\n");
410: uiEntityGVI = null;
411:
412: outputFile.close();
413:
414: return resultString;
415: } catch (IOException e) {
416: Debug
417: .logError(
418: "[UISQLFileWriter.writeUiEntitySql]: IO Exception - Error was:",
419: module);
420: Debug.logError(e.getMessage(), module);
421:
422: return e.getMessage();
423: } catch (SecurityException se) {
424: Debug
425: .logError(
426: "[UISQLFileWriter.writeUiEntitySql]: Security Exception - Error was:",
427: module);
428: Debug.logError(se.getMessage(), module);
429:
430: return se.getMessage();
431: }
432: }
433:
434: /**
435: * DOCUMENT ME!
436: *
437: * @param delegator
438: * @param codeTypeId
439: * @param filePath
440: *
441: * @return
442: */
443: public String writeCodeTypeFile(GenericDelegator delegator,
444: String codeTypeId, String filePath) {
445: HashMap findHashMap = new HashMap();
446: ArrayList findOrder = null;
447: ModelEntity modelEntity = null;
448: GenericValue genericValue = null;
449: String entityName = "";
450: String resultString = "";
451:
452: try {
453: // Open the file, and replace any existing contents.
454: FileWriter tempFile = new FileWriter(filePath, false);
455: BufferedWriter outputFile = new BufferedWriter(tempFile);
456:
457: ////////////////////
458: // Write out the SQL for inserting the Code Type.
459: ////////////////////
460: List codeTypeGVL = null;
461: findHashMap = new HashMap();
462: findHashMap.put("codeTypeId", codeTypeId);
463: findOrder = new ArrayList();
464: findOrder.add("codeTypeId");
465: codeTypeGVL = writeSqlInserts("CodeType", findHashMap,
466: findOrder, delegator, outputFile);
467:
468: if (codeTypeGVL.size() <= 0) {
469: return "No code type found with ID \"" + codeTypeId
470: + "\"";
471: } else {
472: resultString += (String.valueOf(codeTypeGVL.size()) + " Code Types<BR>\n");
473: }
474:
475: ////////////////////
476: // Write out the SQL for inserting the Codes.
477: ////////////////////
478: // Loop through the UI Code Types. (Should be only one).
479: Iterator codeTypeGVI = codeTypeGVL.iterator();
480: List codeGVL = new LinkedList();
481:
482: while (codeTypeGVI.hasNext()) {
483: GenericValue codeGV = (GenericValue) codeTypeGVI.next();
484:
485: findHashMap = new HashMap();
486: findHashMap.put("codeTypeId", codeGV
487: .getString("codeTypeId"));
488: findOrder = new ArrayList();
489: findOrder.add("codeId");
490: codeGVL.addAll(writeSqlInserts("Code", findHashMap,
491: findOrder, delegator, outputFile));
492: }
493:
494: resultString += (String.valueOf(codeGVL.size()) + " Codes<BR>\n");
495: codeTypeGVI = null;
496:
497: outputFile.close();
498:
499: return resultString;
500: } catch (IOException e) {
501: Debug
502: .logError(
503: "[UISQLFileWriter.writeUiEntitySql]: IO Exception - Error was:",
504: module);
505: Debug.logError(e.getMessage(), module);
506:
507: return e.getMessage();
508: } catch (SecurityException se) {
509: Debug
510: .logError(
511: "[UISQLFileWriter.writeUiEntitySql]: Security Exception - Error was:",
512: module);
513: Debug.logError(se.getMessage(), module);
514:
515: return se.getMessage();
516: }
517: }
518:
519: /**
520: * DOCUMENT ME!
521: *
522: * @param outputFile
523: * @param outputLine
524: */
525: protected void writeLine(BufferedWriter outputFile,
526: String outputLine) {
527: try {
528: outputFile.write(outputLine);
529: outputFile.newLine();
530: outputFile.flush();
531:
532: return;
533: } catch (IOException e) {
534: Debug
535: .logError(
536: "[UISQLFileWriter.writeUiScreenSql]: IO Exception - Error was:",
537: module);
538: Debug.logError(e.getMessage(), module);
539: } catch (SecurityException se) {
540: Debug
541: .logError(
542: "[UISQLFileWriter.writeUiScreenSql]: Security Exception - Error was:",
543: module);
544: Debug.logError(se.getMessage(), module);
545: }
546: }
547:
548: /**
549: * DOCUMENT ME!
550: *
551: * @param entityName
552: * @param findHashMap
553: * @param findOrder
554: * @param delegator
555: * @param outputFile
556: *
557: * @return
558: */
559: public List writeSqlInserts(String entityName, HashMap findHashMap,
560: ArrayList findOrder, GenericDelegator delegator,
561: BufferedWriter outputFile) {
562: String fieldName = "";
563: String fieldValue = "";
564: int fieldCount = 0;
565: String outputString = "";
566: List genericValueList = null;
567:
568: try {
569: ModelEntity modelEntity = delegator
570: .getModelEntity(entityName);
571: genericValueList = delegator.findByLike(entityName,
572: findHashMap, findOrder);
573:
574: Iterator genericValueIterator = genericValueList.iterator();
575:
576: while (genericValueIterator.hasNext()) {
577: writeLine(outputFile, "-- Insert " + entityName);
578:
579: GenericValue genericValue = (GenericValue) genericValueIterator
580: .next();
581: writeLine(outputFile, "INSERT INTO "
582: + ModelUtil.javaNameToDbName(entityName) + " (");
583:
584: Iterator screenFieldIterator = modelEntity
585: .getFieldsIterator();
586: fieldCount = 0;
587:
588: while (screenFieldIterator.hasNext()) {
589: outputString = "";
590: fieldCount++;
591:
592: ModelField screenField = (ModelField) screenFieldIterator
593: .next();
594: fieldName = screenField.getName();
595:
596: if (fieldCount == 1) {
597: outputString += " ";
598: } else {
599: outputString += " ,";
600: }
601:
602: outputString += ModelUtil
603: .javaNameToDbName(fieldName);
604: writeLine(outputFile, outputString);
605: }
606:
607: writeLine(outputFile, ") VALUES (");
608: screenFieldIterator = modelEntity.getFieldsIterator();
609: fieldCount = 0;
610:
611: while (screenFieldIterator.hasNext()) {
612: outputString = "";
613: fieldCount++;
614:
615: ModelField screenField = (ModelField) screenFieldIterator
616: .next();
617: fieldName = screenField.getName();
618: fieldValue = genericValue.getString(fieldName);
619:
620: if (fieldValue == null) {
621: fieldValue = "";
622: }
623:
624: if (fieldCount == 1) {
625: outputString += " ";
626: } else {
627: outputString += " ,";
628: }
629:
630: // Replace all single quotes with two single qoutes.
631: fieldValue = StringHelper.replaceAll(fieldValue,
632: "'", "''");
633:
634: // Put single quotes around everything for now.
635: outputString += ("'" + fieldValue + "'");
636: writeLine(outputFile, outputString);
637: }
638:
639: writeLine(outputFile, ");");
640: writeLine(outputFile, "");
641: }
642:
643: return genericValueList;
644: } catch (GenericEntityException gee) {
645: Debug
646: .logError(
647: "[UISQLFileWriter.writeSqlInserts]: Generic Entity Exception - Error was:",
648: module);
649: Debug.logError(gee.getMessage(), module);
650:
651: return genericValueList;
652: }
653: }
654: }
|