1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.ArrayUtil;
26 import com.liferay.portal.kernel.util.ClassUtil;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.PropertiesUtil;
29 import com.liferay.portal.kernel.util.StringMaker;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.model.ModelHintsUtil;
34 import com.liferay.portal.util.PortalUtil;
35 import com.liferay.util.FileUtil;
36 import com.liferay.util.TextFormatter;
37 import com.liferay.util.Time;
38 import com.liferay.util.log4j.Log4JUtil;
39
40 import com.thoughtworks.qdox.JavaDocBuilder;
41 import com.thoughtworks.qdox.model.JavaClass;
42 import com.thoughtworks.qdox.model.JavaMethod;
43 import com.thoughtworks.qdox.model.JavaParameter;
44 import com.thoughtworks.qdox.model.Type;
45
46 import de.hunsicker.io.FileFormat;
47 import de.hunsicker.jalopy.Jalopy;
48 import de.hunsicker.jalopy.storage.Convention;
49 import de.hunsicker.jalopy.storage.ConventionKeys;
50 import de.hunsicker.jalopy.storage.Environment;
51
52 import java.io.BufferedReader;
53 import java.io.File;
54 import java.io.FileNotFoundException;
55 import java.io.FileReader;
56 import java.io.IOException;
57 import java.io.StringReader;
58
59 import java.util.ArrayList;
60 import java.util.HashMap;
61 import java.util.HashSet;
62 import java.util.Iterator;
63 import java.util.LinkedHashSet;
64 import java.util.List;
65 import java.util.Map;
66 import java.util.Properties;
67 import java.util.Set;
68 import java.util.TreeMap;
69 import java.util.TreeSet;
70
71 import org.dom4j.Document;
72 import org.dom4j.Element;
73 import org.dom4j.io.SAXReader;
74
75
84 public class ServiceBuilder {
85
86 public static void main(String[] args) {
87 ClassLoader classLoader = ClassLoader.getSystemClassLoader();
88
89 Log4JUtil.configureLog4J(
90 classLoader.getResource("META-INF/portal-log4j.xml"));
91 Log4JUtil.configureLog4J(
92 classLoader.getResource("META-INF/portal-log4j-ext.xml"));
93
94 ServiceBuilder serviceBuilder = null;
95
96 if (args.length == 7) {
97 String fileName = args[0];
98 String hbmFileName = args[1];
99 String modelHintsFileName = args[2];
100 String springFileName = args[3];
101 String springDataSourceFileName = "";
102 String apiDir = args[5];
103 String implDir = "src";
104 String jsonFileName = args[6];
105 String sqlDir = "../sql";
106 String sqlFileName = "portal-tables.sql";
107 boolean autoNamespaceTables = false;
108 String baseModelImplPackage = "com.liferay.portal.model.impl";
109 String basePersistencePackage = "com.liferay.portal.service.persistence";
110 String beanLocatorUtilPackage = "com.liferay.portal.kernel.bean";
111 String principalBeanPackage = "com.liferay.portal.service.impl";
112 String propsUtilPackage = "com.liferay.portal.util";
113 String springHibernatePackage = "com.liferay.portal.spring.hibernate";
114 String springUtilPackage = "com.liferay.portal.spring.util";
115
116 serviceBuilder = new ServiceBuilder(
117 fileName, hbmFileName, modelHintsFileName, springFileName,
118 springDataSourceFileName, apiDir, implDir, jsonFileName, sqlDir,
119 sqlFileName, autoNamespaceTables, baseModelImplPackage,
120 basePersistencePackage, beanLocatorUtilPackage,
121 principalBeanPackage, propsUtilPackage, springHibernatePackage,
122 springUtilPackage);
123 }
124 else if (args.length == 0) {
125 String fileName = System.getProperty("service.input.file");
126 String hbmFileName = System.getProperty("service.hbm.file");
127 String modelHintsFileName = System.getProperty("service.model.hints.file");
128 String springFileName = System.getProperty("service.spring.file");
129 String springDataSourceFileName = System.getProperty("service.spring.data.source.file");
130 String apiDir = System.getProperty("service.api.dir");
131 String implDir = System.getProperty("service.impl.dir");
132 String jsonFileName = System.getProperty("service.json.file");
133 String sqlDir = System.getProperty("service.sql.dir");
134 String sqlFileName = System.getProperty("service.sql.file");
135 boolean autoNamespaceTables = GetterUtil.getBoolean(System.getProperty("service.auto.namespace.tables"));
136 String baseModelImplPackage = System.getProperty("service.base.model.impl.package");
137 String basePersistencePackage = System.getProperty("service.base.persistence.package");
138 String beanLocatorUtilPackage = System.getProperty("service.bean.locator.util.package");
139 String principalBeanPackage = System.getProperty("service.principal.bean.package");
140 String propsUtilPackage = System.getProperty("service.props.util.package");
141 String springHibernatePackage = System.getProperty("service.spring.hibernate.package");
142 String springUtilPackage = System.getProperty("service.spring.util.package");
143
144 serviceBuilder = new ServiceBuilder(
145 fileName, hbmFileName, modelHintsFileName, springFileName,
146 springDataSourceFileName, apiDir, implDir, jsonFileName, sqlDir,
147 sqlFileName, autoNamespaceTables, baseModelImplPackage,
148 basePersistencePackage, beanLocatorUtilPackage,
149 principalBeanPackage, propsUtilPackage, springHibernatePackage,
150 springUtilPackage);
151 }
152
153 if (serviceBuilder == null) {
154 System.out.println(
155 "Please set the system properties. Sample properties are:\n" +
156 "\t-Dservice.input.file=${service.file}\n" +
157 "\t-Dservice.hbm.file=classes/META-INF/portal-hbm.xml\n" +
158 "\t-Dservice.model.hints.file=classes/META-INF/portal-model-hints.xml\n" +
159 "\t-Dservice.spring.file=classes/META-INF/portal-spring.xml\n" +
160 "\t-Dservice.api.dir=${project.dir}/portal-service/src\n" +
161 "\t-Dservice.impl.dir=src\n" +
162 "\t-Dservice.json.file=${project.dir}/portal-web/docroot/html/js/liferay/service_unpacked.js\n" +
163 "\t-Dservice.sql.dir=../sql\n" +
164 "\t-Dservice.sql.file=portal-tables.sql\n" +
165 "\t-Dservice.base.model.impl.package=com.liferay.portal.model.impl\n" +
166 "\t-Dservice.base.persistence.package=com.liferay.portal.service.persistence\n" +
167 "\t-Dservice.bean.locator.util.package=com.liferay.portal.kernel.bean\n" +
168 "\t-Dservice.principal.bean.package=com.liferay.portal.service.impl\n" +
169 "\t-Dservice.props.util.package=com.liferay.portal.util\n" +
170 "\t-Dservice.spring.hibernate.package=com.liferay.portal.spring.hibernate\n" +
171 "\t-Dservice.spring.util.package=com.liferay.portal.spring.util");
172 }
173 }
174
175 public static Set getBadCmpFields() {
176 Set badCmpFields = new HashSet();
177
178 badCmpFields.add("access");
179 badCmpFields.add("active");
180 badCmpFields.add("alias");
181 badCmpFields.add("code");
182 badCmpFields.add("data");
183 badCmpFields.add("date");
184 badCmpFields.add("end");
185 badCmpFields.add("idd");
186 badCmpFields.add("featured");
187 badCmpFields.add("fields");
188 badCmpFields.add("from");
189 badCmpFields.add("hidden");
190 badCmpFields.add("id");
191 badCmpFields.add("index");
192 badCmpFields.add("internal");
193 badCmpFields.add("interval");
194 badCmpFields.add("join");
195 badCmpFields.add("key");
196 badCmpFields.add("log");
197 badCmpFields.add("number");
198 badCmpFields.add("password");
199 badCmpFields.add("path");
200 badCmpFields.add("primary");
201 badCmpFields.add("sale");
202 badCmpFields.add("settings");
203 badCmpFields.add("size");
204 badCmpFields.add("start");
205 badCmpFields.add("text");
206 badCmpFields.add("to");
207 badCmpFields.add("type");
208 badCmpFields.add("values");
209
210 return badCmpFields;
211 }
212
213 public static Set getBadTableNames() {
214 Set badTableNames = new HashSet();
215
216 badTableNames = new HashSet();
217 badTableNames.add("Account");
218 badTableNames.add("Action");
219 badTableNames.add("Cache");
220 badTableNames.add("ClassName");
221 badTableNames.add("Contact");
222 badTableNames.add("Group");
223 badTableNames.add("Organization");
224 badTableNames.add("Permission");
225 badTableNames.add("Release");
226 badTableNames.add("Resource");
227 badTableNames.add("Role");
228 badTableNames.add("User");
229
230 return badTableNames;
231 }
232
233 public static void writeFile(File file, String content) throws IOException {
234 writeFile(file, content, null);
235 }
236
237 public static void writeFile(File file, String content, Map jalopySettings)
238 throws IOException {
239
240 File tempFile = new File("ServiceBuilder.temp");
241
242 FileUtil.write(tempFile, content);
243
244
249
250
252 String[] checkImports = new String[] {
253 "com.liferay.portal.PortalException",
254 "com.liferay.portal.kernel.log.Log",
255 "com.liferay.portal.kernel.log.LogFactoryUtil",
256 "com.liferay.portal.kernel.util.BooleanWrapper",
257 "com.liferay.portal.kernel.util.DateUtil",
258 "com.liferay.portal.kernel.util.DoubleWrapper",
259 "com.liferay.portal.kernel.util.FloatWrapper",
260 "com.liferay.portal.kernel.util.GetterUtil",
261 "com.liferay.portal.kernel.util.IntegerWrapper",
262 "com.liferay.portal.kernel.util.LongWrapper",
263 "com.liferay.portal.kernel.util.MethodWrapper",
264 "com.liferay.portal.kernel.util.NullWrapper",
265 "com.liferay.portal.kernel.util.OrderByComparator",
266 "com.liferay.portal.kernel.util.ShortWrapper",
267 "com.liferay.portal.kernel.util.StringMaker",
268 "com.liferay.portal.kernel.util.StringPool",
269 "com.liferay.portal.security.auth.HttpPrincipal",
270 "com.liferay.portal.service.http.TunnelUtil",
271 "com.liferay.portal.spring.hibernate.FinderCache",
272 "com.liferay.portal.spring.hibernate.HibernateUtil",
273 "com.liferay.portal.util.PropsUtil",
274 "com.liferay.util.JSONUtil",
275 "com.liferay.util.XSSUtil",
276 "com.liferay.util.dao.hibernate.QueryPos",
277 "com.liferay.util.dao.hibernate.QueryUtil",
278 "java.io.Serializable",
279 "java.rmi.RemoteException",
280 "java.sql.ResultSet",
281 "java.sql.SQLException",
282 "java.sql.Types",
283 "java.util.Collection",
284 "java.util.Collections",
285 "java.util.Date",
286 "java.util.HashSet",
287 "java.util.Iterator",
288 "java.util.List",
289 "java.util.Properties",
290 "java.util.Set",
291 "javax.sql.DataSource",
292 "org.apache.commons.logging.Log",
293 "org.apache.commons.logging.LogFactory",
294 "org.hibernate.Hibernate",
295 "org.hibernate.ObjectNotFoundException",
296 "org.hibernate.Query",
297 "org.hibernate.SQLQuery",
298 "org.json.JSONArray",
299 "org.json.JSONObject",
300 "org.springframework.dao.DataAccessException",
301 "org.springframework.jdbc.core.SqlParameter",
302 "org.springframework.jdbc.object.MappingSqlQuery",
303 "org.springframework.jdbc.object.SqlUpdate"
304 };
305
306 Set classes = ClassUtil.getClasses(tempFile);
307
308 for (int i = 0; i < checkImports.length; i++) {
309 String importClass = checkImports[i].substring(
310 checkImports[i].lastIndexOf(".") + 1, checkImports[i].length());
311
312 if (!classes.contains(importClass)) {
313 content = StringUtil.replace(
314 content, "import " + checkImports[i] + ";", "");
315 }
316 }
317
318 FileUtil.write(tempFile, content);
319
320
322 StringBuffer sb = new StringBuffer();
323
324 Jalopy jalopy = new Jalopy();
325
326 jalopy.setFileFormat(FileFormat.UNIX);
327 jalopy.setInput(tempFile);
328 jalopy.setOutput(sb);
329
330 try {
331 Jalopy.setConvention("../tools/jalopy.xml");
332 }
333 catch (FileNotFoundException fnne) {
334 }
335
336 try {
337 Jalopy.setConvention("../../misc/jalopy.xml");
338 }
339 catch (FileNotFoundException fnne) {
340 }
341
342 if (jalopySettings == null) {
343 jalopySettings = new HashMap();
344 }
345
346 Environment env = Environment.getInstance();
347
348
350 String author = GetterUtil.getString(
351 (String)jalopySettings.get("author"), "Brian Wing Shun Chan");
352
353 env.set("author", author);
354
355
357 env.set("fileName", file.getName());
358
359 Convention convention = Convention.getInstance();
360
361 String classMask =
362 "/**\n" +
363 " * <a href=\"$fileName$.html\"><b><i>View Source</i></b></a>\n" +
364 " *\n";
365
366 String[] classCommentsArray = (String[])jalopySettings.get("classComments");
367
368 if ((classCommentsArray != null) && (classCommentsArray.length > 0)) {
369 for (int i = 0; i < classCommentsArray.length; i++) {
370 String classComments = classCommentsArray[i];
371
372 classComments = StringUtil.wrap(classComments, 76, "\n * ");
374
375 if (classComments.startsWith("\n")) {
376 classComments = classComments.substring(
377 1, classComments.length());
378 }
379
380 classMask += " * <p>\n" + classComments + "\n * </p>\n *\n";
381 }
382 }
383
384 classMask +=
385 " * @author $author$\n" +
386 " *\n";
387
388 String[] seeArray = (String[])jalopySettings.get("see");
389
390 if ((classCommentsArray != null) && (classCommentsArray.length > 0)) {
391 for (int i = 0; i < seeArray.length; i++) {
392 String see = seeArray[i];
393
394 classMask += " * @see " + see + "\n";
395 }
396
397 classMask += " *\n";
398 }
399
400 classMask += " */";
401
402 convention.put(
403 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_CLASS,
404 env.interpolate(classMask));
405
406 convention.put(
407 ConventionKeys.COMMENT_JAVADOC_TEMPLATE_INTERFACE,
408 env.interpolate(classMask));
409
410 jalopy.format();
411
412 String newContent = sb.toString();
413
414
431
432
434 String oldContent = null;
435
436 if (file.exists()) {
437
438
440 oldContent = FileUtil.read(file);
441
442
444 int x = oldContent.indexOf("@version $Revision:");
445
446 if (x != -1) {
447 int y = oldContent.indexOf("$", x);
448 y = oldContent.indexOf("$", y + 1);
449
450 String oldVersion = oldContent.substring(x, y + 1);
451
452 newContent = StringUtil.replace(
453 newContent, "@version $Rev: $", oldVersion);
454 }
455 }
456 else {
457 newContent = StringUtil.replace(
458 newContent, "@version $Rev: $", "@version $Revision: 1.183 $");
459 }
460
461 if (oldContent == null || !oldContent.equals(newContent)) {
462 FileUtil.write(file, newContent);
463
464 System.out.println("Writing " + file);
465
466
468 file.setLastModified(
469 System.currentTimeMillis() - (Time.SECOND * 5));
470 }
471
472 tempFile.deleteOnExit();
473 }
474
475 public ServiceBuilder(
476 String fileName, String hbmFileName, String modelHintsFileName,
477 String springFileName, String springDataSourceFileName, String apiDir,
478 String implDir, String jsonFileName, String sqlDir, String sqlFileName,
479 boolean autoNamespaceTables, String baseModelImplPackage,
480 String basePersistencePackage, String beanLocatorUtilPackage,
481 String principalBeanPackage, String propsUtilPackage,
482 String springHibernatePackage, String springUtilPackage) {
483
484 new ServiceBuilder(
485 fileName, hbmFileName, modelHintsFileName, springFileName,
486 springDataSourceFileName, apiDir, implDir, jsonFileName, sqlDir,
487 sqlFileName, autoNamespaceTables, baseModelImplPackage,
488 basePersistencePackage, beanLocatorUtilPackage,
489 principalBeanPackage, propsUtilPackage, springHibernatePackage,
490 springUtilPackage, true);
491 }
492
493 public ServiceBuilder(
494 String fileName, String hbmFileName, String modelHintsFileName,
495 String springFileName, String springDataSourceFileName, String apiDir,
496 String implDir, String jsonFileName, String sqlDir, String sqlFileName,
497 boolean autoNamespaceTables, String baseModelImplPackage,
498 String basePersistencePackage, String beanLocatorUtilPackage,
499 String principalBeanPackage, String propsUtilPackage,
500 String springHibernatePackage, String springUtilPackage,
501 boolean build) {
502
503 try {
504 _badTableNames = ServiceBuilder.getBadTableNames();
505 _badCmpFields = ServiceBuilder.getBadCmpFields();
506
507 _hbmFileName = hbmFileName;
508 _modelHintsFileName = modelHintsFileName;
509 _springFileName = springFileName;
510 _springDataSourceFileName = springDataSourceFileName;
511 _apiDir = apiDir;
512 _implDir = implDir;
513 _jsonFileName = jsonFileName;
514 _sqlDir = sqlDir;
515 _sqlFileName = sqlFileName;
516 _autoNamespaceTables = autoNamespaceTables;
517 _baseModelImplPackage = baseModelImplPackage;
518 _basePersistencePackage = basePersistencePackage;
519 _beanLocatorUtilPackage = beanLocatorUtilPackage;
520 _principalBeanPackage = principalBeanPackage;
521 _propsUtilPackage = propsUtilPackage;
522 _springHibernatePackage = springHibernatePackage;
523 _springUtilPackage = springUtilPackage;
524
525 Document doc = PortalUtil.readDocumentFromFile(
526 new File(fileName), true);
527
528 Element root = doc.getRootElement();
529
530 String packagePath = root.attributeValue("package-path");
531
532 _outputPath =
533 _implDir + "/" + StringUtil.replace(packagePath, ".", "/");
534
535 _serviceOutputPath =
536 _apiDir + "/" + StringUtil.replace(packagePath, ".", "/");
537
538 _packagePath = packagePath;
539
540 Element portlet = root.element("portlet");
541 Element namespace = root.element("namespace");
542
543 if (portlet != null) {
544 _portletName = portlet.attributeValue("name");
545
546 _portletShortName = portlet.attributeValue("short-name");
547
548 _portletPackageName =
549 TextFormatter.format(_portletName, TextFormatter.B);
550
551 _outputPath += "/" + _portletPackageName;
552
553 _serviceOutputPath += "/" + _portletPackageName;
554
555 _packagePath += "." + _portletPackageName;
556 }
557 else {
558 _portletShortName = namespace.getText();
559 }
560
561 _portletShortName = _portletShortName.trim();
562
563 if (!Validator.isChar(_portletShortName)) {
564 throw new RuntimeException(
565 "The namespace element must be a valid keyword");
566 }
567
568 _ejbList = new ArrayList();
569
570 List entities = root.elements("entity");
571
572 Iterator itr1 = entities.iterator();
573
574 while (itr1.hasNext()) {
575 Element entityEl = (Element)itr1.next();
576
577 String ejbName = entityEl.attributeValue("name");
578
579 String table = entityEl.attributeValue("table");
580
581 if (Validator.isNull(table)) {
582 table = ejbName;
583
584 if (_badTableNames.contains(ejbName)) {
585 table += "_";
586 }
587 }
588
589 if (_autoNamespaceTables) {
590 table = _portletShortName + "_" + table;
591 }
592
593 boolean localService = GetterUtil.getBoolean(
594 entityEl.attributeValue("local-service"), false);
595 boolean remoteService = GetterUtil.getBoolean(
596 entityEl.attributeValue("remote-service"), true);
597 String persistenceClass = GetterUtil.getString(
598 entityEl.attributeValue("persistence-class"),
599 _packagePath + ".service.persistence."+ ejbName +
600 "PersistenceImpl");
601 String dataSource = entityEl.attributeValue("data-source");
602 String sessionFactory = entityEl.attributeValue(
603 "session-factory");
604 String txManager = entityEl.attributeValue(
605 "tx-manager");
606
607 Iterator itr2 = null;
608
609 List pkList = new ArrayList();
610 List regularColList = new ArrayList();
611 List collectionList = new ArrayList();
612 List columnList = new ArrayList();
613
614 List columns = entityEl.elements("column");
615
616 itr2 = columns.iterator();
617
618 while (itr2.hasNext()) {
619 Element column = (Element)itr2.next();
620
621 String columnName = column.attributeValue("name");
622
623 String columnDBName = column.attributeValue("db-name");
624
625 if (Validator.isNull(columnDBName)) {
626 columnDBName = columnName;
627
628 if (_badCmpFields.contains(columnName)) {
629 columnDBName += "_";
630 }
631 }
632
633 String columnType = column.attributeValue("type");
634 boolean primary = GetterUtil.getBoolean(
635 column.attributeValue("primary"), false);
636 String collectionEntity = column.attributeValue("entity");
637 String mappingKey = column.attributeValue("mapping-key");
638 String mappingTable = column.attributeValue("mapping-table");
639 String idType = column.attributeValue("id-type");
640 String idParam = column.attributeValue("id-param");
641 boolean convertNull = GetterUtil.getBoolean(
642 column.attributeValue("convert-null"), true);
643
644 EntityColumn col = new EntityColumn(
645 columnName, columnDBName, columnType, primary,
646 collectionEntity, mappingKey, mappingTable, idType,
647 idParam, convertNull);
648
649 if (primary) {
650 pkList.add(col);
651 }
652
653 if (columnType.equals("Collection")) {
654 collectionList.add(col);
655 }
656 else {
657 regularColList.add(col);
658 }
659
660 columnList.add(col);
661 }
662
663 EntityOrder order = null;
664
665 Element orderEl = entityEl.element("order");
666
667 if (orderEl != null) {
668 boolean asc = true;
669 if ((orderEl.attribute("by") != null) &&
670 (orderEl.attributeValue("by").equals("desc"))) {
671
672 asc = false;
673 }
674
675 List orderColsList = new ArrayList();
676
677 order = new EntityOrder(asc, orderColsList);
678
679 List orderCols = orderEl.elements("order-column");
680
681 Iterator itr3 = orderCols.iterator();
682
683 while (itr3.hasNext()) {
684 Element orderColEl = (Element)itr3.next();
685
686 String orderColName =
687 orderColEl.attributeValue("name");
688 boolean orderColCaseSensitive = GetterUtil.getBoolean(
689 orderColEl.attributeValue("case-sensitive"),
690 true);
691
692 boolean orderColByAscending = asc;
693 String orderColBy = GetterUtil.getString(
694 orderColEl.attributeValue("order-by"));
695 if (orderColBy.equals("asc")) {
696 orderColByAscending = true;
697 }
698 else if (orderColBy.equals("desc")) {
699 orderColByAscending = false;
700 }
701
702 EntityColumn col = Entity.getColumn(
703 orderColName, columnList);
704
705 col = (EntityColumn)col.clone();
706
707 col.setCaseSensitive(orderColCaseSensitive);
708 col.setOrderByAscending(orderColByAscending);
709
710 orderColsList.add(col);
711 }
712 }
713
714 List finderList = new ArrayList();
715
716 List finders = entityEl.elements("finder");
717
718 itr2 = finders.iterator();
719
720 while (itr2.hasNext()) {
721 Element finderEl = (Element)itr2.next();
722
723 String finderName = finderEl.attributeValue("name");
724 String finderReturn =
725 finderEl.attributeValue("return-type");
726 String finderWhere =
727 finderEl.attributeValue("where");
728 boolean finderDBIndex = GetterUtil.getBoolean(
729 finderEl.attributeValue("db-index"), true);
730
731 List finderColsList = new ArrayList();
732
733 List finderCols = finderEl.elements("finder-column");
734
735 Iterator itr3 = finderCols.iterator();
736
737 while (itr3.hasNext()) {
738 Element finderColEl = (Element)itr3.next();
739
740 String finderColName =
741 finderColEl.attributeValue("name");
742
743 String finderColDBName =
744 finderColEl.attributeValue("db-name");
745
746 if (Validator.isNull(finderColDBName)) {
747 finderColDBName = finderColName;
748
749 if (_badCmpFields.contains(finderColName)) {
750 finderColDBName += "_";
751 }
752 }
753
754 String finderColComparator = GetterUtil.getString(
755 finderColEl.attributeValue("comparator"), "=");
756
757 EntityColumn col = Entity.getColumn(
758 finderColName, columnList);
759
760 col = (EntityColumn)col.clone();
761
762 col.setDBName(finderColDBName);
763 col.setComparator(finderColComparator);
764
765 finderColsList.add(col);
766 }
767
768 finderList.add(new EntityFinder(
769 finderName, finderReturn, finderColsList, finderWhere,
770 finderDBIndex));
771 }
772
773 List txRequiredList = new ArrayList();
774
775 itr2 = entityEl.elements("tx-required").iterator();
776
777 while (itr2.hasNext()) {
778 Element txRequiredEl = (Element)itr2.next();
779
780 String txRequired = txRequiredEl.getText();
781
782 txRequiredList.add(txRequired);
783 }
784
785 _ejbList.add(
786 new Entity(
787 _packagePath, _portletName, _portletShortName, ejbName,
788 table, localService, remoteService, persistenceClass,
789 dataSource, sessionFactory, txManager, pkList,
790 regularColList, collectionList, columnList, order,
791 finderList, txRequiredList));
792 }
793
794 List exceptionList = new ArrayList();
795
796 if (root.element("exceptions") != null) {
797 List exceptions =
798 root.element("exceptions").elements("exception");
799
800 itr1 = exceptions.iterator();
801
802 while (itr1.hasNext()) {
803 Element exception = (Element)itr1.next();
804
805 exceptionList.add(exception.getText());
806 }
807 }
808
809 if (build) {
810 for (int x = 0; x < _ejbList.size(); x++) {
811 Entity entity = (Entity)_ejbList.get(x);
812
813 System.out.println("Building " + entity.getName());
814
815 if (true ||
816 entity.getName().equals("EmailAddress") ||
817 entity.getName().equals("User")) {
818
819 if (entity.hasColumns()) {
820 _createHBM(entity);
821 _createHBMUtil(entity);
822
823 _createPersistenceImpl(entity);
824 _createPersistence(entity);
825 _createPersistenceUtil(entity);
826
827 _createModelImpl(entity);
828 _createExtendedModelImpl(entity);
829
830 _createModel(entity);
831 _createExtendedModel(entity);
832
833 _createModelSoap(entity);
834
835 _createPool(entity);
836
837 if (entity.getPKList().size() > 1) {
838 _createEJBPK(entity);
839 }
840 }
841
842 if (entity.hasLocalService()) {
843 _createServiceBaseImpl(entity);
844 _createServiceImpl(entity, _LOCAL);
845 _createService(entity, _LOCAL);
846 _createServiceFactory(entity, _LOCAL);
847 _createServiceUtil(entity, _LOCAL);
848 }
849
850 if (entity.hasRemoteService()) {
851 _createServiceImpl(entity, _REMOTE);
852 _createService(entity, _REMOTE);
853 _createServiceFactory(entity, _REMOTE);
854 _createServiceUtil(entity, _REMOTE);
855
856 if (Validator.isNotNull(_jsonFileName)) {
857 _createServiceHttp(entity);
858 _createServiceJSON(entity);
859
860 if (entity.hasColumns()) {
861 _createServiceJSONSerializer(entity);
862 }
863
864 _createServiceSoap(entity);
865 }
866 }
867 }
868 }
869
870 _createHBMXML();
871 _createModelHintsXML();
872 _createSpringXML();
873
874 if (Validator.isNotNull(_jsonFileName)) {
875 _createJSONJS();
876 _createRemotingXML();
877 }
878
879 _createSQLIndexes();
880 _createSQLTables();
881 _createSQLSequences();
882
883 _createExceptions(exceptionList);
884
885 _createBaseModelImpl();
886 _createBasePersistence();
887 _createBeanLocatorUtil();
888 _createDynamicDialect();
889 _createFinderCache();
890 _createHibernateConfiguration();
891 _createHibernateUtil();
892 _createPrincipalBean();
893 _createProps();
894 _createPropsUtil();
895 _createSpringDataSourceXML();
896 _createSpringUtil();
897 }
898 }
899 catch (Exception e) {
900 e.printStackTrace();
901 }
902 }
903
904 public Entity getEntity(String name) throws IOException {
905 int pos = name.lastIndexOf(".");
906
907 if (pos == -1) {
908 pos = _ejbList.indexOf(new Entity(name));
909
910 return (Entity)_ejbList.get(pos);
911 }
912 else {
913 String refPackage = name.substring(0, pos);
914 String refPackageDir = StringUtil.replace(refPackage, ".", "/");
915 String refEntity = name.substring(pos + 1, name.length());
916 String refFileName =
917 _implDir + "/" + refPackageDir + "/service.xml";
918
919 File refFile = new File(refFileName);
920
921 boolean useTempFile = false;
922
923 if (!refFile.exists()) {
924 refFileName = Time.getTimestamp();
925 refFile = new File(refFileName);
926
927 ClassLoader classLoader = getClass().getClassLoader();
928
929 FileUtil.write(
930 refFileName,
931 StringUtil.read(
932 classLoader, refPackageDir + "/service.xml"));
933
934 useTempFile = true;
935 }
936
937 ServiceBuilder serviceBuilder = new ServiceBuilder(
938 refFileName, _hbmFileName, _modelHintsFileName, _springFileName,
939 _springDataSourceFileName, _apiDir, _implDir, _jsonFileName,
940 _sqlDir, _sqlFileName, _autoNamespaceTables,
941 _baseModelImplPackage, _basePersistencePackage,
942 _beanLocatorUtilPackage, _principalBeanPackage,
943 _propsUtilPackage, _springHibernatePackage, _springUtilPackage,
944 false);
945
946 Entity entity = serviceBuilder.getEntity(refEntity);
947
948 if (useTempFile) {
949 refFile.deleteOnExit();
950 }
951
952 return entity;
953 }
954 }
955
956 private void _appendNullLogic(EntityColumn col, StringMaker sm) {
957 sm.append("if (" + col.getName() + " == null) {");
958
959 if (col.getComparator().equals("=")) {
960 sm.append("query.append(\"" + col.getDBName() + " IS NULL\");");
961 }
962 else if (col.getComparator().equals("<>") || col.getComparator().equals("!=")) {
963 sm.append("query.append(\"" + col.getDBName() + " IS NOT NULL\");");
964 }
965 else {
966 sm.append("query.append(\"" + col.getDBName() + " " + col.getComparator() + " null\");");
967 }
968
969 sm.append("} else {");
970 }
971
972 private void _createBaseModelImpl() throws IOException {
973 if (_baseModelImplPackage.equals("com.liferay.portal.model.impl")) {
974 return;
975 }
976
977
979 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/BaseModelImpl.java");
980
981 content = StringUtil.replace(
982 content,
983 new String[] {
984 "package com.liferay.portal.model.impl;",
985 "import com.liferay.portal.util.PropsUtil;"
986 },
987 new String[] {
988 "package " + _baseModelImplPackage + ";",
989 "import " + _propsUtilPackage + ".PropsUtil;"
990 });
991
992
994 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_baseModelImplPackage, ".", "/") + "/BaseModelImpl.java");
995
996 FileUtil.write(ejbFile, content, true);
997 }
998
999 private void _createBasePersistence() throws IOException {
1000 if (_basePersistencePackage.equals("com.liferay.portal.service.persistence")) {
1001 return;
1002 }
1003
1004
1006 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/BasePersistence.java");
1007
1008 content = StringUtil.replace(
1009 content,
1010 new String[] {
1011 "package com.liferay.portal.service.persistence;",
1012 "import com.liferay.portal.spring.hibernate."
1013 },
1014 new String[] {
1015 "package " + _basePersistencePackage + ";",
1016 "import " + _springHibernatePackage + "."
1017 });
1018
1019
1021 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_basePersistencePackage, ".", "/") + "/BasePersistence.java");
1022
1023 FileUtil.write(ejbFile, content, true);
1024 }
1025
1026 private void _createBeanLocatorUtil() throws IOException {
1027 if (_beanLocatorUtilPackage.equals("com.liferay.portal.kernel.bean")) {
1028 return;
1029 }
1030
1031 StringMaker sm = new StringMaker();
1032
1033
1035 sm.append("package " + _beanLocatorUtilPackage + ";");
1036
1037
1039 sm.append("import " + _springUtilPackage + " .SpringUtil;");
1040 sm.append("import com.liferay.util.CollectionFactory;");
1041 sm.append("import java.util.Set;");
1042 sm.append("import org.apache.commons.logging.Log;");
1043 sm.append("import org.apache.commons.logging.LogFactory;");
1044 sm.append("import org.springframework.context.ApplicationContext;");
1045
1046
1048 sm.append("public class BeanLocatorUtil {");
1049
1050
1052 sm.append("public static Object locate(String name) {");
1053 sm.append("if (_beans.contains(name)) {");
1054 sm.append("if (_log.isWarnEnabled()) {");
1055 sm.append("_log.warn(\"Cache the reference to \" + name + \" for better performance\");");
1056 sm.append("}");
1057 sm.append("}");
1058 sm.append("ApplicationContext ctx = SpringUtil.getContext();");
1059 sm.append("if (_log.isDebugEnabled()) {");
1060 sm.append("_log.debug(\"Locating \" + name);");
1061 sm.append("}");
1062 sm.append("Object obj = ctx.getBean(name);");
1063 sm.append("_beans.add(name);");
1064 sm.append("return obj;");
1065 sm.append("}");
1066
1067
1069 sm.append("private static Log _log = LogFactory.getLog(BeanLocatorUtil.class);");
1070
1071 sm.append("private static Set _beans = CollectionFactory.getHashSet();");
1072
1073
1075 sm.append("}");
1076
1077
1079 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_beanLocatorUtilPackage, ".", "/") + "/BeanLocatorUtil.java");
1080
1081 writeFile(ejbFile, sm.toString());
1082 }
1083
1084 private void _createDynamicDialect() throws IOException {
1085 if (_springHibernatePackage.equals("com.liferay.portal.spring.hibernate")) {
1086 return;
1087 }
1088
1089
1091 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/DynamicDialect.java");
1092
1093 content = StringUtil.replace(
1094 content,
1095 new String[] {
1096 "package com.liferay.portal.spring.hibernate;"
1097 },
1098 new String[] {
1099 "package " + _springHibernatePackage + ";"
1100 });
1101
1102
1104 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_springHibernatePackage, ".", "/") + "/DynamicDialect.java");
1105
1106 FileUtil.write(ejbFile, content, true);
1107 }
1108
1109 private void _createEJBPK(Entity entity) throws IOException {
1110 List pkList = entity.getPKList();
1111
1112 StringMaker sm = new StringMaker();
1113
1114
1116 sm.append("package " + _packagePath + ".service.persistence;");
1117
1118
1120 sm.append("import com.liferay.portal.kernel.util.DateUtil;");
1121 sm.append("import com.liferay.portal.kernel.util.StringMaker;");
1122 sm.append("import com.liferay.portal.kernel.util.StringPool;");
1123 sm.append("import java.io.Serializable;");
1124 sm.append("import java.util.Date;");
1125
1126
1128 sm.append("public class " + entity.getPKClassName() + " implements Comparable, Serializable {");
1129
1130
1132 for (int i = 0; i < pkList.size(); i++) {
1133 EntityColumn col = (EntityColumn)pkList.get(i);
1134
1135 sm.append("public " + col.getType() + " " + col.getName() + ";");
1136 }
1137
1138
1140 sm.append("public " + entity.getPKClassName() + "() {}");
1141
1142
1144 sm.append("public " + entity.getPKClassName() + "(");
1145
1146 for (int i = 0; i < pkList.size(); i++) {
1147 EntityColumn col = (EntityColumn)pkList.get(i);
1148
1149 sm.append(col.getType() + " " + col.getName());
1150
1151 if ((i + 1) != pkList.size()) {
1152 sm.append(", ");
1153 }
1154 }
1155
1156 sm.append(") {");
1157
1158 for (int i = 0; i < pkList.size(); i++) {
1159 EntityColumn col = (EntityColumn)pkList.get(i);
1160
1161 sm.append("this." + col.getName() + " = " + col.getName() + ";");
1162 }
1163
1164 sm.append("}");
1165
1166
1168 for (int i = 0; i < pkList.size(); i++) {
1169 EntityColumn col = (EntityColumn)pkList.get(i);
1170
1171 if (!col.isCollection()) {
1172 sm.append("public " + col.getType() + " get" + col.getMethodName() + "() {");
1173 sm.append("return " + col.getName() + ";");
1174 sm.append("}");
1175
1176 sm.append("public void set" + col.getMethodName() + "(" + col.getType() + " " + col.getName() + ") {");
1177 sm.append("this." + col.getName() + " = " + col.getName() + ";");
1178 sm.append("}");
1179 }
1180 }
1181
1182
1184 sm.append("public int compareTo(Object obj) {");
1185 sm.append("if (obj == null) {");
1186 sm.append("return -1;");
1187 sm.append("}");
1188 sm.append(entity.getPKClassName() + " pk = (" + entity.getPKClassName() + ")obj;");
1189 sm.append("int value = 0;");
1190
1191 for (int i = 0; i < pkList.size(); i++) {
1192 EntityColumn col = (EntityColumn)pkList.get(i);
1193
1194 String colType = col.getType();
1195
1196 if (!col.isPrimitiveType()) {
1197 if (colType.equals("Date")) {
1198 sm.append("value = DateUtil.compareTo(" + col.getName() + ", pk." + col.getName() + ");");
1199 }
1200 else {
1201 sm.append("value = " + col.getName() + ".compareTo(pk." + col.getName() + ");");
1202 }
1203 }
1204 else {
1205 if (colType.equals("boolean")) {
1206 sm.append("if (!" + col.getName() + " && pk." + col.getName() + ") {");
1207 sm.append("value = -1;");
1208 sm.append("}");
1209 sm.append("else if (" + col.getName() + " && !pk." + col.getName() + ") {");
1210 sm.append("value = 1;");
1211 sm.append("}");
1212 sm.append("else {");
1213 sm.append("value = 0;");
1214 sm.append("}");
1215 }
1216 else {
1217 sm.append("if (" + col.getName() + " < pk." + col.getName() + ") {");
1218 sm.append("value = -1;");
1219 sm.append("}");
1220 sm.append("else if (" + col.getName() + " > pk." + col.getName() + ") {");
1221 sm.append("value = 1;");
1222 sm.append("}");
1223 sm.append("else {");
1224 sm.append("value = 0;");
1225 sm.append("}");
1226 }
1227 }
1228
1229 sm.append("if (value != 0) {");
1230 sm.append("return value;");
1231 sm.append("}");
1232 }
1233
1234 sm.append("return 0;");
1235 sm.append("}");
1236
1237
1239 sm.append("public boolean equals(Object obj) {");
1240 sm.append("if (obj == null) {");
1241 sm.append("return false;");
1242 sm.append("}");
1243 sm.append(entity.getPKClassName() + " pk = null;");
1244 sm.append("try {");
1245 sm.append("pk = (" + entity.getPKClassName() + ")obj;");
1246 sm.append("}");
1247 sm.append("catch (ClassCastException cce) {");
1248 sm.append("return false;");
1249 sm.append("}");
1250 sm.append("if (");
1251
1252 for (int i = 0; i < pkList.size(); i++) {
1253 EntityColumn col = (EntityColumn)pkList.get(i);
1254
1255 if (!col.isPrimitiveType()) {
1256 sm.append("(" + col.getName() + ".equals(pk." + col.getName() + "))");
1257 }
1258 else {
1259 sm.append("(" + col.getName() + " == pk." + col.getName() + ")");
1260 }
1261
1262 if ((i + 1) != pkList.size()) {
1263 sm.append(" && ");
1264 }
1265 }
1266
1267 sm.append(") {");
1268 sm.append("return true;");
1269 sm.append("} else {");
1270 sm.append("return false;");
1271 sm.append("}");
1272 sm.append("}");
1273
1274
1276 sm.append("public int hashCode() {");
1277 sm.append("return (");
1278
1279 for (int i = 0; i < pkList.size(); i++) {
1280 EntityColumn col = (EntityColumn)pkList.get(i);
1281
1282 if (i != 0) {
1283 sm.append(" + ");
1284 }
1285
1286 if (!col.isPrimitiveType() && !col.getType().equals("String")) {
1287 sm.append(col.getName() + ".toString()");
1288 }
1289 else {
1290 sm.append("String.valueOf(" + col.getName() + ")");
1291 }
1292 }
1293
1294 sm.append(").hashCode();");
1295 sm.append("}");
1296
1297
1299 sm.append("public String toString() {");
1300 sm.append("StringMaker sm = new StringMaker();");
1301 sm.append("sm.append(StringPool.OPEN_CURLY_BRACE);");
1302
1303 for (int i = 0; i < pkList.size(); i++) {
1304 EntityColumn col = (EntityColumn)pkList.get(i);
1305
1306 sm.append("sm.append(\"" + col.getName() + "\");");
1307 sm.append("sm.append(StringPool.EQUAL);");
1308 sm.append("sm.append(" + col.getName() + ");");
1309
1310 if ((i + 1) != pkList.size()) {
1311 sm.append("sm.append(StringPool.COMMA);");
1312 sm.append("sm.append(StringPool.SPACE);");
1313 }
1314 }
1315
1316 sm.append("sm.append(StringPool.CLOSE_CURLY_BRACE);");
1317 sm.append("return sm.toString();");
1318 sm.append("}");
1319
1320
1322 sm.append("}");
1323
1324
1326 File ejbFile = new File(_serviceOutputPath + "/service/persistence/" + entity.getPKClassName() + ".java");
1327
1328 writeFile(ejbFile, sm.toString());
1329
1330
1337 }
1338
1339 private void _createExceptions(List exceptions) throws IOException {
1340 String copyright = null;
1341 try {
1342 copyright = FileUtil.read("../copyright.txt");
1343 }
1344 catch (FileNotFoundException fnfe) {
1345 }
1346
1347 for (int i = 0; i < _ejbList.size(); i++) {
1348 Entity entity = (Entity)_ejbList.get(i);
1349
1350 if (entity.hasColumns()) {
1351 exceptions.add(_getNoSuchEntityException(entity));
1352 }
1353 }
1354
1355 for (int i = 0; i < exceptions.size(); i++) {
1356 String exception = (String)exceptions.get(i);
1357
1358 StringMaker sm = new StringMaker();
1359
1360 if (Validator.isNotNull(copyright)) {
1361 sm.append(copyright + "\n");
1362 sm.append("\n");
1363 }
1364
1365 sm.append("package " + _packagePath + ";\n");
1366 sm.append("\n");
1367 sm.append("import com.liferay.portal.PortalException;\n");
1368 sm.append("\n");
1369
1370 if (Validator.isNotNull(copyright)) {
1371 sm.append("/**\n");
1372 sm.append(" * <a href=\"" + exception + "Exception.java.html\"><b><i>View Source</i></b></a>\n");
1373 sm.append(" *\n");
1374 sm.append(" * @author Brian Wing Shun Chan\n");
1375 sm.append(" *\n");
1376 sm.append(" */\n");
1377 }
1378
1379 sm.append("public class " + exception + "Exception extends PortalException {\n");
1380 sm.append("\n");
1381 sm.append("\tpublic " + exception + "Exception() {\n");
1382 sm.append("\t\tsuper();\n");
1383 sm.append("\t}\n");
1384 sm.append("\n");
1385 sm.append("\tpublic " + exception + "Exception(String msg) {\n");
1386 sm.append("\t\tsuper(msg);\n");
1387 sm.append("\t}\n");
1388 sm.append("\n");
1389 sm.append("\tpublic " + exception + "Exception(String msg, Throwable cause) {\n");
1390 sm.append("\t\tsuper(msg, cause);\n");
1391 sm.append("\t}\n");
1392 sm.append("\n");
1393 sm.append("\tpublic " + exception + "Exception(Throwable cause) {\n");
1394 sm.append("\t\tsuper(cause);\n");
1395 sm.append("\t}\n");
1396 sm.append("\n");
1397 sm.append("}");
1398
1399 File exceptionFile = new File(_serviceOutputPath + "/" + exception + "Exception.java");
1400
1401 if (!exceptionFile.exists()) {
1402 FileUtil.write(exceptionFile, sm.toString());
1403 }
1404
1405
1412 }
1413 }
1414
1415 private void _createExtendedModel(Entity entity) throws IOException {
1416 JavaClass javaClass = _getJavaClass(_outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1417
1418 JavaMethod[] methods = javaClass.getMethods();
1419
1420 StringMaker sm = new StringMaker();
1421
1422
1424 sm.append("package " + _packagePath + ".model;");
1425
1426
1428 sm.append("public interface " + entity.getName() + " extends " + entity.getName() + "Model {");
1429
1430
1432 for (int i = 0; i < methods.length; i++) {
1433 JavaMethod javaMethod = methods[i];
1434
1435 String methodName = javaMethod.getName();
1436
1437 if (!javaMethod.isConstructor() && !javaMethod.isStatic() && javaMethod.isPublic()) {
1438 sm.append("public " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
1439
1440 JavaParameter[] parameters = javaMethod.getParameters();
1441
1442 for (int j = 0; j < parameters.length; j++) {
1443 JavaParameter javaParameter = parameters[j];
1444
1445 sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
1446
1447 if ((j + 1) != parameters.length) {
1448 sm.append(", ");
1449 }
1450 }
1451
1452 sm.append(")");
1453
1454 Type[] thrownExceptions = javaMethod.getExceptions();
1455
1456 Set newExceptions = new LinkedHashSet();
1457
1458 for (int j = 0; j < thrownExceptions.length; j++) {
1459 Type thrownException = thrownExceptions[j];
1460
1461 newExceptions.add(thrownException.getValue());
1462 }
1463
1464 if (newExceptions.size() > 0) {
1465 sm.append(" throws ");
1466
1467 Iterator itr = newExceptions.iterator();
1468
1469 while (itr.hasNext()) {
1470 sm.append(itr.next());
1471
1472 if (itr.hasNext()) {
1473 sm.append(", ");
1474 }
1475 }
1476 }
1477
1478 sm.append(";");
1479 }
1480 }
1481
1482
1484 sm.append("}");
1485
1486
1488 File modelFile = new File(_serviceOutputPath + "/model/" + entity.getName() + ".java");
1489
1490 Map jalopySettings = new HashMap();
1491
1492 String[] classComments = {
1493 _DEFAULT_CLASS_COMMENTS,
1494 "This interface is a model that represents the <code>" + entity.getTable() + "</code> table in the database.",
1495 "Customize <code>" + _packagePath + ".service.model.impl." + entity.getName() + "Impl</code> and rerun the ServiceBuilder to generate the new methods."
1496 };
1497
1498 String[] see = {
1499 _packagePath + ".service.model." + entity.getName() + "Model",
1500 _packagePath + ".service.model.impl." + entity.getName() + "Impl",
1501 _packagePath + ".service.model.impl." + entity.getName() + "ModelImpl"
1502 };
1503
1504 jalopySettings.put("classComments", classComments);
1505 jalopySettings.put("see", see);
1506
1507 writeFile(modelFile, sm.toString(), jalopySettings);
1508
1509
1516 }
1517
1518 private void _createExtendedModelImpl(Entity entity) throws IOException {
1519 StringMaker sm = new StringMaker();
1520
1521
1523 sm.append("package " + _packagePath + ".model.impl;");
1524
1525
1527 sm.append("import " + _packagePath + ".model." + entity.getName() + ";");
1528
1529
1531 sm.append("public class " + entity.getName() + "Impl extends " + entity.getName() + "ModelImpl implements " + entity.getName() + " {");
1532
1533
1535 sm.append("public " + entity.getName() + "Impl() {");
1536 sm.append("}");
1537
1538
1540 sm.append("}");
1541
1542
1544 File modelFile = new File(_outputPath + "/model/impl/" + entity.getName() + "Impl.java");
1545
1546 if (!modelFile.exists()) {
1547 writeFile(modelFile, sm.toString());
1548 }
1549 }
1550
1551 private void _createFinderCache() throws IOException {
1552 if (_springHibernatePackage.equals("com.liferay.portal.spring.hibernate")) {
1553 return;
1554 }
1555
1556
1558 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/FinderCache.java");
1559
1560 content = StringUtil.replace(
1561 content,
1562 new String[] {
1563 "package com.liferay.portal.spring.hibernate;",
1564 "import com.liferay.portal.util.PropsUtil;"
1565 },
1566 new String[] {
1567 "package " + _springHibernatePackage + ";",
1568 "import " + _propsUtilPackage + ".PropsUtil;"
1569 });
1570
1571
1573 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_springHibernatePackage, ".", "/") + "/FinderCache.java");
1574
1575 FileUtil.write(ejbFile, content, true);
1576 }
1577
1578 private void _createHBM(Entity entity) throws IOException {
1579 File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "HBM.java");
1580
1581 if (ejbFile.exists()) {
1582 System.out.println("Removing deprecated " + ejbFile);
1583
1584 ejbFile.delete();
1585 }
1586 }
1587
1588 private void _createHBMUtil(Entity entity) throws IOException {
1589 File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "HBMUtil.java");
1590
1591 if (ejbFile.exists()) {
1592 System.out.println("Removing deprecated " + ejbFile);
1593
1594 ejbFile.delete();
1595 }
1596 }
1597
1598 private void _createHBMXML() throws IOException {
1599 StringMaker sm = new StringMaker();
1600
1601 for (int i = 0; i < _ejbList.size(); i++) {
1602 Entity entity = (Entity)_ejbList.get(i);
1603
1604 List pkList = entity.getPKList();
1605 List columnList = entity.getColumnList();
1606
1607 if (entity.hasColumns()) {
1608 sm.append("\t<class name=\"" + _packagePath + ".model.impl." + entity.getName() + "Impl\" table=\"" + entity.getTable() + "\">\n");
1609 sm.append("\t\t<cache usage=\"read-write\" />\n");
1610
1611 if (entity.hasCompoundPK()) {
1612 sm.append("\t\t<composite-id name=\"primaryKey\" class=\"" + _packagePath + ".service.persistence." + entity.getName() + "PK\">\n");
1613
1614 for (int j = 0; j < pkList.size(); j++) {
1615 EntityColumn col = (EntityColumn)pkList.get(j);
1616
1617 sm.append("\t\t\t<key-property name=\"" + col.getName() + "\" ");
1618
1619 if (!col.getName().equals(col.getDBName())) {
1620 sm.append("column=\"" + col.getDBName() + "\" />\n");
1621 }
1622 else {
1623 sm.append("/>\n");
1624 }
1625 }
1626
1627 sm.append("\t\t</composite-id>\n");
1628 }
1629 else {
1630 EntityColumn col = (EntityColumn)pkList.get(0);
1631
1632 sm.append("\t\t<id name=\"" + col.getName() + "\" ");
1633
1634 if (!col.getName().equals(col.getDBName())) {
1635 sm.append("column=\"" + col.getDBName() + "\" ");
1636 }
1637
1638 sm.append("type=\"");
1639
1640 if (!entity.hasPrimitivePK()) {
1641 sm.append("java.lang.");
1642 }
1643
1644 sm.append(col.getType() + "\">\n");
1645
1646 String colIdType = col.getIdType();
1647
1648 if (Validator.isNull(colIdType)) {
1649 sm.append("\t\t\t<generator class=\"assigned\" />\n");
1650 }
1651 else if (colIdType.equals("class")) {
1652 sm.append("\t\t\t<generator class=\"" + col.getIdParam() + "\" />\n");
1653 }
1654 else if (colIdType.equals("sequence")) {
1655 sm.append("\t\t\t<generator class=\"sequence\">\n");
1656 sm.append("\t\t\t\t<param name=\"sequence\">" + col.getIdParam() + "</param>\n");
1657 sm.append("\t\t\t</generator>\n");
1658 }
1659 else {
1660 sm.append("\t\t\t<generator class=\"" + colIdType + "\" />\n");
1661 }
1662
1663 sm.append("\t\t</id>\n");
1664 }
1665
1666 for (int j = 0; j < columnList.size(); j++) {
1667 EntityColumn col = (EntityColumn)columnList.get(j);
1668
1669 String colType = col.getType();
1670
1671 if (!col.isPrimary() && !col.isCollection() && col.getEJBName() == null) {
1672 sm.append("\t\t<property name=\"" + col.getName() + "\" ");
1673
1674 if (!col.getName().equals(col.getDBName())) {
1675 sm.append("column=\"" + col.getDBName() + "\" ");
1676 }
1677
1678 if (col.isPrimitiveType() || colType.equals("String")) {
1679 sm.append("type=\"com.liferay.util.dao.hibernate.");
1680 sm.append(_getPrimitiveObj(colType));
1681 sm.append("Type\" ");
1682 }
1683
1684 sm.append("/>\n");
1685 }
1686 }
1687
1688 sm.append("\t</class>\n");
1689 }
1690 }
1691
1692 File xmlFile = new File(_hbmFileName);
1693
1694 if (!xmlFile.exists()) {
1695 String content =
1696 "<?xml version=\"1.0\"?>\n" +
1697 "<!DOCTYPE hibernate-mapping PUBLIC \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\n" +
1698 "\n" +
1699 "<hibernate-mapping default-lazy=\"false\" auto-import=\"false\">\n" +
1700 "</hibernate-mapping>";
1701
1702 FileUtil.write(xmlFile, content);
1703 }
1704
1705 String oldContent = FileUtil.read(xmlFile);
1706 String newContent = _fixHBMXML(oldContent);
1707
1708 int firstClass = newContent.indexOf(
1709 "<class name=\"" + _packagePath + ".model.impl.");
1710 int lastClass = newContent.lastIndexOf(
1711 "<class name=\"" + _packagePath + ".model.impl.");
1712
1713 if (firstClass == -1) {
1714 int x = newContent.indexOf("</hibernate-mapping>");
1715
1716 newContent =
1717 newContent.substring(0, x) + sm.toString() +
1718 newContent.substring(x, newContent.length());
1719 }
1720 else {
1721 firstClass = newContent.lastIndexOf(
1722 "<class", firstClass) - 1;
1723 lastClass = newContent.indexOf(
1724 "</class>", lastClass) + 9;
1725
1726 newContent =
1727 newContent.substring(0, firstClass) + sm.toString() +
1728 newContent.substring(lastClass, newContent.length());
1729 }
1730
1731 if (!oldContent.equals(newContent)) {
1732 FileUtil.write(xmlFile, newContent);
1733 }
1734 }
1735
1736 private void _createHibernateConfiguration() throws IOException {
1737 if (_springHibernatePackage.equals("com.liferay.portal.spring.hibernate")) {
1738 return;
1739 }
1740
1741
1743 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/HibernateConfiguration.java");
1744
1745 content = StringUtil.replace(
1746 content,
1747 new String[] {
1748 "package com.liferay.portal.spring.hibernate;",
1749 "import com.liferay.portal.util.PropsUtil;",
1750 "extends TransactionAwareConfiguration"
1751 },
1752 new String[] {
1753 "package " + _springHibernatePackage + ";",
1754 "import " + _propsUtilPackage + ".PropsUtil;",
1755 "extends org.springframework.orm.hibernate3.LocalSessionFactoryBean"
1756 });
1757
1758
1760 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_springHibernatePackage, ".", "/") + "/HibernateConfiguration.java");
1761
1762 FileUtil.write(ejbFile, content, true);
1763 }
1764
1765 private void _createHibernateUtil() throws IOException {
1766 if (_springHibernatePackage.equals("com.liferay.portal.spring.hibernate")) {
1767 return;
1768 }
1769
1770
1772 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/HibernateUtil.java");
1773
1774 content = StringUtil.replace(
1775 content,
1776 new String[] {
1777 "package com.liferay.portal.spring.hibernate;",
1778 "import com.liferay.portal.util.PropsUtil;"
1779 },
1780 new String[] {
1781 "package " + _springHibernatePackage + ";",
1782 "import " + _propsUtilPackage + ".PropsUtil;"
1783 });
1784
1785
1787 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_springHibernatePackage, ".", "/") + "/HibernateUtil.java");
1788
1789 FileUtil.write(ejbFile, content, true);
1790 }
1791
1792 private void _createJSONJS() throws IOException {
1793 StringMaker sm = new StringMaker();
1794
1795 if (_ejbList.size() > 0) {
1796 sm.append("Liferay.Service." + _portletShortName + " = {\n");
1797 sm.append("\tservicePackage: \"" + _packagePath + ".service.http.\"\n");
1798 sm.append("};\n\n");
1799 }
1800
1801 for (int i = 0; i < _ejbList.size(); i++) {
1802 Entity entity = (Entity)_ejbList.get(i);
1803
1804 if (entity.hasRemoteService()) {
1805 JavaClass javaClass = _getJavaClass(_outputPath + "/service/http/" + entity.getName() + "ServiceJSON.java");
1806
1807 JavaMethod[] methods = javaClass.getMethods();
1808
1809 Set jsonMethods = new LinkedHashSet();
1810
1811 for (int j = 0; j < methods.length; j++) {
1812 JavaMethod javaMethod = methods[j];
1813
1814 String methodName = javaMethod.getName();
1815
1816 if (javaMethod.isPublic()) {
1817 jsonMethods.add(methodName);
1818 }
1819 }
1820
1821 if (jsonMethods.size() > 0) {
1822 sm.append("Liferay.Service." + _portletShortName + "." + entity.getName() + " = {\n");
1823 sm.append("\tserviceClassName: Liferay.Service." + _portletShortName + ".servicePackage + \"" + entity.getName() + "\" + Liferay.Service.classNameSuffix,\n\n");
1824
1825 Iterator itr = jsonMethods.iterator();
1826
1827 while (itr.hasNext()) {
1828 String methodName = (String)itr.next();
1829
1830 sm.append("\t" + methodName + ": function(params, callback) {\n");
1831 sm.append("\t\tparams.serviceClassName = this.serviceClassName;\n");
1832 sm.append("\t\tparams.serviceMethodName = \"" + methodName + "\";\n\n");
1833 sm.append("\t\treturn Liferay.Service.ajax(params, callback);\n");
1834 sm.append("\t}");
1835
1836 if (itr.hasNext()) {
1837 sm.append(",\n");
1838 }
1839
1840 sm.append("\n");
1841 }
1842
1843 sm.append("};\n\n");
1844 }
1845 }
1846 }
1847
1848 File xmlFile = new File(_jsonFileName);
1849
1850 if (!xmlFile.exists()) {
1851 String content = "";
1852
1853 FileUtil.write(xmlFile, content);
1854 }
1855
1856 String oldContent = FileUtil.read(xmlFile);
1857 String newContent = new String(oldContent);
1858
1859 int oldBegin = oldContent.indexOf(
1860 "Liferay.Service." + _portletShortName);
1861
1862 int oldEnd = oldContent.lastIndexOf(
1863 "Liferay.Service." + _portletShortName);
1864 oldEnd = oldContent.indexOf("};", oldEnd);
1865
1866 int newBegin = newContent.indexOf(
1867 "Liferay.Service." + _portletShortName);
1868
1869 int newEnd = newContent.lastIndexOf(
1870 "Liferay.Service." + _portletShortName);
1871 newEnd = newContent.indexOf("};", newEnd);
1872
1873 if (newBegin == -1) {
1874 newContent = oldContent + "\n\n" + sm.toString().trim();
1875 }
1876 else {
1877 newContent =
1878 newContent.substring(0, oldBegin) + sm.toString().trim() +
1879 newContent.substring(oldEnd + 2, newContent.length());
1880 }
1881
1882 if (!oldContent.equals(newContent)) {
1883 FileUtil.write(xmlFile, newContent);
1884 }
1885 }
1886
1887 private void _createModel(Entity entity) throws IOException {
1888 List regularColList = entity.getRegularColList();
1889
1890 StringMaker sm = new StringMaker();
1891
1892
1894 sm.append("package " + _packagePath + ".model;");
1895
1896
1898 if (entity.hasCompoundPK()) {
1899 sm.append("import " + _packagePath + ".service.persistence." + entity.getName() + "PK;");
1900 }
1901
1902 sm.append("import com.liferay.portal.model.BaseModel;");
1903 sm.append("import java.util.Date;");
1904
1905
1907 sm.append("public interface " + entity.getName() + "Model extends BaseModel {");
1908
1909
1911 sm.append("public " + entity.getPKClassName() + " getPrimaryKey();");
1912
1913 sm.append("public void setPrimaryKey(" + entity.getPKClassName() + " pk);");
1914
1915
1917 for (int i = 0; i < regularColList.size(); i++) {
1918 EntityColumn col = (EntityColumn)regularColList.get(i);
1919
1920 String colType = col.getType();
1921
1922 sm.append("public " + colType + " get" + col.getMethodName() + "();");
1923
1924 if (colType.equals("boolean")) {
1925 sm.append("public " + colType + " is" + col.getMethodName() + "();");
1926 }
1927
1928 sm.append("public void set" + col.getMethodName() + "(" + colType + " " + col.getName() + ");");
1929 }
1930
1931
1933 sm.append("}");
1934
1935
1937 File modelFile = new File(_serviceOutputPath + "/model/" + entity.getName() + "Model.java");
1938
1939 Map jalopySettings = new HashMap();
1940
1941 String[] classComments = {
1942 _DEFAULT_CLASS_COMMENTS,
1943 "This interface is a model that represents the <code>" + entity.getTable() + "</code> table in the database."
1944 };
1945
1946 String[] see = {
1947 _packagePath + ".service.model." + entity.getName(),
1948 _packagePath + ".service.model.impl." + entity.getName() + "Impl",
1949 _packagePath + ".service.model.impl." + entity.getName() + "ModelImpl"
1950 };
1951
1952 jalopySettings.put("classComments", classComments);
1953 jalopySettings.put("see", see);
1954
1955 writeFile(modelFile, sm.toString(), jalopySettings);
1956
1957
1964 }
1965
1966 private void _createModelHintsXML() throws IOException {
1967 StringMaker sm = new StringMaker();
1968
1969 for (int i = 0; i < _ejbList.size(); i++) {
1970 Entity entity = (Entity)_ejbList.get(i);
1971
1972 List columnList = entity.getColumnList();
1973
1974 if (entity.hasColumns()) {
1975 sm.append("\t<model name=\"" + _packagePath + ".model." + entity.getName() + "\">\n");
1976
1977 Map defaultHints = ModelHintsUtil.getDefaultHints(_packagePath + ".model." + entity.getName());
1978
1979 if ((defaultHints != null) && (defaultHints.size() > 0)) {
1980 sm.append("\t\t<default-hints>\n");
1981
1982 Iterator itr = defaultHints.entrySet().iterator();
1983
1984 while (itr.hasNext()) {
1985 Map.Entry entry = (Map.Entry)itr.next();
1986
1987 String key = (String)entry.getKey();
1988 String value = (String)entry.getValue();
1989
1990 sm.append("\t\t\t<hint name=\"" + key + "\">" + value + "</hint>\n");
1991 }
1992
1993 sm.append("\t\t</default-hints>\n");
1994 }
1995
1996 for (int j = 0; j < columnList.size(); j++) {
1997 EntityColumn col = (EntityColumn)columnList.get(j);
1998
1999 if (!col.isCollection()) {
2000 sm.append("\t\t<field name=\"" + col.getName() + "\" type=\"" + col.getType() + "\"");
2001
2002 Element field = ModelHintsUtil.getFieldsEl(_packagePath + ".model." + entity.getName(), col.getName());
2003
2004 List hints = null;
2005
2006 if (field != null) {
2007 hints = field.elements();
2008 }
2009
2010 if ((hints == null) || (hints.size() == 0)) {
2011 sm.append(" />\n");
2012 }
2013 else {
2014 sm.append(">\n");
2015
2016 Iterator itr = hints.iterator();
2017
2018 while (itr.hasNext()) {
2019 Element hint = (Element)itr.next();
2020
2021 if (hint.getName().equals("hint")) {
2022 sm.append("\t\t\t<hint name=\"" + hint.attributeValue("name") + "\">" + hint.getText() + "</hint>\n");
2023 }
2024 else {
2025 sm.append("\t\t\t<hint-collection name=\"" + hint.attributeValue("name") + "\" />\n");
2026 }
2027 }
2028
2029 sm.append("\t\t</field>\n");
2030 }
2031 }
2032 }
2033
2034 sm.append("\t</model>\n");
2035 }
2036 }
2037
2038 File xmlFile = new File(_modelHintsFileName);
2039
2040 if (!xmlFile.exists()) {
2041 String content =
2042 "<?xml version=\"1.0\"?>\n" +
2043 "\n" +
2044 "<model-hints>\n" +
2045 "</model-hints>";
2046
2047 FileUtil.write(xmlFile, content);
2048 }
2049
2050 String oldContent = FileUtil.read(xmlFile);
2051 String newContent = new String(oldContent);
2052
2053 int firstModel = newContent.indexOf(
2054 "<model name=\"" + _packagePath + ".model.");
2055 int lastModel = newContent.lastIndexOf(
2056 "<model name=\"" + _packagePath + ".model.");
2057
2058 if (firstModel == -1) {
2059 int x = newContent.indexOf("</model-hints>");
2060 newContent =
2061 newContent.substring(0, x) + sm.toString() +
2062 newContent.substring(x, newContent.length());
2063 }
2064 else {
2065 firstModel = newContent.lastIndexOf(
2066 "<model", firstModel) - 1;
2067 lastModel = newContent.indexOf(
2068 "</model>", lastModel) + 9;
2069
2070 newContent =
2071 newContent.substring(0, firstModel) + sm.toString() +
2072 newContent.substring(lastModel, newContent.length());
2073 }
2074
2075 if (!oldContent.equals(newContent)) {
2076 FileUtil.write(xmlFile, newContent);
2077 }
2078 }
2079
2080 private void _createModelImpl(Entity entity) throws IOException {
2081 List pkList = entity.getPKList();
2082 List regularColList = entity.getRegularColList();
2083
2084 StringMaker sm = new StringMaker();
2085
2086
2088 sm.append("package " + _packagePath + ".model.impl;");
2089
2090
2092 if (entity.hasCompoundPK()) {
2093 sm.append("import " + _packagePath + ".service.persistence." + entity.getName() + "PK;");
2094 }
2095
2096 sm.append("import " + _baseModelImplPackage + ".BaseModelImpl;");
2097 sm.append("import " + _propsUtilPackage + ".PropsUtil;");
2098 sm.append("import com.liferay.portal.kernel.util.DateUtil;");
2099 sm.append("import com.liferay.portal.kernel.util.GetterUtil;");
2100 sm.append("import com.liferay.util.XSSUtil;");
2101 sm.append("import java.io.Serializable;");
2102 sm.append("import java.sql.Types;");
2103 sm.append("import java.util.Date;");
2104
2105
2107 sm.append("public class " + entity.getName() + "ModelImpl extends BaseModelImpl {");
2108
2109
2111 sm.append("public static String TABLE_NAME = \"" + entity.getTable() + "\";");
2112
2113 sm.append("public static Object[][] TABLE_COLUMNS = {");
2114
2115 for (int i = 0; i < regularColList.size(); i++) {
2116 EntityColumn col = (EntityColumn)regularColList.get(i);
2117
2118 String sqlType = _getSqlType(_packagePath + ".model." + entity.getName(), col.getName(), col.getType());
2119
2120 sm.append("{\"" + col.getDBName() + "\", new Integer(Types." + sqlType + ")}");
2121
2122 if ((i + 1) < regularColList.size()) {
2123 sm.append(",");
2124 }
2125 }
2126
2127 sm.append("};");
2128
2129 String createTableSQL = _getCreateTableSQL(entity);
2130
2131 createTableSQL = StringUtil.replace(createTableSQL, "\n", "");
2132 createTableSQL = StringUtil.replace(createTableSQL, "\t", "");
2133 createTableSQL = createTableSQL.substring(0, createTableSQL.length() - 1);
2134
2135 sm.append("public static String TABLE_SQL_CREATE = \"" + createTableSQL + "\";");
2136
2137 sm.append("public static String TABLE_SQL_DROP = \"drop table " + entity.getTable() + "\";");
2138
2139 sm.append("public static boolean XSS_ALLOW_BY_MODEL = GetterUtil.getBoolean(PropsUtil.get(\"xss.allow." + _packagePath + ".model." + entity.getName() + "\"), XSS_ALLOW);");
2140
2141 for (int i = 0; i < regularColList.size(); i++) {
2142 EntityColumn col = (EntityColumn)regularColList.get(i);
2143
2144 if (col.getType().equals("String")) {
2145 sm.append("public static boolean XSS_ALLOW_" + col.getName().toUpperCase() + " = GetterUtil.getBoolean(PropsUtil.get(\"xss.allow." + _packagePath + ".model." + entity.getName() + "." + col.getName() + "\"), XSS_ALLOW_BY_MODEL);");
2146 }
2147 }
2148
2149 sm.append("public static long LOCK_EXPIRATION_TIME = GetterUtil.getLong(PropsUtil.get(\"lock.expiration.time." + _packagePath + ".model." + entity.getName() + "Model\"));");
2150
2151
2153 sm.append("public " + entity.getName() + "ModelImpl() {");
2154 sm.append("}");
2155
2156
2158 sm.append("public " + entity.getPKClassName() + " getPrimaryKey() {");
2159
2160 if (entity.hasCompoundPK()) {
2161 sm.append("return new " + entity.getPKClassName() + "(");
2162
2163 for (int i = 0; i < pkList.size(); i++) {
2164 EntityColumn col = (EntityColumn)pkList.get(i);
2165
2166 sm.append("_" + col.getName());
2167
2168 if ((i + 1) != (pkList.size())) {
2169 sm.append(", ");
2170 }
2171 }
2172
2173 sm.append(");");
2174 }
2175 else {
2176 EntityColumn col = (EntityColumn)pkList.get(0);
2177
2178 sm.append("return _" + col.getName() + ";");
2179 }
2180
2181 sm.append("}");
2182
2183 sm.append("public void setPrimaryKey(" + entity.getPKClassName() + " pk) {");
2184
2185 if (entity.hasCompoundPK()) {
2186 for (int i = 0; i < pkList.size(); i++) {
2187 EntityColumn col = (EntityColumn)pkList.get(i);
2188
2189 sm.append("set" + col.getMethodName() + "(pk." + col.getName() + ");");
2190 }
2191 }
2192 else {
2193 EntityColumn col = (EntityColumn)pkList.get(0);
2194
2195 sm.append("set" + col.getMethodName() + "(pk);");
2196 }
2197
2198 sm.append("}");
2199
2200 sm.append("public Serializable getPrimaryKeyObj() {");
2201
2202 if (entity.hasCompoundPK()) {
2203 sm.append("return new " + entity.getPKClassName() + "(");
2204
2205 for (int i = 0; i < pkList.size(); i++) {
2206 EntityColumn col = (EntityColumn)pkList.get(i);
2207
2208 sm.append("_" + col.getName());
2209
2210 if ((i + 1) != (pkList.size())) {
2211 sm.append(", ");
2212 }
2213 }
2214
2215 sm.append(");");
2216 }
2217 else {
2218 EntityColumn col = (EntityColumn)pkList.get(0);
2219
2220 sm.append("return ");
2221
2222 if (entity.hasPrimitivePK()) {
2223 sm.append("new ");
2224 sm.append(_getPrimitiveObj(entity.getPKClassName()));
2225 sm.append("(");
2226 }
2227
2228 sm.append("_" + col.getName());
2229
2230 if (entity.hasPrimitivePK()) {
2231 sm.append(")");
2232 }
2233
2234 sm.append(";");
2235 }
2236
2237 sm.append("}");
2238
2239
2241 for (int i = 0; i < regularColList.size(); i++) {
2242 EntityColumn col = (EntityColumn)regularColList.get(i);
2243
2244 String colType = col.getType();
2245
2246 sm.append("public " + colType + " get" + col.getMethodName() + "() {");
2247
2248 if (colType.equals("String") && col.isConvertNull()) {
2249 sm.append("return GetterUtil.getString(_" + col.getName() + ");");
2250 }
2251 else {
2252 sm.append("return _" + col.getName() + ";");
2253 }
2254
2255 sm.append("}");
2256
2257 if (colType.equals("boolean")) {
2258 sm.append("public " + colType + " is" + col.getMethodName() + "() {");
2259 sm.append("return _" + col.getName() + ";");
2260 sm.append("}");
2261 }
2262
2263 sm.append("public void set" + col.getMethodName() + "(" + colType + " " + col.getName() + ") {");
2264 sm.append("if (");
2265
2266 if (!col.isPrimitiveType()) {
2267 sm.append("(" + col.getName() + " == null && _" + col.getName() + " != null) ||");
2268 sm.append("(" + col.getName() + " != null && _" + col.getName() + " == null) ||");
2269 sm.append("(" + col.getName() + " != null && _" + col.getName() + " != null && !" + col.getName() + ".equals(_" + col.getName() + "))");
2270 }
2271 else {
2272 sm.append(col.getName() + " != _" + col.getName());
2273 }
2274
2275 sm.append(") {");
2276
2277 if (colType.equals("String")) {
2278 sm.append("if (!XSS_ALLOW_" + col.getName().toUpperCase() + ") {");
2279 sm.append(col.getName() + " = XSSUtil.strip(" + col.getName() + ");");
2280 sm.append("}");
2281 }
2282
2283 sm.append("_" + col.getName() + " = " + col.getName() + ";");
2284 sm.append("}");
2285 sm.append("}");
2286 }
2287
2288
2290 sm.append("public Object clone() {");
2291 sm.append(entity.getName() + "Impl clone = new " + entity.getName() + "Impl();");
2292
2293 for (int i = 0; i < regularColList.size(); i++) {
2294 EntityColumn col = (EntityColumn)regularColList.get(i);
2295
2296 sm.append("clone.set" + col.getMethodName() + "(");
2297
2298 if (col.getEJBName() == null) {
2299 sm.append("get" + col.getMethodName() + "()");
2300 }
2301 else {
2302 sm.append("(" + col.getEJBName() + ")get" + col.getMethodName() + "().clone()");
2303 }
2304
2305 sm.append(");");
2306 }
2307
2308 sm.append("return clone;");
2309 sm.append("}");
2310
2311
2313 sm.append("public int compareTo(Object obj) {");
2314 sm.append("if (obj == null) {");
2315 sm.append("return -1;");
2316 sm.append("}");
2317 sm.append(entity.getName() + "Impl " + entity.getVarName() + " = (" + entity.getName() + "Impl)obj;");
2318
2319 if (entity.isOrdered()) {
2320 EntityOrder order = entity.getOrder();
2321
2322 List orderList = order.getColumns();
2323
2324 sm.append("int value = 0;");
2325
2326 for (int i = 0; i < orderList.size(); i++) {
2327 EntityColumn col = (EntityColumn)orderList.get(i);
2328
2329 String colType = col.getType();
2330
2331 if (!col.isPrimitiveType()) {
2332 if (colType.equals("Date")) {
2333 sm.append("value = DateUtil.compareTo(get" + col.getMethodName() + "(), " + entity.getVarName() + ".get" + col.getMethodName() + "());");
2334 }
2335 else {
2336 if (col.isCaseSensitive()) {
2337 sm.append("value = get" + col.getMethodName() + "().compareTo(" + entity.getVarName() + ".get" + col.getMethodName() + "());");
2338 }
2339 else {
2340 sm.append("value = get" + col.getMethodName() + "().toLowerCase().compareTo(" + entity.getVarName() + ".get" + col.getMethodName() + "().toLowerCase());");
2341 }
2342 }
2343 }
2344 else {
2345 String ltComparator = "<";
2346 String gtComparator = ">";
2347
2348 if (colType.equals("boolean")) {
2349 ltComparator = "==";
2350 gtComparator = "!=";
2351 }
2352
2353 sm.append("if (get" + col.getMethodName() + "() " + ltComparator + " " + entity.getVarName() + ".get" + col.getMethodName() + "()) {");
2354 sm.append("value = -1;");
2355 sm.append("}");
2356 sm.append("else if (get" + col.getMethodName() + "() " + gtComparator + " " + entity.getVarName() + ".get" + col.getMethodName() + "()) {");
2357 sm.append("value = 1;");
2358 sm.append("}");
2359 sm.append("else {");
2360 sm.append("value = 0;");
2361 sm.append("}");
2362 }
2363
2364 if (!col.isOrderByAscending()) {
2365 sm.append("value = value * -1;");
2366 }
2367
2368 sm.append("if (value != 0) {");
2369 sm.append("return value;");
2370 sm.append("}");
2371 }
2372
2373 sm.append("return 0;");
2374 }
2375 else {
2376 sm.append(entity.getPKClassName() + " pk = " + entity.getVarName() + ".getPrimaryKey();");
2377
2378 if (entity.hasPrimitivePK()) {
2379 sm.append("if (getPrimaryKey() < pk) {");
2380 sm.append("return -1;");
2381 sm.append("}");
2382 sm.append("else if (getPrimaryKey() > pk) {");
2383 sm.append("return 1;");
2384 sm.append("}");
2385 sm.append("else {");
2386 sm.append("return 0;");
2387 sm.append("}");
2388 }
2389 else {
2390 sm.append("return getPrimaryKey().compareTo(pk);");
2391 }
2392 }
2393
2394 sm.append("}");
2395
2396
2398 sm.append("public boolean equals(Object obj) {");
2399 sm.append("if (obj == null) {");
2400 sm.append("return false;");
2401 sm.append("}");
2402 sm.append(entity.getName() + "Impl " + entity.getVarName() + " = null;");
2403 sm.append("try {");
2404 sm.append(entity.getVarName() + " = (" + entity.getName() + "Impl)obj;");
2405 sm.append("}");
2406 sm.append("catch (ClassCastException cce) {");
2407 sm.append("return false;");
2408 sm.append("}");
2409 sm.append(entity.getPKClassName() + " pk = " + entity.getVarName() + ".getPrimaryKey();");
2410
2411 if (entity.hasPrimitivePK()) {
2412 sm.append("if (getPrimaryKey() == pk) {");
2413 }
2414 else {
2415 sm.append("if (getPrimaryKey().equals(pk)) {");
2416 }
2417
2418 sm.append("return true;");
2419 sm.append("}");
2420 sm.append("else {");
2421 sm.append("return false;");
2422 sm.append("}");
2423 sm.append("}");
2424
2425
2427 sm.append("public int hashCode() {");
2428
2429 if (entity.hasPrimitivePK()) {
2430 sm.append("return (int)getPrimaryKey();");
2431 }
2432 else {
2433 sm.append("return getPrimaryKey().hashCode();");
2434 }
2435
2436 sm.append("}");
2437
2438
2440 for (int i = 0; i < regularColList.size(); i++) {
2441 EntityColumn col = (EntityColumn)regularColList.get(i);
2442
2443 sm.append("private " + col.getType() + " _" + col.getName() + ";");
2444 }
2445
2446
2448 sm.append("}");
2449
2450
2452 File modelFile = new File(_outputPath + "/model/impl/" + entity.getName() + "ModelImpl.java");
2453
2454 Map jalopySettings = new HashMap();
2455
2456 String[] classComments = {
2457 _DEFAULT_CLASS_COMMENTS,
2458 "This class is a model that represents the <code>" + entity.getTable() + "</code> table in the database."
2459 };
2460
2461 String[] see = {
2462 _packagePath + ".service.model." + entity.getName(),
2463 _packagePath + ".service.model." + entity.getName() + "Model",
2464 _packagePath + ".service.model.impl." + entity.getName() + "Impl"
2465 };
2466
2467 jalopySettings.put("classComments", classComments);
2468 jalopySettings.put("see", see);
2469
2470 writeFile(modelFile, sm.toString(), jalopySettings);
2471 }
2472
2473 private void _createModelSoap(Entity entity) throws IOException {
2474 List pkList = entity.getPKList();
2475 List regularColList = entity.getRegularColList();
2476
2477 StringMaker sm = new StringMaker();
2478
2479
2481 sm.append("package " + _packagePath + ".model;");
2482
2483
2485 if (entity.hasCompoundPK()) {
2486 sm.append("import " + _packagePath + ".service.persistence." + entity.getName() + "PK;");
2487 }
2488
2489 sm.append("import java.io.Serializable;");
2490 sm.append("import java.util.ArrayList;");
2491 sm.append("import java.util.Date;");
2492 sm.append("import java.util.List;");
2493
2494
2496 sm.append("public class " + entity.getName() + "Soap implements Serializable {");
2497
2498
2500 sm.append("public static " + entity.getName() + "Soap toSoapModel(" + entity.getName() + " model) {");
2501 sm.append(entity.getName() + "Soap soapModel = new " + entity.getName() + "Soap();");
2502
2503 for (int i = 0; i < regularColList.size(); i++) {
2504 EntityColumn col = (EntityColumn)regularColList.get(i);
2505
2506 sm.append("soapModel.set" + col.getMethodName() + "(model.get" + col.getMethodName() + "());");
2507 }
2508
2509 sm.append("return soapModel;");
2510 sm.append("}");
2511
2512 sm.append("public static " + entity.getName() + "Soap[] toSoapModels(List models) {");
2513 sm.append("List soapModels = new ArrayList(models.size());");
2514 sm.append("for (int i = 0; i < models.size(); i++) {");
2515 sm.append(entity.getName() + " model = (" + entity.getName() + ")models.get(i);");
2516 sm.append("soapModels.add(toSoapModel(model));");
2517 sm.append("}");
2518 sm.append("return (" + entity.getName() + "Soap[])soapModels.toArray(new " + entity.getName() + "Soap[0]);");
2519 sm.append("}");
2520
2521
2523 sm.append("public " + entity.getName() + "Soap() {");
2524 sm.append("}");
2525
2526
2528 sm.append("public " + entity.getPKClassName() + " getPrimaryKey() {");
2529
2530 if (entity.hasCompoundPK()) {
2531 sm.append("return new " + entity.getPKClassName() + "(");
2532
2533 for (int i = 0; i < pkList.size(); i++) {
2534 EntityColumn col = (EntityColumn)pkList.get(i);
2535
2536 sm.append("_" + col.getName());
2537
2538 if ((i + 1) != (pkList.size())) {
2539 sm.append(", ");
2540 }
2541 }
2542
2543 sm.append(");");
2544 }
2545 else {
2546 EntityColumn col = (EntityColumn)pkList.get(0);
2547
2548 sm.append("return _" + col.getName() + ";");
2549 }
2550
2551 sm.append("}");
2552
2553 sm.append("public void setPrimaryKey(" + entity.getPKClassName() + " pk) {");
2554
2555 if (entity.hasCompoundPK()) {
2556 for (int i = 0; i < pkList.size(); i++) {
2557 EntityColumn col = (EntityColumn)pkList.get(i);
2558
2559 sm.append("set" + col.getMethodName() + "(pk." + col.getName() + ");");
2560 }
2561 }
2562 else {
2563 EntityColumn col = (EntityColumn)pkList.get(0);
2564
2565 sm.append("set" + col.getMethodName() + "(pk);");
2566 }
2567
2568 sm.append("}");
2569
2570
2572 for (int i = 0; i < regularColList.size(); i++) {
2573 EntityColumn col = (EntityColumn)regularColList.get(i);
2574
2575 String colType = col.getType();
2576
2577 sm.append("public " + colType + " get" + col.getMethodName() + "() {");
2578 sm.append("return _" + col.getName() + ";");
2579 sm.append("}");
2580
2581 if (colType.equals("boolean")) {
2582 sm.append("public " + colType + " is" + col.getMethodName() + "() {");
2583 sm.append("return _" + col.getName() + ";");
2584 sm.append("}");
2585 }
2586
2587 sm.append("public void set" + col.getMethodName() + "(" + colType + " " + col.getName() + ") {");
2588 sm.append("_" + col.getName() + " = " + col.getName() + ";");
2589 sm.append("}");
2590 }
2591
2592
2594 for (int i = 0; i < regularColList.size(); i++) {
2595 EntityColumn col = (EntityColumn)regularColList.get(i);
2596
2597 sm.append("private " + col.getType() + " _" + col.getName() + ";");
2598 }
2599
2600
2602 sm.append("}");
2603
2604
2606 File modelFile = new File(_serviceOutputPath + "/model/" + entity.getName() + "Soap.java");
2607
2608 Map jalopySettings = new HashMap();
2609
2610 String[] classComments = {
2611 _DEFAULT_CLASS_COMMENTS,
2612 "This class is used by <code>" + _packagePath + ".service.http." + entity.getName() + "ServiceSoap</code>."
2613 };
2614
2615 String[] see = {
2616 _packagePath + ".service.http." + entity.getName() + "ServiceSoap"
2617 };
2618
2619 jalopySettings.put("classComments", classComments);
2620 jalopySettings.put("see", see);
2621
2622 writeFile(modelFile, sm.toString(), jalopySettings);
2623 }
2624
2625 private void _createPersistence(Entity entity) throws IOException {
2626 JavaClass javaClass = _getJavaClass(_outputPath + "/service/persistence/" + entity.getName() + "PersistenceImpl.java");
2627
2628 JavaMethod[] methods = javaClass.getMethods();
2629
2630 StringMaker sm = new StringMaker();
2631
2632
2634 sm.append("package " + _packagePath + ".service.persistence;");
2635
2636
2638 sm.append("public interface " + entity.getName() + "Persistence {");
2639
2640
2642 for (int i = 0; i < methods.length; i++) {
2643 JavaMethod javaMethod = methods[i];
2644
2645 String methodName = javaMethod.getName();
2646
2647 if (!javaMethod.isConstructor() && javaMethod.isPublic()) {
2648 sm.append("public " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
2649
2650 JavaParameter[] parameters = javaMethod.getParameters();
2651
2652 for (int j = 0; j < parameters.length; j++) {
2653 JavaParameter javaParameter = parameters[j];
2654
2655 sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
2656
2657 if ((j + 1) != parameters.length) {
2658 sm.append(", ");
2659 }
2660 }
2661
2662 sm.append(")");
2663
2664 Type[] thrownExceptions = javaMethod.getExceptions();
2665
2666 if (thrownExceptions.length > 0) {
2667 sm.append(" throws ");
2668
2669 for (int j = 0; j < thrownExceptions.length; j++) {
2670 Type thrownException = thrownExceptions[j];
2671
2672 sm.append(thrownException.getValue());
2673
2674 if ((j + 1) != thrownExceptions.length) {
2675 sm.append(", ");
2676 }
2677 }
2678 }
2679
2680 sm.append(";");
2681 }
2682 }
2683
2684
2686 sm.append("}");
2687
2688
2690 File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "Persistence.java");
2691
2692 writeFile(ejbFile, sm.toString());
2693 }
2694
2695 private void _createPersistenceImpl(Entity entity) throws IOException {
2696 List columnList = entity.getColumnList();
2697 List finderList = entity.getFinderList();
2698
2699 String pkClassName = entity.getPKClassName();
2700 String pkVarName = entity.getPKVarName();
2701
2702 StringMaker sm = new StringMaker();
2703
2704
2706 sm.append("package " + _packagePath + ".service.persistence;");
2707
2708
2710 sm.append("import " + _packagePath + "." + _getNoSuchEntityException(entity) + "Exception;");
2711 sm.append("import " + _packagePath + ".model." + entity.getName() + ";");
2712 sm.append("import " + _packagePath + ".model.impl." + entity.getName() + "Impl;");
2713 sm.append("import " + _basePersistencePackage + ".BasePersistence;");
2714 sm.append("import " + _springHibernatePackage + ".FinderCache;");
2715 sm.append("import " + _springHibernatePackage + ".HibernateUtil;");
2716 sm.append("import com.liferay.portal.PortalException;");
2717 sm.append("import com.liferay.portal.SystemException;");
2718 sm.append("import com.liferay.portal.kernel.dao.DynamicQuery;");
2719 sm.append("import com.liferay.portal.kernel.dao.DynamicQueryInitializer;");
2720 sm.append("import com.liferay.portal.kernel.util.OrderByComparator;");
2721 sm.append("import com.liferay.portal.kernel.util.StringMaker;");
2722 sm.append("import com.liferay.portal.kernel.util.StringPool;");
2723 sm.append("import com.liferay.util.dao.hibernate.QueryPos;");
2724 sm.append("import com.liferay.util.dao.hibernate.QueryUtil;");
2725 sm.append("import java.sql.ResultSet;");
2726 sm.append("import java.sql.SQLException;");
2727 sm.append("import java.sql.Types;");
2728 sm.append("import java.util.Collection;");
2729 sm.append("import java.util.Collections;");
2730 sm.append("import java.util.Date;");
2731 sm.append("import java.util.HashSet;");
2732 sm.append("import java.util.Iterator;");
2733 sm.append("import java.util.List;");
2734 sm.append("import java.util.Set;");
2735 sm.append("import javax.sql.DataSource;");
2736 sm.append("import org.apache.commons.logging.Log;");
2737 sm.append("import org.apache.commons.logging.LogFactory;");
2738 sm.append("import org.hibernate.Hibernate;");
2739 sm.append("import org.hibernate.ObjectNotFoundException;");
2740 sm.append("import org.hibernate.Query;");
2741 sm.append("import org.hibernate.Session;");
2742 sm.append("import org.hibernate.SQLQuery;");
2743 sm.append("import org.springframework.dao.DataAccessException;");
2744 sm.append("import org.springframework.jdbc.core.SqlParameter;");
2745 sm.append("import org.springframework.jdbc.object.MappingSqlQuery;");
2746 sm.append("import org.springframework.jdbc.object.SqlUpdate;");
2747
2748
2750 sm.append("public class " + entity.getName() + "PersistenceImpl extends BasePersistence implements " + entity.getName() + "Persistence {");
2751
2752
2754 sm.append("public " + entity.getName() + " create(" + entity.getPKClassName() + " " + pkVarName + ") {");
2755 sm.append(entity.getName() + " " + entity.getVarName() + " = new " + entity.getName() + "Impl();");
2756 sm.append(entity.getVarName() + ".setNew(true);");
2757 sm.append(entity.getVarName() + ".setPrimaryKey(" + pkVarName + ");");
2758 sm.append("return " + entity.getVarName() + ";");
2759 sm.append("}");
2760
2761
2763 sm.append("public " + entity.getName() + " remove(" + pkClassName + " " + pkVarName + ") throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
2764 sm.append("Session session = null;");
2765 sm.append("try {");
2766 sm.append("session = openSession();");
2767 sm.append(entity.getName() + " " + entity.getVarName() + " = (" + entity.getName() + ")session.get(" + entity.getName() + "Impl.class, ");
2768
2769 if (entity.hasPrimitivePK()) {
2770 sm.append("new ");
2771 sm.append(_getPrimitiveObj(entity.getPKClassName()));
2772 sm.append("(");
2773 }
2774
2775 sm.append(pkVarName);
2776
2777 if (entity.hasPrimitivePK()) {
2778 sm.append(")");
2779 }
2780
2781 sm.append(");");
2782 sm.append("if (" + entity.getVarName() + " == null) {");
2783 sm.append("if (_log.isWarnEnabled()) {");
2784 sm.append("_log.warn(\"No " + entity.getName() + " exists with the primary key \" + " + pkVarName + ");");
2785 sm.append("}");
2786 sm.append("throw new " + _getNoSuchEntityException(entity) + "Exception(\"No " + entity.getName() + " exists with the primary key \" + " + pkVarName + ");");
2787 sm.append("}");
2788 sm.append("return remove(" + entity.getVarName() + ");");
2789 sm.append("}");
2790 sm.append("catch (" + _getNoSuchEntityException(entity) + "Exception nsee) {");
2791 sm.append("throw nsee;");
2792 sm.append("}");
2793 sm.append("catch (Exception e) {");
2794 sm.append("throw HibernateUtil.processException(e);");
2795 sm.append("}");
2796 sm.append("finally {");
2797 sm.append("closeSession(session);");
2798 sm.append("}");
2799 sm.append("}");
2800
2801 sm.append("public " + entity.getName() + " remove(" + entity.getName() + " " + entity.getVarName() + ") throws SystemException {");
2802
2803 for (int i = 0; i < columnList.size(); i++) {
2804 EntityColumn col = (EntityColumn)columnList.get(i);
2805
2806 if (col.isCollection() && col.isMappingManyToMany()) {
2807 Entity tempEntity = getEntity(col.getEJBName());
2808
2809
2811 sm.append("try {");
2812 sm.append("clear" + tempEntity.getNames() + ".clear(" + entity.getVarName() + ".getPrimaryKey());");
2813 sm.append("}");
2814 sm.append("catch (Exception e) {");
2815 sm.append("throw HibernateUtil.processException(e);");
2816 sm.append("}");
2817 sm.append("finally {");
2818 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
2819 sm.append("}");
2820 }
2821 }
2822
2823 sm.append("Session session = null;");
2824 sm.append("try {");
2825 sm.append("session = openSession();");
2826 sm.append("session.delete(" + entity.getVarName() + ");");
2828 sm.append("session.flush();");
2829 sm.append("return " + entity.getVarName() + ";");
2830 sm.append("}");
2831 sm.append("catch (Exception e) {");
2832 sm.append("throw HibernateUtil.processException(e);");
2833 sm.append("}");
2834 sm.append("finally {");
2835 sm.append("closeSession(session);");
2836 sm.append("FinderCache.clearCache(" + entity.getName() + ".class.getName());");
2837 sm.append("}");
2838 sm.append("}");
2839
2840
2842 sm.append("public " + entity.getName() + " update(" + _packagePath + ".model." + entity.getName() + " " + entity.getVarName() + ") throws SystemException {");
2843 sm.append("return update(" + entity.getVarName() + ", false);");
2844 sm.append("}");
2845
2846 sm.append("public " + entity.getName() + " update(" + _packagePath + ".model." + entity.getName() + " " + entity.getVarName() + ", boolean merge) throws SystemException {");
2847
2848 for (int i = 0; i < columnList.size(); i++) {
2849 EntityColumn col = (EntityColumn)columnList.get(i);
2850
2851 if (col.isCollection() && col.isMappingManyToMany()) {
2852 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
2853 }
2854 }
2855
2856 sm.append("Session session = null;");
2857 sm.append("try {");
2858 sm.append("session = openSession();");
2859 sm.append("if (merge) {");
2860 sm.append("session.merge(" + entity.getVarName() + ");");
2861 sm.append("}");
2862 sm.append("else {");
2863 sm.append("if (" + entity.getVarName() + ".isNew()) {");
2864 sm.append("session.save(" + entity.getVarName() + ");");
2865 sm.append("}");
2866 sm.append("}");
2870 sm.append("session.flush();");
2871 sm.append(entity.getVarName() + ".setNew(false);");
2872 sm.append("return " + entity.getVarName() + ";");
2873 sm.append("}");
2874 sm.append("catch (Exception e) {");
2875 sm.append("throw HibernateUtil.processException(e);");
2876 sm.append("}");
2877 sm.append("finally {");
2878 sm.append("closeSession(session);");
2879 sm.append("FinderCache.clearCache(" + entity.getName() + ".class.getName());");
2880 sm.append("}");
2881 sm.append("}");
2882
2883
2885 sm.append("public " + entity.getName() + " findByPrimaryKey(" + pkClassName + " " + pkVarName + ") throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
2886 sm.append(entity.getName() + " " + entity.getVarName() + " = fetchByPrimaryKey(" + pkVarName + ");");
2887 sm.append("if (" + entity.getVarName() + " == null) {");
2888 sm.append("if (_log.isWarnEnabled()) {");
2889 sm.append("_log.warn(\"No " + entity.getName() + " exists with the primary key \" + " + pkVarName + ");");
2890 sm.append("}");
2891 sm.append("throw new " + _getNoSuchEntityException(entity) + "Exception(\"No " + entity.getName() + " exists with the primary key \" + " + pkVarName + ");");
2892 sm.append("}");
2893 sm.append("return " + entity.getVarName() + ";");
2894 sm.append("}");
2895
2896 sm.append("public " + entity.getName() + " fetchByPrimaryKey(" + pkClassName + " " + pkVarName + ") throws SystemException {");
2897 sm.append("Session session = null;");
2898 sm.append("try {");
2899 sm.append("session = openSession();");
2900 sm.append("return (" + entity.getName() + ")session.get(" + entity.getName() + "Impl.class, ");
2901
2902 if (entity.hasPrimitivePK()) {
2903 sm.append("new ");
2904 sm.append(_getPrimitiveObj(entity.getPKClassName()));
2905 sm.append("(");
2906 }
2907
2908 sm.append(pkVarName);
2909
2910 if (entity.hasPrimitivePK()) {
2911 sm.append(")");
2912 }
2913
2914 sm.append(");");
2915 sm.append("}");
2916 sm.append("catch (Exception e) {");
2917 sm.append("throw HibernateUtil.processException(e);");
2918 sm.append("}");
2919 sm.append("finally {");
2920 sm.append("closeSession(session);");
2921 sm.append("}");
2922 sm.append("}");
2923
2924 for (int i = 0; i < finderList.size(); i++) {
2925 EntityFinder finder = (EntityFinder)finderList.get(i);
2926
2927 List finderColsList = finder.getColumns();
2928
2929 if (!finder.isCollection()) {
2930 sm.append("public " + entity.getName() + " findBy" + finder.getName() + "(");
2931
2932 for (int j = 0; j < finderColsList.size(); j++) {
2933 EntityColumn col = (EntityColumn)finderColsList.get(j);
2934
2935 sm.append(col.getType() + " " + col.getName());
2936
2937 if ((j + 1) != finderColsList.size()) {
2938 sm.append(", ");
2939 }
2940 }
2941
2942 sm.append(") throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
2943 sm.append(entity.getName() + " " + entity.getVarName() + " = fetchBy" + finder.getName() + "(");
2944
2945 for (int j = 0; j < finderColsList.size(); j++) {
2946 EntityColumn col = (EntityColumn)finderColsList.get(j);
2947
2948 sm.append(col.getName());
2949
2950 if ((j + 1) != finderColsList.size()) {
2951 sm.append(", ");
2952 }
2953 }
2954
2955 sm.append(");");
2956 sm.append("if (" + entity.getVarName() + " == null) {");
2957 sm.append("StringMaker msg = new StringMaker();");
2958 sm.append("msg.append(\"No " + entity.getName() + " exists with the key \");");
2959
2960 for (int j = 0; j < finderColsList.size(); j++) {
2961 EntityColumn col = (EntityColumn)finderColsList.get(j);
2962
2963 if (j == 0) {
2964 sm.append("msg.append(StringPool.OPEN_CURLY_BRACE);");
2965 }
2966
2967 sm.append("msg.append(\"" + col.getName() + "=\");");
2968 sm.append("msg.append(" + col.getName() + ");");
2969
2970 if ((j + 1) != finderColsList.size()) {
2971 sm.append("msg.append(\", \");");
2972 }
2973
2974 if ((j + 1) == finderColsList.size()) {
2975 sm.append("msg.append(StringPool.CLOSE_CURLY_BRACE);");
2976 }
2977 }
2978
2979 sm.append("if (_log.isWarnEnabled()) {");
2980 sm.append("_log.warn(msg.toString());");
2981 sm.append("}");
2982 sm.append("throw new " + _getNoSuchEntityException(entity) + "Exception(msg.toString());");
2983 sm.append("}");
2984 sm.append("return " + entity.getVarName() + ";");
2985 sm.append("}");
2986
2987 sm.append("public " + entity.getName() + " fetchBy" + finder.getName() + "(");
2988
2989 for (int j = 0; j < finderColsList.size(); j++) {
2990 EntityColumn col = (EntityColumn)finderColsList.get(j);
2991
2992 sm.append(col.getType() + " " + col.getName());
2993
2994 if ((j + 1) != finderColsList.size()) {
2995 sm.append(", ");
2996 }
2997 }
2998
2999 sm.append(") throws SystemException {");
3000 sm.append("String finderClassName = " + entity.getName() + ".class.getName();");
3001 sm.append("String finderMethodName = \"fetchBy" + finder.getName() + "\";");
3002 sm.append("String finderParams[] = new String[] {");
3003
3004 for (int j = 0; j < finderColsList.size(); j++) {
3005 EntityColumn col = (EntityColumn)finderColsList.get(j);
3006
3007 if (col.isPrimitiveType()) {
3008 sm.append(_getPrimitiveObj(col.getType()));
3009 }
3010 else {
3011 sm.append(col.getType());
3012 }
3013
3014 sm.append(".class.getName()");
3015
3016 if ((j + 1) != finderColsList.size()) {
3017 sm.append(", ");
3018 }
3019 }
3020
3021 sm.append("};");
3022 sm.append("Object finderArgs[] = new Object[] {");
3023
3024 for (int j = 0; j < finderColsList.size(); j++) {
3025 EntityColumn col = (EntityColumn)finderColsList.get(j);
3026
3027 if (col.isPrimitiveType()) {
3028 if (col.getType().equals("boolean")) {
3029 sm.append(_getPrimitiveObj(col.getType()) + ".valueOf(");
3030 }
3031 else {
3032 sm.append("new " + _getPrimitiveObj(col.getType()) + "(");
3033 }
3034 }
3035
3036 sm.append(col.getName());
3037
3038 if (col.isPrimitiveType()) {
3039 sm.append(")");
3040 }
3041
3042 if ((j + 1) != finderColsList.size()) {
3043 sm.append(", ");
3044 }
3045 }
3046
3047 sm.append("};");
3048 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
3049 sm.append("if (result == null) {");
3050 sm.append("Session session = null;");
3051 sm.append("try {");
3052 sm.append("session = openSession();");
3053 sm.append("StringMaker query = new StringMaker();");
3054 sm.append("query.append(\"FROM " + _packagePath + ".model." + entity.getName() + " WHERE \");");
3055
3056 for (int j = 0; j < finderColsList.size(); j++) {
3057 EntityColumn col = (EntityColumn)finderColsList.get(j);
3058
3059 if (_requiresNullCheck(col)) {
3060 _appendNullLogic(col, sm);
3061 }
3062
3063 sm.append("query.append(\"" + col.getDBName() + " " + col.getComparator() + " ?\");");
3064
3065 if (_requiresNullCheck(col)) {
3066 sm.append("}");
3067 }
3068
3069 if ((j + 1) != finderColsList.size()) {
3070 sm.append("query.append(\" AND \");");
3071 }
3072 else if (Validator.isNull(finder.getWhere())) {
3073 sm.append("query.append(\" \");");
3074 }
3075 else {
3076 sm.append("query.append(\" AND " + finder.getWhere() + " \");");
3077 }
3078 }
3079
3080 EntityOrder order = entity.getOrder();
3081
3082 if (order != null) {
3083 List orderList = order.getColumns();
3084
3085 sm.append("query.append(\"ORDER BY \");");
3086
3087 for (int j = 0; j < orderList.size(); j++) {
3088 EntityColumn col = (EntityColumn)orderList.get(j);
3089
3090 sm.append("query.append(\"" + col.getDBName() + " " + (col.isOrderByAscending() ? "ASC" : "DESC") + "\")");
3091
3092 if ((j + 1) != orderList.size()) {
3093 sm.append(".append(\", \");");
3094 }
3095 else {
3096 sm.append(";");
3097 }
3098 }
3099 }
3100
3101 sm.append("Query q = session.createQuery(query.toString());");
3102 sm.append("int queryPos = 0;");
3103
3104 for (int j = 0; j < finderColsList.size(); j++) {
3105 EntityColumn col = (EntityColumn)finderColsList.get(j);
3106
3107 if (_requiresNullCheck(col)) {
3108 sm.append("if (" + col.getName() + " != null) {");
3109 }
3110
3111 String colType = col.getType();
3112 String colObjType = colType;
3113
3114 if (col.isPrimitiveType()) {
3115 colObjType = _getPrimitiveObj(colType);
3116 }
3117
3118 sm.append("q.set" + colObjType + "(queryPos++, " + col.getName());
3119
3120 if (colType.equals("Boolean")) {
3121 sm.append(".booleanValue()");
3122 }
3123 else if (colType.equals("Double")) {
3124 sm.append(".doubleValue()");
3125 }
3126 else if (colType.equals("Float")) {
3127 sm.append(".floatValue()");
3128 }
3129 else if (colType.equals("Integer")) {
3130 sm.append(".intValue()");
3131 }
3132 else if (colType.equals("Long")) {
3133 sm.append(".longValue()");
3134 }
3135 else if (colType.equals("Short")) {
3136 sm.append(".shortValue()");
3137 }
3138
3139 sm.append(");");
3140
3141 if (_requiresNullCheck(col)) {
3142 sm.append("}");
3143 }
3144 }
3145
3146 sm.append("List list = q.list();");
3147 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, list);");
3148 sm.append("if (list.size() == 0) {");
3149 sm.append("return null;");
3150 sm.append("}");
3151 sm.append("else {");
3152 sm.append("return (" + entity.getName() + ")list.get(0);");
3153 sm.append("}");
3154 sm.append("}");
3155 sm.append("catch (Exception e) {");
3156 sm.append("throw HibernateUtil.processException(e);");
3157 sm.append("}");
3158 sm.append("finally {");
3159 sm.append("closeSession(session);");
3160 sm.append("}");
3161 sm.append("}");
3162 sm.append("else {");
3163 sm.append("List list = (List)result;");
3164 sm.append("if (list.size() == 0) {");
3165 sm.append("return null;");
3166 sm.append("}");
3167 sm.append("else {");
3168 sm.append("return (" + entity.getName() + ")list.get(0);");
3169 sm.append("}");
3170 sm.append("}");
3171 sm.append("}");
3172 }
3173 else {
3174 sm.append("public List findBy" + finder.getName() + "(");
3175
3176 for (int j = 0; j < finderColsList.size(); j++) {
3177 EntityColumn col = (EntityColumn)finderColsList.get(j);
3178
3179 sm.append(col.getType() + " " + col.getName());
3180
3181 if ((j + 1) != finderColsList.size()) {
3182 sm.append(", ");
3183 }
3184 }
3185
3186 sm.append(") throws SystemException {");
3187 sm.append("String finderClassName = " + entity.getName() + ".class.getName();");
3188 sm.append("String finderMethodName = \"findBy" + finder.getName() + "\";");
3189 sm.append("String finderParams[] = new String[] {");
3190
3191 for (int j = 0; j < finderColsList.size(); j++) {
3192 EntityColumn col = (EntityColumn)finderColsList.get(j);
3193
3194 if (col.isPrimitiveType()) {
3195 sm.append(_getPrimitiveObj(col.getType()));
3196 }
3197 else {
3198 sm.append(col.getType());
3199 }
3200
3201 sm.append(".class.getName()");
3202
3203 if ((j + 1) != finderColsList.size()) {
3204 sm.append(", ");
3205 }
3206 }
3207
3208 sm.append("};");
3209 sm.append("Object finderArgs[] = new Object[] {");
3210
3211 for (int j = 0; j < finderColsList.size(); j++) {
3212 EntityColumn col = (EntityColumn)finderColsList.get(j);
3213
3214 if (col.isPrimitiveType()) {
3215 if (col.getType().equals("boolean")) {
3216 sm.append(_getPrimitiveObj(col.getType()) + ".valueOf(");
3217 }
3218 else {
3219 sm.append("new " + _getPrimitiveObj(col.getType()) + "(");
3220 }
3221 }
3222
3223 sm.append(col.getName());
3224
3225 if (col.isPrimitiveType()) {
3226 sm.append(")");
3227 }
3228
3229 if ((j + 1) != finderColsList.size()) {
3230 sm.append(", ");
3231 }
3232 }
3233
3234 sm.append("};");
3235 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
3236 sm.append("if (result == null) {");
3237 sm.append("Session session = null;");
3238 sm.append("try {");
3239 sm.append("session = openSession();");
3240 sm.append("StringMaker query = new StringMaker();");
3241 sm.append("query.append(\"FROM " + _packagePath + ".model." + entity.getName() + " WHERE \");");
3242
3243 for (int j = 0; j < finderColsList.size(); j++) {
3244 EntityColumn col = (EntityColumn)finderColsList.get(j);
3245
3246 if (_requiresNullCheck(col)) {
3247 _appendNullLogic(col, sm);
3248 }
3249
3250 sm.append("query.append(\"" + col.getDBName() + " " + col.getComparator() + " ?\");");
3251
3252 if (_requiresNullCheck(col)) {
3253 sm.append("}");
3254 }
3255
3256 if ((j + 1) != finderColsList.size()) {
3257 sm.append("query.append(\" AND \");");
3258 }
3259 else if (Validator.isNull(finder.getWhere())) {
3260 sm.append("query.append(\" \");");
3261 }
3262 else {
3263 sm.append("query.append(\" AND " + finder.getWhere() + " \");");
3264 }
3265 }
3266
3267 EntityOrder order = entity.getOrder();
3268
3269 if (order != null) {
3270 List orderList = order.getColumns();
3271
3272 sm.append("query.append(\"ORDER BY \");");
3273
3274 for (int j = 0; j < orderList.size(); j++) {
3275 EntityColumn col = (EntityColumn)orderList.get(j);
3276
3277 sm.append("query.append(\"" + col.getDBName() + " " + (col.isOrderByAscending() ? "ASC" : "DESC") + "\")");
3278
3279 if ((j + 1) != orderList.size()) {
3280 sm.append(".append(\", \");");
3281 }
3282 else {
3283 sm.append(";");
3284 }
3285 }
3286 }
3287
3288 sm.append("Query q = session.createQuery(query.toString());");
3289 sm.append("int queryPos = 0;");
3290
3291 for (int j = 0; j < finderColsList.size(); j++) {
3292 EntityColumn col = (EntityColumn)finderColsList.get(j);
3293
3294 if (_requiresNullCheck(col)) {
3295 sm.append("if (" + col.getName() + " != null) {");
3296 }
3297
3298 String colType = col.getType();
3299 String colObjType = colType;
3300
3301 if (col.isPrimitiveType()) {
3302 colObjType = _getPrimitiveObj(colType);
3303 }
3304
3305 sm.append("q.set" + colObjType + "(queryPos++, " + col.getName());
3306
3307 if (colType.equals("Boolean")) {
3308 sm.append(".booleanValue()");
3309 }
3310 else if (colType.equals("Double")) {
3311 sm.append(".doubleValue()");
3312 }
3313 else if (colType.equals("Float")) {
3314 sm.append(".floatValue()");
3315 }
3316 else if (colType.equals("Integer")) {
3317 sm.append(".intValue()");
3318 }
3319 else if (colType.equals("Long")) {
3320 sm.append(".longValue()");
3321 }
3322 else if (colType.equals("Short")) {
3323 sm.append(".shortValue()");
3324 }
3325
3326 sm.append(");");
3327
3328 if (_requiresNullCheck(col)) {
3329 sm.append("}");
3330 }
3331 }
3332
3333 sm.append("List list = q.list();");
3334 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, list);");
3335 sm.append("return list;");
3336 sm.append("}");
3337 sm.append("catch (Exception e) {");
3338 sm.append("throw HibernateUtil.processException(e);");
3339 sm.append("}");
3340 sm.append("finally {");
3341 sm.append("closeSession(session);");
3342 sm.append("}");
3343 sm.append("}");
3344 sm.append("else {");
3345 sm.append("return (List)result;");
3346 sm.append("}");
3347 sm.append("}");
3348
3349 sm.append("public List findBy" + finder.getName() + "(");
3350
3351 for (int j = 0; j < finderColsList.size(); j++) {
3352 EntityColumn col = (EntityColumn)finderColsList.get(j);
3353
3354 sm.append(col.getType() + " " + col.getName() + ", ");
3355 }
3356
3357 sm.append("int begin, int end) throws SystemException {");
3358 sm.append("return findBy" + finder.getName() + "(");
3359
3360 for (int j = 0; j < finderColsList.size(); j++) {
3361 EntityColumn col = (EntityColumn)finderColsList.get(j);
3362
3363 sm.append("" + col.getName() + ", ");
3364 }
3365
3366 sm.append("begin, end, null);");
3367 sm.append("}");
3368
3369 sm.append("public List findBy" + finder.getName() + "(");
3370
3371 for (int j = 0; j < finderColsList.size(); j++) {
3372 EntityColumn col = (EntityColumn)finderColsList.get(j);
3373
3374 sm.append(col.getType() + " " + col.getName() + ", ");
3375 }
3376
3377 sm.append("int begin, int end, OrderByComparator obc) throws SystemException {");
3378 sm.append("String finderClassName = " + entity.getName() + ".class.getName();");
3379 sm.append("String finderMethodName = \"findBy" + finder.getName() + "\";");
3380 sm.append("String finderParams[] = new String[] {");
3381
3382 for (int j = 0; j < finderColsList.size(); j++) {
3383 EntityColumn col = (EntityColumn)finderColsList.get(j);
3384
3385 if (col.isPrimitiveType()) {
3386 sm.append(_getPrimitiveObj(col.getType()));
3387 }
3388 else {
3389 sm.append(col.getType());
3390 }
3391
3392 sm.append(".class.getName(), ");
3393 }
3394
3395 sm.append("\"java.lang.Integer\", \"java.lang.Integer\", \"com.liferay.portal.kernel.util.OrderByComparator\"");
3396 sm.append("};");
3397 sm.append("Object finderArgs[] = new Object[] {");
3398
3399 for (int j = 0; j < finderColsList.size(); j++) {
3400 EntityColumn col = (EntityColumn)finderColsList.get(j);
3401
3402 if (col.isPrimitiveType()) {
3403 if (col.getType().equals("boolean")) {
3404 sm.append(_getPrimitiveObj(col.getType()) + ".valueOf(");
3405 }
3406 else {
3407 sm.append("new " + _getPrimitiveObj(col.getType()) + "(");
3408 }
3409 }
3410
3411 sm.append(col.getName());
3412
3413 if (col.isPrimitiveType()) {
3414 sm.append(")");
3415 }
3416
3417 sm.append(", ");
3418 }
3419
3420 sm.append("String.valueOf(begin), String.valueOf(end), String.valueOf(obc)");
3421 sm.append("};");
3422 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
3423 sm.append("if (result == null) {");
3424 sm.append("Session session = null;");
3425 sm.append("try {");
3426 sm.append("session = openSession();");
3427 sm.append("StringMaker query = new StringMaker();");
3428 sm.append("query.append(\"FROM " + _packagePath + ".model." + entity.getName() + " WHERE \");");
3429
3430 for (int j = 0; j < finderColsList.size(); j++) {
3431 EntityColumn col = (EntityColumn)finderColsList.get(j);
3432
3433 if (_requiresNullCheck(col)) {
3434 _appendNullLogic(col, sm);
3435 }
3436
3437 sm.append("query.append(\"" + col.getDBName() + " " + col.getComparator() + " ?\");");
3438
3439 if (_requiresNullCheck(col)) {
3440 sm.append("}");
3441 }
3442
3443 if ((j + 1) != finderColsList.size()) {
3444 sm.append("query.append(\" AND \");");
3445 }
3446 else if (Validator.isNull(finder.getWhere())) {
3447 sm.append("query.append(\" \");");
3448 }
3449 else {
3450 sm.append("query.append(\" AND " + finder.getWhere() + " \");");
3451 }
3452 }
3453
3454 sm.append("if (obc != null) {");
3455 sm.append("query.append(\"ORDER BY \");");
3456 sm.append("query.append(obc.getOrderBy());");
3457 sm.append("}");
3458
3459 if (order != null) {
3460 List orderList = order.getColumns();
3461
3462 sm.append("else {");
3463 sm.append("query.append(\"ORDER BY \");");
3464
3465 for (int j = 0; j < orderList.size(); j++) {
3466 EntityColumn col = (EntityColumn)orderList.get(j);
3467
3468 sm.append("query.append(\"" + col.getDBName() + " " + (col.isOrderByAscending() ? "ASC" : "DESC") + "\")");
3469
3470 if ((j + 1) != orderList.size()) {
3471 sm.append(".append(\", \");");
3472 }
3473 else {
3474 sm.append(";");
3475 }
3476 }
3477
3478 sm.append("}");
3479 }
3480
3481 sm.append("Query q = session.createQuery(query.toString());");
3482 sm.append("int queryPos = 0;");
3483
3484 for (int j = 0; j < finderColsList.size(); j++) {
3485 EntityColumn col = (EntityColumn)finderColsList.get(j);
3486
3487 if (_requiresNullCheck(col)) {
3488 sm.append("if (" + col.getName() + " != null) {");
3489 }
3490
3491 String colType = col.getType();
3492 String colObjType = colType;
3493
3494 if (col.isPrimitiveType()) {
3495 colObjType = _getPrimitiveObj(colType);
3496 }
3497
3498 sm.append("q.set" + colObjType + "(queryPos++, " + col.getName());
3499
3500 if (colType.equals("Boolean")) {
3501 sm.append(".booleanValue()");
3502 }
3503 else if (colType.equals("Double")) {
3504 sm.append(".doubleValue()");
3505 }
3506 else if (colType.equals("Float")) {
3507 sm.append(".floatValue()");
3508 }
3509 else if (colType.equals("Integer")) {
3510 sm.append(".intValue()");
3511 }
3512 else if (colType.equals("Long")) {
3513 sm.append(".longValue()");
3514 }
3515 else if (colType.equals("Short")) {
3516 sm.append(".shortValue()");
3517 }
3518
3519 sm.append(");");
3520
3521 if (_requiresNullCheck(col)) {
3522 sm.append("}");
3523 }
3524 }
3525
3526 sm.append("List list = QueryUtil.list(q, getDialect(), begin, end);");
3527 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, list);");
3528 sm.append("return list;");
3529 sm.append("}");
3530 sm.append("catch (Exception e) {");
3531 sm.append("throw HibernateUtil.processException(e);");
3532 sm.append("}");
3533 sm.append("finally {");
3534 sm.append("closeSession(session);");
3535 sm.append("}");
3536 sm.append("}");
3537 sm.append("else {");
3538 sm.append("return (List)result;");
3539 sm.append("}");
3540 sm.append("}");
3541
3542 sm.append("public " + entity.getName() + " findBy" + finder.getName() + "_First(");
3543
3544 for (int j = 0; j < finderColsList.size(); j++) {
3545 EntityColumn col = (EntityColumn)finderColsList.get(j);
3546
3547 sm.append(col.getType() + " " + col.getName() + ", ");
3548 }
3549
3550 sm.append("OrderByComparator obc) throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
3551
3552 sm.append("List list = findBy" + finder.getName() + "(");
3553
3554 for (int j = 0; j < finderColsList.size(); j++) {
3555 EntityColumn col = (EntityColumn)finderColsList.get(j);
3556
3557 sm.append("" + col.getName() + ", ");
3558 }
3559
3560 sm.append("0, 1, obc);");
3561
3562 sm.append("if (list.size() == 0) {");
3563 sm.append("StringMaker msg = new StringMaker();");
3564 sm.append("msg.append(\"No " + entity.getName() + " exists with the key \");");
3565
3566 for (int j = 0; j < finderColsList.size(); j++) {
3567 EntityColumn col = (EntityColumn)finderColsList.get(j);
3568
3569 if (j == 0) {
3570 sm.append("msg.append(StringPool.OPEN_CURLY_BRACE);");
3571 }
3572
3573 sm.append("msg.append(\"" + col.getName() + "=\");");
3574 sm.append("msg.append(" + col.getName() + ");");
3575
3576 if ((j + 1) != finderColsList.size()) {
3577 sm.append("msg.append(\", \");");
3578 }
3579
3580 if ((j + 1) == finderColsList.size()) {
3581 sm.append("msg.append(StringPool.CLOSE_CURLY_BRACE);");
3582 }
3583 }
3584
3585 sm.append("throw new " + _getNoSuchEntityException(entity) + "Exception(msg.toString());"); sm.append("}");
3586 sm.append("else {");
3587 sm.append("return (" + entity.getName() + ")list.get(0);");
3588 sm.append("}");
3589 sm.append("}");
3590
3591 sm.append("public " + entity.getName() + " findBy" + finder.getName() + "_Last(");
3592
3593 for (int j = 0; j < finderColsList.size(); j++) {
3594 EntityColumn col = (EntityColumn)finderColsList.get(j);
3595
3596 sm.append(col.getType() + " " + col.getName() + ", ");
3597 }
3598
3599 sm.append("OrderByComparator obc) throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
3600
3601 sm.append("int count = countBy" + finder.getName() + "(");
3602
3603 for (int j = 0; j < finderColsList.size(); j++) {
3604 EntityColumn col = (EntityColumn)finderColsList.get(j);
3605
3606 sm.append("" + col.getName());
3607
3608 if ((j + 1) != finderColsList.size()) {
3609 sm.append(", ");
3610 }
3611 }
3612
3613 sm.append(");");
3614
3615 sm.append("List list = findBy" + finder.getName() + "(");
3616
3617 for (int j = 0; j < finderColsList.size(); j++) {
3618 EntityColumn col = (EntityColumn)finderColsList.get(j);
3619
3620 sm.append("" + col.getName() + ", ");
3621 }
3622
3623 sm.append("count - 1, count, obc);");
3624
3625 sm.append("if (list.size() == 0) {");
3626 sm.append("StringMaker msg = new StringMaker();");
3627 sm.append("msg.append(\"No " + entity.getName() + " exists with the key \");");
3628
3629 for (int j = 0; j < finderColsList.size(); j++) {
3630 EntityColumn col = (EntityColumn)finderColsList.get(j);
3631
3632 if (j == 0) {
3633 sm.append("msg.append(StringPool.OPEN_CURLY_BRACE);");
3634 }
3635
3636 sm.append("msg.append(\"" + col.getName() + "=\");");
3637 sm.append("msg.append(" + col.getName() + ");");
3638
3639 if ((j + 1) != finderColsList.size()) {
3640 sm.append("msg.append(\", \");");
3641 }
3642
3643 if ((j + 1) == finderColsList.size()) {
3644 sm.append("msg.append(StringPool.CLOSE_CURLY_BRACE);");
3645 }
3646 }
3647
3648 sm.append("throw new " + _getNoSuchEntityException(entity) + "Exception(msg.toString());");
3649 sm.append("}");
3650 sm.append("else {");
3651 sm.append("return (" + entity.getName() + ")list.get(0);");
3652 sm.append("}");
3653 sm.append("}");
3654
3655 sm.append("public " + entity.getName() + "[] findBy" + finder.getName() + "_PrevAndNext(" + pkClassName + " " + pkVarName + ", ");
3656
3657 for (int j = 0; j < finderColsList.size(); j++) {
3658 EntityColumn col = (EntityColumn)finderColsList.get(j);
3659
3660 sm.append(col.getType() + " " + col.getName() + ", ");
3661 }
3662
3663 sm.append("OrderByComparator obc) throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
3664 sm.append(entity.getName() + " " + entity.getVarName() + " = findByPrimaryKey(" + pkVarName + ");");
3665
3666 sm.append("int count = countBy" + finder.getName() + "(");
3667
3668 for (int j = 0; j < finderColsList.size(); j++) {
3669 EntityColumn col = (EntityColumn)finderColsList.get(j);
3670
3671 sm.append("" + col.getName());
3672
3673 if ((j + 1) != finderColsList.size()) {
3674 sm.append(", ");
3675 }
3676 }
3677
3678 sm.append(");");
3679
3680 sm.append("Session session = null;");
3681 sm.append("try {");
3682 sm.append("session = openSession();");
3683 sm.append("StringMaker query = new StringMaker();");
3684 sm.append("query.append(\"FROM " + _packagePath + ".model." + entity.getName() + " WHERE \");");
3685
3686 for (int j = 0; j < finderColsList.size(); j++) {
3687 EntityColumn col = (EntityColumn)finderColsList.get(j);
3688
3689 if (_requiresNullCheck(col)) {
3690 _appendNullLogic(col, sm);
3691 }
3692
3693 sm.append("query.append(\"" + col.getDBName() + " " + col.getComparator() + " ?\");");
3694
3695 if (_requiresNullCheck(col)) {
3696 sm.append("}");
3697 }
3698
3699 if ((j + 1) != finderColsList.size()) {
3700 sm.append("query.append(\" AND \");");
3701 }
3702 else if (Validator.isNull(finder.getWhere())) {
3703 sm.append("query.append(\" \");");
3704 }
3705 else {
3706 sm.append("query.append(\" AND " + finder.getWhere() + " \");");
3707 }
3708 }
3709
3710 sm.append("if (obc != null) {");
3711 sm.append("query.append(\"ORDER BY \");");
3712 sm.append("query.append(obc.getOrderBy());");
3713 sm.append("}");
3714
3715 if (order != null) {
3716 List orderList = order.getColumns();
3717
3718 sm.append("else {");
3719 sm.append("query.append(\"ORDER BY \");");
3720
3721 for (int j = 0; j < orderList.size(); j++) {
3722 EntityColumn col = (EntityColumn)orderList.get(j);
3723
3724 sm.append("query.append(\"" + col.getDBName() + " " + (col.isOrderByAscending() ? "ASC" : "DESC") + "\")");
3725
3726 if ((j + 1) != orderList.size()) {
3727 sm.append(".append(\", \");");
3728 }
3729 else {
3730 sm.append(";");
3731 }
3732 }
3733
3734 sm.append("}");
3735 }
3736
3737 sm.append("Query q = session.createQuery(query.toString());");
3738 sm.append("int queryPos = 0;");
3739
3740 for (int j = 0; j < finderColsList.size(); j++) {
3741 EntityColumn col = (EntityColumn)finderColsList.get(j);
3742
3743 if (_requiresNullCheck(col)) {
3744 sm.append("if (" + col.getName() + " != null) {");
3745 }
3746
3747 String colType = col.getType();
3748 String colObjType = colType;
3749
3750 if (col.isPrimitiveType()) {
3751 colObjType = _getPrimitiveObj(colType);
3752 }
3753
3754 sm.append("q.set" + colObjType + "(queryPos++, " + col.getName());
3755
3756 if (colType.equals("Boolean")) {
3757 sm.append(".booleanValue()");
3758 }
3759 else if (colType.equals("Double")) {
3760 sm.append(".doubleValue()");
3761 }
3762 else if (colType.equals("Float")) {
3763 sm.append(".floatValue()");
3764 }
3765 else if (colType.equals("Integer")) {
3766 sm.append(".intValue()");
3767 }
3768 else if (colType.equals("Long")) {
3769 sm.append(".longValue()");
3770 }
3771 else if (colType.equals("Short")) {
3772 sm.append(".shortValue()");
3773 }
3774
3775 sm.append(");");
3776
3777 if (_requiresNullCheck(col)) {
3778 sm.append("}");
3779 }
3780 }
3781
3782 sm.append("Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, " + entity.getVarName() + ");");
3783
3784 sm.append(entity.getName() + "[] array = new " + entity.getName() + "Impl[3];");
3785
3786 sm.append("array[0] = (" + entity.getName() + ")objArray[0];");
3787 sm.append("array[1] = (" + entity.getName() + ")objArray[1];");
3788 sm.append("array[2] = (" + entity.getName() + ")objArray[2];");
3789
3790 sm.append("return array;");
3791 sm.append("}");
3792 sm.append("catch (Exception e) {");
3793 sm.append("throw HibernateUtil.processException(e);");
3794 sm.append("}");
3795 sm.append("finally {");
3796 sm.append("closeSession(session);");
3797 sm.append("}");
3798 sm.append("}");
3799 }
3800 }
3801
3802 sm.append("public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer) throws SystemException {");
3803 sm.append("Session session = null;");
3804 sm.append("try {");
3805 sm.append("session = openSession();");
3806 sm.append("DynamicQuery query = queryInitializer.initialize(session);");
3807 sm.append("return query.list();");
3808 sm.append("}");
3809 sm.append("catch (Exception e) {");
3810 sm.append("throw HibernateUtil.processException(e);");
3811 sm.append("}");
3812 sm.append("finally {");
3813 sm.append("closeSession(session);");
3814 sm.append("}");
3815 sm.append("}");
3816
3817 sm.append("public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer, int begin, int end) throws SystemException {");
3818 sm.append("Session session = null;");
3819 sm.append("try {");
3820 sm.append("session = openSession();");
3821 sm.append("DynamicQuery query = queryInitializer.initialize(session);");
3822 sm.append("query.setLimit(begin, end);");
3823 sm.append("return query.list();");
3824 sm.append("}");
3825 sm.append("catch (Exception e) {");
3826 sm.append("throw HibernateUtil.processException(e);");
3827 sm.append("}");
3828 sm.append("finally {");
3829 sm.append("closeSession(session);");
3830 sm.append("}");
3831 sm.append("}");
3832
3833 sm.append("public List findAll() throws SystemException {");
3834 sm.append("return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);");
3835 sm.append("}");
3836
3837 sm.append("public List findAll(int begin, int end) throws SystemException {");
3838 sm.append("return findAll(begin, end, null);");
3839 sm.append("}");
3840
3841 sm.append("public List findAll(int begin, int end, OrderByComparator obc) throws SystemException {");
3842 sm.append("String finderClassName = " + entity.getName() + ".class.getName();");
3843 sm.append("String finderMethodName = \"findAll\";");
3844 sm.append("String finderParams[] = new String[] {");
3845 sm.append("\"java.lang.Integer\", \"java.lang.Integer\", \"com.liferay.portal.kernel.util.OrderByComparator\"");
3846 sm.append("};");
3847 sm.append("Object finderArgs[] = new Object[] {");
3848 sm.append("String.valueOf(begin), String.valueOf(end), String.valueOf(obc)");
3849 sm.append("};");
3850 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
3851 sm.append("if (result == null) {");
3852 sm.append("Session session = null;");
3853 sm.append("try {");
3854 sm.append("session = openSession();");
3855 sm.append("StringMaker query = new StringMaker();");
3856 sm.append("query.append(\"FROM " + _packagePath + ".model." + entity.getName() + " \");");
3857
3858 sm.append("if (obc != null) {");
3859 sm.append("query.append(\"ORDER BY \");");
3860 sm.append("query.append(obc.getOrderBy());");
3861 sm.append("}");
3862
3863 EntityOrder order = entity.getOrder();
3864
3865 if (order != null) {
3866 List orderList = order.getColumns();
3867
3868 sm.append("else {");
3869 sm.append("query.append(\"ORDER BY \");");
3870
3871 for (int j = 0; j < orderList.size(); j++) {
3872 EntityColumn col = (EntityColumn)orderList.get(j);
3873
3874 sm.append("query.append(\"" + col.getDBName() + " " + (col.isOrderByAscending() ? "ASC" : "DESC") + "\")");
3875
3876 if ((j + 1) != orderList.size()) {
3877 sm.append(".append(\", \");");
3878 }
3879 else {
3880 sm.append(";");
3881 }
3882 }
3883
3884 sm.append("}");
3885 }
3886
3887 sm.append("Query q = session.createQuery(query.toString());");
3888 sm.append("List list = QueryUtil.list(q, getDialect(), begin, end);");
3889 sm.append("if (obc == null) {");
3890 sm.append("Collections.sort(list);");
3891 sm.append("}");
3892 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, list);");
3893 sm.append("return list;");
3894 sm.append("}");
3895 sm.append("catch (Exception e) {");
3896 sm.append("throw HibernateUtil.processException(e);");
3897 sm.append("}");
3898 sm.append("finally {");
3899 sm.append("closeSession(session);");
3900 sm.append("}");
3901 sm.append("}");
3902 sm.append("else {");
3903 sm.append("return (List)result;");
3904 sm.append("}");
3905 sm.append("}");
3906
3907
3909 for (int i = 0; i < finderList.size(); i++) {
3910 EntityFinder finder = (EntityFinder)finderList.get(i);
3911
3912 List finderColsList = finder.getColumns();
3913
3914 if (!finder.isCollection()) {
3915 sm.append("public void removeBy" + finder.getName() + "(");
3916
3917 for (int j = 0; j < finderColsList.size(); j++) {
3918 EntityColumn col = (EntityColumn)finderColsList.get(j);
3919
3920 sm.append(col.getType() + " " + col.getName());
3921
3922 if ((j + 1) != finderColsList.size()) {
3923 sm.append(", ");
3924 }
3925 }
3926 sm.append(") throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
3927 sm.append(entity.getName() + " " + entity.getVarName() + " = findBy" + finder.getName() + "(");
3928
3929 for (int j = 0; j < finderColsList.size(); j++) {
3930 EntityColumn col = (EntityColumn)finderColsList.get(j);
3931
3932 sm.append(col.getName());
3933
3934 if ((j + 1) != finderColsList.size()) {
3935 sm.append(", ");
3936 }
3937 }
3938
3939 sm.append(");");
3940 sm.append("remove(" + entity.getVarName() + ");");
3941 sm.append("}");
3942 }
3943 else {
3944 sm.append("public void removeBy" + finder.getName() + "(");
3945
3946 for (int j = 0; j < finderColsList.size(); j++) {
3947 EntityColumn col = (EntityColumn)finderColsList.get(j);
3948
3949 sm.append(col.getType() + " " + col.getName());
3950
3951 if ((j + 1) != finderColsList.size()) {
3952 sm.append(", ");
3953 }
3954 }
3955
3956 sm.append(") throws SystemException {");
3957 sm.append("Iterator itr = findBy" + finder.getName() + "(");
3958
3959 for (int j = 0; j < finderColsList.size(); j++) {
3960 EntityColumn col = (EntityColumn)finderColsList.get(j);
3961
3962 sm.append(col.getName());
3963
3964 if ((j + 1) != finderColsList.size()) {
3965 sm.append(", ");
3966 }
3967 }
3968
3969 sm.append(").iterator();");
3970 sm.append("while (itr.hasNext()) {");
3971 sm.append(entity.getName() + " " + entity.getVarName() + " = (" + entity.getName() + ")itr.next();");
3972 sm.append("remove(" + entity.getVarName() + ");");
3973 sm.append("}");
3974 sm.append("}");
3975 }
3976 }
3977
3978 sm.append("public void removeAll() throws SystemException {");
3979 sm.append("Iterator itr = findAll().iterator();");
3980 sm.append("while (itr.hasNext()) {");
3981 sm.append("remove((" + entity.getName() + ")itr.next());");
3982 sm.append("}");
3983 sm.append("}");
3984
3985
3987 for (int i = 0; i < finderList.size(); i++) {
3988 EntityFinder finder = (EntityFinder)finderList.get(i);
3989
3990 List finderColsList = finder.getColumns();
3991
3992 sm.append("public int countBy" + finder.getName() + "(");
3993
3994 for (int j = 0; j < finderColsList.size(); j++) {
3995 EntityColumn col = (EntityColumn)finderColsList.get(j);
3996
3997 sm.append(col.getType() + " " + col.getName());
3998
3999 if ((j + 1) != finderColsList.size()) {
4000 sm.append(", ");
4001 }
4002 }
4003
4004 sm.append(") throws SystemException {");
4005 sm.append("String finderClassName = " + entity.getName() + ".class.getName();");
4006 sm.append("String finderMethodName = \"countBy" + finder.getName() + "\";");
4007 sm.append("String finderParams[] = new String[] {");
4008
4009 for (int j = 0; j < finderColsList.size(); j++) {
4010 EntityColumn col = (EntityColumn)finderColsList.get(j);
4011
4012 if (col.isPrimitiveType()) {
4013 sm.append(_getPrimitiveObj(col.getType()));
4014 }
4015 else {
4016 sm.append(col.getType());
4017 }
4018
4019 sm.append(".class.getName()");
4020
4021 if ((j + 1) != finderColsList.size()) {
4022 sm.append(", ");
4023 }
4024 }
4025
4026 sm.append("};");
4027 sm.append("Object finderArgs[] = new Object[] {");
4028
4029 for (int j = 0; j < finderColsList.size(); j++) {
4030 EntityColumn col = (EntityColumn)finderColsList.get(j);
4031
4032 if (col.isPrimitiveType()) {
4033 if (col.getType().equals("boolean")) {
4034 sm.append(_getPrimitiveObj(col.getType()) + ".valueOf(");
4035 }
4036 else {
4037 sm.append("new " + _getPrimitiveObj(col.getType()) + "(");
4038 }
4039 }
4040
4041 sm.append(col.getName());
4042
4043 if (col.isPrimitiveType()) {
4044 sm.append(")");
4045 }
4046
4047 if ((j + 1) != finderColsList.size()) {
4048 sm.append(", ");
4049 }
4050 }
4051
4052 sm.append("};");
4053 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
4054 sm.append("if (result == null) {");
4055 sm.append("Session session = null;");
4056 sm.append("try {");
4057 sm.append("session = openSession();");
4058 sm.append("StringMaker query = new StringMaker();");
4059 sm.append("query.append(\"SELECT COUNT(*) \");");
4060 sm.append("query.append(\"FROM " + _packagePath + ".model." + entity.getName() + " WHERE \");");
4061
4062 for (int j = 0; j < finderColsList.size(); j++) {
4063 EntityColumn col = (EntityColumn)finderColsList.get(j);
4064
4065 if (_requiresNullCheck(col)) {
4066 _appendNullLogic(col, sm);
4067 }
4068
4069 sm.append("query.append(\"" + col.getDBName() + " " + col.getComparator() + " ?\");");
4070
4071 if (_requiresNullCheck(col)) {
4072 sm.append("}");
4073 }
4074
4075 if ((j + 1) != finderColsList.size()) {
4076 sm.append("query.append(\" AND \");");
4077 }
4078 else if (Validator.isNull(finder.getWhere())) {
4079 sm.append("query.append(\" \");");
4080 }
4081 else {
4082 sm.append("query.append(\" AND " + finder.getWhere() + " \");");
4083 }
4084 }
4085
4086 sm.append("Query q = session.createQuery(query.toString());");
4087 sm.append("int queryPos = 0;");
4088
4089 for (int j = 0; j < finderColsList.size(); j++) {
4090 EntityColumn col = (EntityColumn)finderColsList.get(j);
4091
4092 if (_requiresNullCheck(col)) {
4093 sm.append("if (" + col.getName() + " != null) {");
4094 }
4095
4096 String colType = col.getType();
4097 String colObjType = colType;
4098
4099 if (col.isPrimitiveType()) {
4100 colObjType = _getPrimitiveObj(colType);
4101 }
4102
4103 sm.append("q.set" + colObjType + "(queryPos++, " + col.getName());
4104
4105 if (colType.equals("Boolean")) {
4106 sm.append(".booleanValue()");
4107 }
4108 else if (colType.equals("Double")) {
4109 sm.append(".doubleValue()");
4110 }
4111 else if (colType.equals("Float")) {
4112 sm.append(".floatValue()");
4113 }
4114 else if (colType.equals("Integer")) {
4115 sm.append(".intValue()");
4116 }
4117 else if (colType.equals("Long")) {
4118 sm.append(".longValue()");
4119 }
4120 else if (colType.equals("Short")) {
4121 sm.append(".shortValue()");
4122 }
4123
4124 sm.append(");");
4125
4126 if (_requiresNullCheck(col)) {
4127 sm.append("}");
4128 }
4129 }
4130
4131 sm.append("Long count = null;");
4132 sm.append("Iterator itr = q.list().iterator();");
4133 sm.append("if (itr.hasNext()) {");
4134 sm.append("count = (Long)itr.next();");
4135 sm.append("}");
4136 sm.append("if (count == null) {");
4137 sm.append("count = new Long(0);");
4138 sm.append("}");
4139 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, count);");
4140 sm.append("return count.intValue();");
4141 sm.append("}");
4142 sm.append("catch (Exception e) {");
4143 sm.append("throw HibernateUtil.processException(e);");
4144 sm.append("}");
4145 sm.append("finally {");
4146 sm.append("closeSession(session);");
4147 sm.append("}");
4148 sm.append("}");
4149 sm.append("else {");
4150 sm.append("return ((Long)result).intValue();");
4151 sm.append("}");
4152 sm.append("}");
4153 }
4154
4155 sm.append("public int countAll() throws SystemException {");
4156 sm.append("String finderClassName = " + entity.getName() + ".class.getName();");
4157 sm.append("String finderMethodName = \"countAll\";");
4158 sm.append("String finderParams[] = new String[] {};");
4159 sm.append("Object finderArgs[] = new Object[] {};");
4160 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
4161 sm.append("if (result == null) {");
4162 sm.append("Session session = null;");
4163 sm.append("try {");
4164 sm.append("session = openSession();");
4165 sm.append("StringMaker query = new StringMaker();");
4166 sm.append("query.append(\"SELECT COUNT(*) \");");
4167 sm.append("query.append(\"FROM " + _packagePath + ".model." + entity.getName() + "\");");
4168 sm.append("Query q = session.createQuery(query.toString());");
4169 sm.append("Long count = null;");
4170 sm.append("Iterator itr = q.list().iterator();");
4171 sm.append("if (itr.hasNext()) {");
4172 sm.append("count = (Long)itr.next();");
4173 sm.append("}");
4174 sm.append("if (count == null) {");
4175 sm.append("count = new Long(0);");
4176 sm.append("}");
4177 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, count);");
4178 sm.append("return count.intValue();");
4179 sm.append("}");
4180 sm.append("catch (Exception e) {");
4181 sm.append("throw HibernateUtil.processException(e);");
4182 sm.append("}");
4183 sm.append("finally {");
4184 sm.append("closeSession(session);");
4185 sm.append("}");
4186 sm.append("}");
4187 sm.append("else {");
4188 sm.append("return ((Long)result).intValue();");
4189 sm.append("}");
4190 sm.append("}");
4191
4192
4194 for (int i = 0; i < columnList.size(); i++) {
4195 EntityColumn col = (EntityColumn)columnList.get(i);
4196
4197 if ((col.isCollection()) &&
4198 (col.isMappingManyToMany() || col.isMappingOneToMany())) {
4199
4200 Entity tempEntity = getEntity(col.getEJBName());
4201 EntityOrder tempOrder = tempEntity.getOrder();
4202
4203
4205 sm.append("public List get" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk) throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
4206 sm.append("return get" + tempEntity.getNames() + "(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);");
4207 sm.append("}");
4208
4209
4211 sm.append("public List get" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, int begin, int end) throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
4212 sm.append("return get" + tempEntity.getNames() + "(pk, begin, end, null);");
4213 sm.append("}");
4214
4215
4217 sm.append("public List get" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, int begin, int end, OrderByComparator obc) throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
4218 sm.append("String finderClassName = \"" + col.getMappingTable() + "\";");
4219 sm.append("String finderMethodName = \"get" + tempEntity.getNames() + "\";");
4220 sm.append("String finderParams[] = new String[] {");
4221
4222 if (entity.hasPrimitivePK()) {
4223 sm.append(_getPrimitiveObj(entity.getPKClassName()));
4224 }
4225 else {
4226 sm.append(entity.getPKClassName());
4227 }
4228
4229 sm.append(".class.getName(), \"java.lang.Integer\", \"java.lang.Integer\", \"com.liferay.portal.kernel.util.OrderByComparator\"");
4230 sm.append("};");
4231 sm.append("Object finderArgs[] = new Object[] {");
4232
4233 if (entity.hasPrimitivePK()) {
4234 if (entity.getPKClassName().equals("boolean")) {
4235 sm.append(_getPrimitiveObj(entity.getPKClassName()) + ".valueOf(");
4236 }
4237 else {
4238 sm.append("new " + _getPrimitiveObj(entity.getPKClassName()) + "(");
4239 }
4240 }
4241
4242 sm.append("pk");
4243
4244 if (entity.hasPrimitivePK()) {
4245 sm.append(")");
4246 }
4247
4248 sm.append(", String.valueOf(begin), String.valueOf(end), String.valueOf(obc)");
4249 sm.append("};");
4250 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
4251 sm.append("if (result == null) {");
4252 sm.append("Session session = null;");
4253 sm.append("try {");
4254 sm.append("session = HibernateUtil.openSession();");
4255 sm.append("StringMaker sm = new StringMaker();");
4256 sm.append("sm.append(_SQL_GET" + tempEntity.getName().toUpperCase() + "S);");
4257
4258 sm.append("if (obc != null) {");
4259 sm.append("sm.append(\"ORDER BY \");");
4260 sm.append("sm.append(obc.getOrderBy());");
4261 sm.append("}");
4262
4263 if (tempOrder != null) {
4264 List tempOrderList = tempOrder.getColumns();
4265
4266 sm.append("else {");
4267 sm.append("sm.append(\"ORDER BY \");");
4268
4269 for (int j = 0; j < tempOrderList.size(); j++) {
4270 EntityColumn tempOrderCol = (EntityColumn)tempOrderList.get(j);
4271
4272 sm.append("sm.append(\"" + tempEntity.getTable() + "." + tempOrderCol.getDBName() + " " + (tempOrderCol.isOrderByAscending() ? "ASC" : "DESC") + "\");");
4273
4274 if ((j + 1) != tempOrderList.size()) {
4275 sm.append("sm.append(\", \");");
4276 }
4277 }
4278
4279 sm.append("}");
4280 }
4281
4282 sm.append("String sql = sm.toString();");
4283 sm.append("SQLQuery q = session.createSQLQuery(sql);");
4284 sm.append("q.addEntity(\"" + tempEntity.getTable() + "\", " + tempEntity.getPackagePath() + ".model.impl." + tempEntity.getName() + "Impl.class);");
4285 sm.append("QueryPos qPos = QueryPos.getInstance(q);");
4286 sm.append("qPos.add(pk);");
4287 sm.append("List list = QueryUtil.list(q, getDialect(), begin, end);");
4288 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, list);");
4289 sm.append("return list;");
4290 sm.append("}");
4291 sm.append("catch (Exception e) {");
4292 sm.append("throw new SystemException(e);");
4293 sm.append("}");
4294 sm.append("finally {");
4295 sm.append("closeSession(session);");
4296 sm.append("}");
4297 sm.append("}");
4298 sm.append("else {");
4299 sm.append("return (List)result;");
4300 sm.append("}");
4301 sm.append("}");
4302
4303
4305 sm.append("public int get" + tempEntity.getNames() + "Size(" + entity.getPKClassName() + " pk) throws SystemException {");
4306 sm.append("String finderClassName = \"" + col.getMappingTable() + "\";");
4307 sm.append("String finderMethodName = \"get" + tempEntity.getNames() + "Size\";");
4308 sm.append("String finderParams[] = new String[] {");
4309
4310 if (entity.hasPrimitivePK()) {
4311 sm.append(_getPrimitiveObj(entity.getPKClassName()));
4312 }
4313 else {
4314 sm.append(entity.getPKClassName());
4315 }
4316
4317 sm.append(".class.getName()");
4318 sm.append("};");
4319 sm.append("Object finderArgs[] = new Object[] {");
4320
4321 if (entity.hasPrimitivePK()) {
4322 if (entity.getPKClassName().equals("boolean")) {
4323 sm.append(_getPrimitiveObj(entity.getPKClassName()) + ".valueOf(");
4324 }
4325 else {
4326 sm.append("new " + _getPrimitiveObj(entity.getPKClassName()) + "(");
4327 }
4328 }
4329
4330 sm.append("pk");
4331
4332 if (entity.hasPrimitivePK()) {
4333 sm.append(")");
4334 }
4335
4336 sm.append("};");
4337 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
4338 sm.append("if (result == null) {");
4339 sm.append("Session session = null;");
4340 sm.append("try {");
4341 sm.append("session = openSession();");
4342 sm.append("SQLQuery q = session.createSQLQuery(_SQL_GET" + tempEntity.getName().toUpperCase() + "SSIZE);");
4343 sm.append("q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);");
4344 sm.append("QueryPos qPos = QueryPos.getInstance(q);");
4345 sm.append("qPos.add(pk);");
4346 sm.append("Long count = null;");
4347 sm.append("Iterator itr = q.list().iterator();");
4348 sm.append("if (itr.hasNext()) {");
4349 sm.append("count = (Long)itr.next();");
4350 sm.append("}");
4351 sm.append("if (count == null) {");
4352 sm.append("count = new Long(0);");
4353 sm.append("}");
4354 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, count);");
4355 sm.append("return count.intValue();");
4356 sm.append("}");
4357 sm.append("catch (Exception e) {");
4358 sm.append("throw HibernateUtil.processException(e);");
4359 sm.append("}");
4360 sm.append("finally {");
4361 sm.append("closeSession(session);");
4362 sm.append("}");
4363 sm.append("}");
4364 sm.append("else {");
4365 sm.append("return ((Long)result).intValue();");
4366 sm.append("}");
4367 sm.append("}");
4368
4369
4371 sm.append("public boolean contains" + tempEntity.getName() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPKClassName() + " " + tempEntity.getVarName() + "PK) throws SystemException {");
4372 sm.append("String finderClassName = \"" + col.getMappingTable() + "\";");
4373 sm.append("String finderMethodName = \"contains" + tempEntity.getNames() + "\";");
4374 sm.append("String finderParams[] = new String[] {");
4375
4376 if (entity.hasPrimitivePK()) {
4377 sm.append(_getPrimitiveObj(entity.getPKClassName()));
4378 }
4379 else {
4380 sm.append(entity.getPKClassName());
4381 }
4382
4383 sm.append(".class.getName(), ");
4384
4385 if (tempEntity.hasPrimitivePK()) {
4386 sm.append(_getPrimitiveObj(tempEntity.getPKClassName()));
4387 }
4388 else {
4389 sm.append(tempEntity.getPKClassName());
4390 }
4391
4392 sm.append(".class.getName()");
4393 sm.append("};");
4394 sm.append("Object finderArgs[] = new Object[] {");
4395
4396 if (entity.hasPrimitivePK()) {
4397 if (entity.getPKClassName().equals("boolean")) {
4398 sm.append(_getPrimitiveObj(entity.getPKClassName()) + ".valueOf(");
4399 }
4400 else {
4401 sm.append("new " + _getPrimitiveObj(entity.getPKClassName()) + "(");
4402 }
4403 }
4404
4405 sm.append("pk");
4406
4407 if (entity.hasPrimitivePK()) {
4408 sm.append(")");
4409 }
4410
4411 sm.append(", ");
4412
4413 if (tempEntity.hasPrimitivePK()) {
4414 sm.append("new " + _getPrimitiveObj(entity.getPKClassName()) + "(");
4415 }
4416
4417 sm.append(tempEntity.getVarName() + "PK");
4418
4419 if (tempEntity.hasPrimitivePK()) {
4420 sm.append(")");
4421 }
4422
4423 sm.append("};");
4424 sm.append("Object result = FinderCache.getResult(finderClassName, finderMethodName, finderParams, finderArgs, getSessionFactory());");
4425 sm.append("if (result == null) {");
4426 sm.append("try {");
4427 sm.append("Boolean value = Boolean.valueOf(contains" + tempEntity.getName() + ".contains(pk, " + tempEntity.getVarName() + "PK));");
4428 sm.append("FinderCache.putResult(finderClassName, finderMethodName, finderParams, finderArgs, value);");
4429 sm.append("return value.booleanValue();");
4430 sm.append("}");
4431 sm.append("catch (DataAccessException dae) {");
4432 sm.append("throw new SystemException(dae);");
4433 sm.append("}");
4434 sm.append("}");
4435 sm.append("else {");
4436 sm.append("return ((Boolean)result).booleanValue();");
4437 sm.append("}");
4438 sm.append("}");
4439
4440
4442 sm.append("public boolean contains" + tempEntity.getName() + "s(" + entity.getPKClassName() + " pk) throws SystemException {");
4443 sm.append("if (get" + tempEntity.getNames() + "Size(pk) > 0) {");
4444 sm.append("return true;");
4445 sm.append("}");
4446 sm.append("else {");
4447 sm.append("return false;");
4448 sm.append("}");
4449 sm.append("}");
4450
4451 if (col.isMappingManyToMany()) {
4452
4453
4455 sm.append("public void add" + tempEntity.getName() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPKClassName() + " " + tempEntity.getVarName() + "PK) throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4456 sm.append("try {");
4457 sm.append("add" + tempEntity.getName() + ".add(pk, " + tempEntity.getVarName() + "PK);");
4458 sm.append("}");
4459 sm.append("catch (DataAccessException dae) {");
4460 sm.append("throw new SystemException(dae);");
4461 sm.append("}");
4462 sm.append("finally {");
4463 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4464 sm.append("}");
4465 sm.append("}");
4466
4467
4469 sm.append("public void add" + tempEntity.getName() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPackagePath() + ".model." + tempEntity.getName() + " " + tempEntity.getVarName() + ") throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4470 sm.append("try {");
4471 sm.append("add" + tempEntity.getName() + ".add(pk, " + tempEntity.getVarName() + ".getPrimaryKey());");
4472 sm.append("}");
4473 sm.append("catch (DataAccessException dae) {");
4474 sm.append("throw new SystemException(dae);");
4475 sm.append("}");
4476 sm.append("finally {");
4477 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4478 sm.append("}");
4479 sm.append("}");
4480
4481
4483 sm.append("public void add" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPKClassName() + "[] " + tempEntity.getVarName() + "PKs) throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4484 sm.append("try {");
4485 sm.append("for (int i = 0; i < " + tempEntity.getVarName() + "PKs.length; i++) {");
4486 sm.append("add" + tempEntity.getName() + ".add(pk, " + tempEntity.getVarName() + "PKs[i]);");
4487 sm.append("}");
4488 sm.append("}");
4489 sm.append("catch (DataAccessException dae) {");
4490 sm.append("throw new SystemException(dae);");
4491 sm.append("}");
4492 sm.append("finally {");
4493 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4494 sm.append("}");
4495 sm.append("}");
4496
4497
4499 sm.append("public void add" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, List " + tempEntity.getVarNames() + ") throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4500 sm.append("try {");
4501 sm.append("for (int i = 0; i < " + tempEntity.getVarNames() + ".size(); i++) {");
4502 sm.append(tempEntity.getPackagePath() + ".model." + tempEntity.getName() + " " + tempEntity.getVarName() + " = (" + tempEntity.getPackagePath() + ".model." + tempEntity.getName() + ")" + tempEntity.getVarNames() + ".get(i);");
4503 sm.append("add" + tempEntity.getName() + ".add(pk, " + tempEntity.getVarName() + ".getPrimaryKey());");
4504 sm.append("}");
4505 sm.append("}");
4506 sm.append("catch (DataAccessException dae) {");
4507 sm.append("throw new SystemException(dae);");
4508 sm.append("}");
4509 sm.append("finally {");
4510 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4511 sm.append("}");
4512 sm.append("}");
4513
4514
4516 sm.append("public void clear" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk) throws " + _getNoSuchEntityException(entity) + "Exception, SystemException {");
4517 sm.append("try {");
4518 sm.append("clear" + tempEntity.getNames() + ".clear(pk);");
4519 sm.append("}");
4520 sm.append("catch (DataAccessException dae) {");
4521 sm.append("throw new SystemException(dae);");
4522 sm.append("}");
4523 sm.append("finally {");
4524 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4525 sm.append("}");
4526 sm.append("}");
4527
4528
4530 sm.append("public void remove" + tempEntity.getName() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPKClassName() + " " + tempEntity.getVarName() + "PK) throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4531 sm.append("try {");
4532 sm.append("remove" + tempEntity.getName() + ".remove(pk, " + tempEntity.getVarName() + "PK);");
4533 sm.append("}");
4534 sm.append("catch (DataAccessException dae) {");
4535 sm.append("throw new SystemException(dae);");
4536 sm.append("}");
4537 sm.append("finally {");
4538 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4539 sm.append("}");
4540 sm.append("}");
4541
4542
4544 sm.append("public void remove" + tempEntity.getName() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPackagePath() + ".model." + tempEntity.getName() + " " + tempEntity.getVarName() + ") throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4545 sm.append("try {");
4546 sm.append("remove" + tempEntity.getName() + ".remove(pk, " + tempEntity.getVarName() + ".getPrimaryKey());");
4547 sm.append("}");
4548 sm.append("catch (DataAccessException dae) {");
4549 sm.append("throw new SystemException(dae);");
4550 sm.append("}");
4551 sm.append("finally {");
4552 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4553 sm.append("}");
4554 sm.append("}");
4555
4556
4558 sm.append("public void remove" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPKClassName() + "[] " + tempEntity.getVarName() + "PKs) throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4559 sm.append("try {");
4560 sm.append("for (int i = 0; i < " + tempEntity.getVarName() + "PKs.length; i++) {");
4561 sm.append("remove" + tempEntity.getName() + ".remove(pk, " + tempEntity.getVarName() + "PKs[i]);");
4562 sm.append("}");
4563 sm.append("}");
4564 sm.append("catch (DataAccessException dae) {");
4565 sm.append("throw new SystemException(dae);");
4566 sm.append("}");
4567 sm.append("finally {");
4568 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4569 sm.append("}");
4570 sm.append("}");
4571
4572
4574 sm.append("public void remove" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, List " + tempEntity.getVarNames() + ") throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4575 sm.append("try {");
4576 sm.append("for (int i = 0; i < " + tempEntity.getVarNames() + ".size(); i++) {");
4577 sm.append(tempEntity.getPackagePath() + ".model." + tempEntity.getName() + " " + tempEntity.getVarName() + " = (" + tempEntity.getPackagePath() + ".model." + tempEntity.getName() + ")" + tempEntity.getVarNames() + ".get(i);");
4578 sm.append("remove" + tempEntity.getName() + ".remove(pk, " + tempEntity.getVarName() + ".getPrimaryKey());");
4579 sm.append("}");
4580 sm.append("}");
4581 sm.append("catch (DataAccessException dae) {");
4582 sm.append("throw new SystemException(dae);");
4583 sm.append("}");
4584 sm.append("finally {");
4585 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4586 sm.append("}");
4587 sm.append("}");
4588
4589
4591 sm.append("public void set" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, " + tempEntity.getPKClassName() + "[] " + tempEntity.getVarName() + "PKs) throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4592 sm.append("try {");
4593 sm.append("clear" + tempEntity.getNames() + ".clear(pk);");
4594 sm.append("for (int i = 0; i < " + tempEntity.getVarName() + "PKs.length; i++) {");
4595 sm.append("add" + tempEntity.getName() + ".add(pk, " + tempEntity.getVarName() + "PKs[i]);");
4596 sm.append("}");
4597 sm.append("}");
4598 sm.append("catch (DataAccessException dae) {");
4599 sm.append("throw new SystemException(dae);");
4600 sm.append("}");
4601 sm.append("finally {");
4602 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4603 sm.append("}");
4604 sm.append("}");
4605
4606
4608 sm.append("public void set" + tempEntity.getNames() + "(" + entity.getPKClassName() + " pk, List " + tempEntity.getVarNames() + ") throws " + _getNoSuchEntityException(entity) + "Exception, " + tempEntity.getPackagePath() + "." + _getNoSuchEntityException(tempEntity) + "Exception, SystemException {");
4609 sm.append("try {");
4610 sm.append("clear" + tempEntity.getNames() + ".clear(pk);");
4611 sm.append("for (int i = 0; i < " + tempEntity.getVarNames() + ".size(); i++) {");
4612 sm.append(tempEntity.getPackagePath() + ".model." + tempEntity.getName() + " " + tempEntity.getVarName() + " = (" + tempEntity.getPackagePath() + ".model." + tempEntity.getName() + ")" + tempEntity.getVarNames() + ".get(i);");
4613 sm.append("add" + tempEntity.getName() + ".add(pk, " + tempEntity.getVarName() + ".getPrimaryKey());");
4614 sm.append("}");
4615 sm.append("}");
4616 sm.append("catch (DataAccessException dae) {");
4617 sm.append("throw new SystemException(dae);");
4618 sm.append("}");
4619 sm.append("finally {");
4620 sm.append("FinderCache.clearCache(\"" + col.getMappingTable() + "\");");
4621 sm.append("}");
4622 sm.append("}");
4623 }
4624 }
4625 }
4626
4627 sm.append("protected void initDao() {");
4628
4629 for (int i = 0; i < columnList.size(); i++) {
4630 EntityColumn col = (EntityColumn)columnList.get(i);
4631
4632 if ((col.isCollection()) &&
4633 (col.isMappingManyToMany() || col.isMappingOneToMany())) {
4634
4635 Entity tempEntity = getEntity(col.getEJBName());
4636
4637
4639 sm.append("contains" + tempEntity.getName() + " = new Contains" + tempEntity.getName() + "(this);");
4640
4641 if (col.isMappingManyToMany()) {
4642
4643
4645 sm.append("add" + tempEntity.getName() + " = new Add" + tempEntity.getName() + "(this);");
4646
4647
4649 sm.append("clear" + tempEntity.getNames() + " = new Clear" + tempEntity.getNames() + "(this);");
4650
4651
4653 sm.append("remove" + tempEntity.getName() + " = new Remove" + tempEntity.getName() + "(this);");
4654 }
4655 }
4656 }
4657
4658 sm.append("}");
4659
4660 for (int i = 0; i < columnList.size(); i++) {
4661 EntityColumn col = (EntityColumn)columnList.get(i);
4662
4663 if ((col.isCollection()) &&
4664 (col.isMappingManyToMany() || col.isMappingOneToMany())) {
4665
4666 Entity tempEntity = getEntity(col.getEJBName());
4667
4668
4670 sm.append("protected Contains" + tempEntity.getName() + " contains" + tempEntity.getName() + ";");
4671
4672 if (col.isMappingManyToMany()) {
4673
4674
4676 sm.append("protected Add" + tempEntity.getName() + " add" + tempEntity.getName() + ";");
4677
4678
4680 sm.append("protected Clear" + tempEntity.getNames() + " clear" + tempEntity.getNames() + ";");
4681
4682
4684 sm.append("protected Remove" + tempEntity.getName() + " remove" + tempEntity.getName() + ";");
4685 }
4686 }
4687 }
4688
4689 for (int i = 0; i < columnList.size(); i++) {
4690 EntityColumn col = (EntityColumn)columnList.get(i);
4691
4692 if ((col.isCollection()) &&
4693 (col.isMappingManyToMany() || col.isMappingOneToMany())) {
4694
4695 Entity tempEntity = getEntity(col.getEJBName());
4696
4697 String entitySqlType = _getSqlType(_packagePath + ".model." + entity.getName(), entity.getPKVarName(), entity.getPKClassName());
4698
4699 String pkVarNameWrapper = pkVarName;
4700
4701 if (entity.hasPrimitivePK()) {
4702 pkVarNameWrapper = "new " + _getPrimitiveObj(entity.getPKClassName()) + "(" + pkVarName + ")";
4703 }
4704
4705 String tempEntitySqlType = _getSqlType(tempEntity.getPackagePath() + ".model." + entity.getName(), tempEntity.getPKVarName(), tempEntity.getPKClassName());
4706
4707 String tempEntityPkVarNameWrapper = tempEntity.getPKVarName();
4708
4709 if (tempEntity.hasPrimitivePK()) {
4710 tempEntityPkVarNameWrapper = "new " + _getPrimitiveObj(tempEntity.getPKClassName()) + "(" + tempEntityPkVarNameWrapper + ")";
4711 }
4712
4713
4715 sm.append("protected class Contains" + tempEntity.getName() + " extends MappingSqlQuery {");
4716 sm.append("protected Contains" + tempEntity.getName() + "(" + entity.getName() + "PersistenceImpl persistenceImpl) {");
4717 sm.append("super(persistenceImpl.getDataSource(), _SQL_CONTAINS" + tempEntity.getName().toUpperCase() + ");");
4718 sm.append("declareParameter(new SqlParameter(Types." + entitySqlType + "));");
4719 sm.append("declareParameter(new SqlParameter(Types." + tempEntitySqlType + "));");
4720 sm.append("compile();");
4721 sm.append("}");
4722 sm.append("protected Object mapRow(ResultSet rs, int rowNumber) throws SQLException {");
4723 sm.append("return new Integer(rs.getInt(\"COUNT_VALUE\"));");
4724 sm.append("}");
4725 sm.append("protected boolean contains(" + pkClassName + " " + pkVarName + ", " + tempEntity.getPKClassName() + " " + tempEntity.getPKVarName() + ") {");
4726 sm.append("List results = execute(new Object[] {" + pkVarNameWrapper + ", " + tempEntityPkVarNameWrapper + "});");
4727 sm.append("if (results.size() > 0) {");
4728 sm.append("Integer count = (Integer)results.get(0);");
4729 sm.append("if (count.intValue() > 0) {");
4730 sm.append("return true;");
4731 sm.append("}");
4732 sm.append("}");
4733 sm.append("return false;");
4734 sm.append("}");
4735 sm.append("}");
4736
4737 if (col.isMappingManyToMany()) {
4738
4739
4741 sm.append("protected class Add" + tempEntity.getName() + " extends SqlUpdate {");
4742 sm.append("protected Add" + tempEntity.getName() + "(" + entity.getName() + "PersistenceImpl persistenceImpl) {");
4743 sm.append("super(persistenceImpl.getDataSource(), \"INSERT INTO " + col.getMappingTable() + " (" + pkVarName + ", " + tempEntity.getPKVarName() + ") VALUES (?, ?)\");");
4744 sm.append("_persistenceImpl = persistenceImpl;");
4745 sm.append("declareParameter(new SqlParameter(Types." + entitySqlType + "));");
4746 sm.append("declareParameter(new SqlParameter(Types." + tempEntitySqlType + "));");
4747 sm.append("compile();");
4748 sm.append("}");
4749 sm.append("protected void add(" + pkClassName + " " + pkVarName + ", " + tempEntity.getPKClassName() + " " + tempEntity.getPKVarName() + ") {");
4750 sm.append("if (!_persistenceImpl.contains" + tempEntity.getName() + ".contains(" + pkVarName + ", " + tempEntity.getPKVarName() + ")) {");
4751 sm.append("update(new Object[] {" + pkVarNameWrapper + ", " + tempEntityPkVarNameWrapper + "});");
4752 sm.append("}");
4753 sm.append("}");
4754 sm.append("private " + entity.getName() + "PersistenceImpl _persistenceImpl;");
4755 sm.append("}");
4756
4757
4759 sm.append("protected class Clear" + tempEntity.getNames() + " extends SqlUpdate {");
4760 sm.append("protected Clear" + tempEntity.getNames() + "(" + entity.getName() + "PersistenceImpl persistenceImpl) {");
4761 sm.append("super(persistenceImpl.getDataSource(), \"DELETE FROM " + col.getMappingTable() + " WHERE " + pkVarName + " = ?\");");
4762 sm.append("declareParameter(new SqlParameter(Types." + entitySqlType + "));");
4763 sm.append("compile();");
4764 sm.append("}");
4765 sm.append("protected void clear(" + pkClassName + " " + pkVarName + ") {");
4766 sm.append("update(new Object[] {" + pkVarNameWrapper + "});");
4767 sm.append("}");
4768 sm.append("}");
4769
4770
4772 sm.append("protected class Remove" + tempEntity.getName() + " extends SqlUpdate {");
4773 sm.append("protected Remove" + tempEntity.getName() + "(" + entity.getName() + "PersistenceImpl persistenceImpl) {");
4774 sm.append("super(persistenceImpl.getDataSource(), \"DELETE FROM " + col.getMappingTable() + " WHERE " + pkVarName + " = ? AND " + tempEntity.getPKVarName() + " = ?\");");
4775 sm.append("declareParameter(new SqlParameter(Types." + entitySqlType + "));");
4776 sm.append("declareParameter(new SqlParameter(Types." + tempEntitySqlType + "));");
4777 sm.append("compile();");
4778 sm.append("}");
4779 sm.append("protected void remove(" + pkClassName + " " + pkVarName + ", " + tempEntity.getPKClassName() + " " + tempEntity.getPKVarName() + ") {");
4780 sm.append("update(new Object[] {" + pkVarNameWrapper + ", " + tempEntityPkVarNameWrapper + "});");
4781 sm.append("}");
4782 sm.append("}");
4783 }
4784 }
4785 }
4786
4787 for (int i = 0; i < columnList.size(); i++) {
4788 EntityColumn col = (EntityColumn)columnList.get(i);
4789
4790 if (col.isCollection()) {
4791 Entity tempEntity = getEntity(col.getEJBName());
4792
4793 if (col.isMappingManyToMany()) {
4794
4795
4797 sm.append("private static final String _SQL_GET" + tempEntity.getName().toUpperCase() + "S = \"SELECT {" + tempEntity.getTable() + ".*} FROM " + tempEntity.getTable() + " INNER JOIN " + col.getMappingTable() + " ON (" + col.getMappingTable() + "." + tempEntity.getPKVarName() + " = " + tempEntity.getTable() + "." + tempEntity.getPKVarName() + ") WHERE (" + col.getMappingTable() + "." + entity.getPKVarName() + " = ?)\";");
4798
4799
4801 sm.append("private static final String _SQL_GET" + tempEntity.getName().toUpperCase() + "SSIZE = \"SELECT COUNT(*) AS COUNT_VALUE FROM " + col.getMappingTable() + " WHERE " + entity.getPKVarName() + " = ?\";");
4802
4803
4805 sm.append("private static final String _SQL_CONTAINS" + tempEntity.getName().toUpperCase() + " = \"SELECT COUNT(*) AS COUNT_VALUE FROM " + col.getMappingTable() + " WHERE " + entity.getPKVarName() + " = ? AND " + tempEntity.getPKVarName() + " = ?\";");
4806 }
4807 else if (col.isMappingOneToMany()) {
4808
4809
4811 sm.append("private static final String _SQL_GET" + tempEntity.getName().toUpperCase() + "S = \"SELECT {" + tempEntity.getTable() + ".*} FROM " + tempEntity.getTable() + " INNER JOIN " + entity.getTable() + " ON (" + entity.getTable() + "." + entity.getPKVarName() + " = " + tempEntity.getTable() + "." + entity.getPKVarName() + ") WHERE (" + entity.getTable() + "." + entity.getPKVarName() + " = ?)\";");
4812
4813
4815 sm.append("private static final String _SQL_GET" + tempEntity.getName().toUpperCase() + "SSIZE = \"SELECT COUNT(*) AS COUNT_VALUE FROM " + tempEntity.getTable() + " WHERE " + entity.getPKVarName() + " = ?\";");
4816
4817
4819 sm.append("private static final String _SQL_CONTAINS" + tempEntity.getName().toUpperCase() + " = \"SELECT COUNT(*) AS COUNT_VALUE FROM " + tempEntity.getTable() + " WHERE " + entity.getPKVarName() + " = ? AND " + tempEntity.getPKVarName() + " = ?\";");
4820 }
4821 }
4822 }
4823
4824
4826 sm.append("private static Log _log = LogFactory.getLog(" + entity.getName() + "PersistenceImpl.class);");
4827
4828
4830 sm.append("}");
4831
4832
4834 File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "PersistenceImpl.java");
4835
4836 writeFile(ejbFile, sm.toString());
4837 }
4838
4839 private void _createPersistenceUtil(Entity entity) throws IOException {
4840 JavaClass javaClass = _getJavaClass(_outputPath + "/service/persistence/" + entity.getName() + "PersistenceImpl.java");
4841
4842 JavaMethod[] methods = javaClass.getMethods();
4843
4844 StringMaker sm = new StringMaker();
4845
4846
4848 sm.append("package " + _packagePath + ".service.persistence;");
4849
4850
4852 sm.append("import " + _propsUtilPackage + ".PropsUtil;");
4853 sm.append("import com.liferay.portal.kernel.util.GetterUtil;");
4854 sm.append("import com.liferay.portal.kernel.util.Validator;");
4855 sm.append("import com.liferay.portal.model.ModelListener;");
4856 sm.append("import org.apache.commons.logging.Log;");
4857 sm.append("import org.apache.commons.logging.LogFactory;");
4858
4859
4861 sm.append("public class " + entity.getName() + "Util {");
4862
4863
4865 for (int i = 0; i < methods.length; i++) {
4866 JavaMethod javaMethod = methods[i];
4867
4868 String methodName = javaMethod.getName();
4869
4870 if (!javaMethod.isConstructor() && javaMethod.isPublic()) {
4871 sm.append("public static " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
4872
4873 JavaParameter[] parameters = javaMethod.getParameters();
4874
4875 String p0Name = "";
4876
4877 if (parameters.length > 0) {
4878 p0Name = parameters[0].getName();
4879 }
4880
4881 for (int j = 0; j < parameters.length; j++) {
4882 JavaParameter javaParameter = parameters[j];
4883
4884 sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
4885
4886 if ((j + 1) != parameters.length) {
4887 sm.append(", ");
4888 }
4889 }
4890
4891 sm.append(")");
4892
4893 Type[] thrownExceptions = javaMethod.getExceptions();
4894
4895 if (thrownExceptions.length > 0) {
4896 sm.append(" throws ");
4897
4898 for (int j = 0; j < thrownExceptions.length; j++) {
4899 Type thrownException = thrownExceptions[j];
4900
4901 sm.append(thrownException.getValue());
4902
4903 if ((j + 1) != thrownExceptions.length) {
4904 sm.append(", ");
4905 }
4906 }
4907 }
4908
4909 sm.append(" {");
4910
4911 if (methodName.equals("remove") || methodName.equals("update")) {
4912 sm.append("ModelListener listener = _getListener();");
4913
4914 if (methodName.equals("update")) {
4915 sm.append("boolean isNew = " + p0Name + ".isNew();");
4916 }
4917
4918 sm.append("if (listener != null) {");
4919
4920 if (methodName.equals("remove")) {
4921 if (entity.getVarName().equals(p0Name)) {
4922 sm.append("listener.onBeforeRemove(" + p0Name + ");");
4923 }
4924 else {
4925 sm.append("listener.onBeforeRemove(findByPrimaryKey(" + p0Name + "));");
4926 }
4927 }
4928 else {
4929 sm.append("if (isNew) {");
4930 sm.append("listener.onBeforeCreate(" + p0Name + ");");
4931 sm.append("}");
4932 sm.append("else {");
4933 sm.append("listener.onBeforeUpdate(" + p0Name + ");");
4934 sm.append("}");
4935 }
4936
4937 sm.append("}");
4938
4939 if (methodName.equals("remove") && !entity.getVarName().equals(p0Name)) {
4940 sm.append(_packagePath + ".model." + entity.getName() + " " + entity.getVarName() + " = ");
4941 }
4942 else {
4943 sm.append(entity.getVarName() + " = ");
4944 }
4945 }
4946 else {
4947 if (!javaMethod.getReturns().getValue().equals("void")) {
4948 sm.append("return ");
4949 }
4950 }
4951
4952 sm.append("getPersistence()." + methodName + "(");
4953
4954 for (int j = 0; j < parameters.length; j++) {
4955 JavaParameter javaParameter = parameters[j];
4956
4957 sm.append(javaParameter.getName());
4958
4959 if ((j + 1) != parameters.length) {
4960 sm.append(", ");
4961 }
4962 }
4963
4964 sm.append(");");
4965
4966 if (methodName.equals("remove") || methodName.equals("update")) {
4967 sm.append("if (listener != null) {");
4968
4969 if (methodName.equals("remove")) {
4970 sm.append("listener.onAfterRemove(" + entity.getVarName() + ");");
4971 }
4972 else {
4973 sm.append("if (isNew) {");
4974 sm.append("listener.onAfterCreate(" + entity.getVarName() + ");");
4975 sm.append("}");
4976 sm.append("else {");
4977 sm.append("listener.onAfterUpdate(" + entity.getVarName() + ");");
4978 sm.append("}");
4979 }
4980
4981 sm.append("}");
4982
4983 sm.append("return " + entity.getVarName() + ";");
4984 }
4985
4986 sm.append("}");
4987 }
4988 }
4989
4990 sm.append("public static " + entity.getName() + "Persistence getPersistence() {");
4991 sm.append("return _getUtil()._persistence;");
4992 sm.append("}");
4993
4994 sm.append("public void setPersistence(" + entity.getName() + "Persistence persistence) {");
4995 sm.append("_persistence = persistence;");
4996 sm.append("}");
4997
4998 sm.append("private static " + entity.getName() + "Util _getUtil() {");
4999 sm.append("if (_util == null) {");
5000 sm.append("_util = (" + entity.getName() + "Util)" + _beanLocatorUtilPackage + ".BeanLocatorUtil.locate(_UTIL);");
5001 sm.append("}");
5002 sm.append("return _util;");
5003 sm.append("}");
5004
5005 sm.append("private static ModelListener _getListener() {");
5006 sm.append("if (Validator.isNotNull(_LISTENER)) {");
5007 sm.append("try {");
5008 sm.append("return (ModelListener)Class.forName(_LISTENER).newInstance();");
5009 sm.append("}");
5010 sm.append("catch (Exception e) {");
5011 sm.append("_log.error(e);");
5012 sm.append("}");
5013 sm.append("}");
5014 sm.append("return null;");
5015 sm.append("}");
5016
5017
5019 sm.append("private static final String _UTIL = " + entity.getName() + "Util.class.getName();");
5020 sm.append("private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(\"value.object.listener." + _packagePath + ".model." + entity.getName() + "\"));");
5021
5022 sm.append("private static Log _log = LogFactory.getLog(" + entity.getName() + "Util.class);");
5023
5024 sm.append("private static " + entity.getName() + "Util _util;");
5025
5026 sm.append("private " + entity.getName() + "Persistence _persistence;");
5027
5028
5030 sm.append("}");
5031
5032
5034 File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "Util.java");
5035
5036 writeFile(ejbFile, sm.toString());
5037 }
5038
5039 private void _createPool(Entity entity) throws IOException {
5040 File ejbFile = new File(_outputPath + "/service/persistence/" + entity.getName() + "Pool.java");
5041
5042 if (ejbFile.exists()) {
5043 System.out.println("Removing deprecated " + ejbFile);
5044
5045 ejbFile.delete();
5046 }
5047 }
5048
5049 private void _createPrincipalBean() throws IOException {
5050 if (_principalBeanPackage.equals("com.liferay.portal.service.impl")) {
5051 return;
5052 }
5053
5054
5056 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/PrincipalBean.java");
5057
5058 content = StringUtil.replace(
5059 content,
5060 new String[] {
5061 "package com.liferay.portal.service.impl;"
5062 },
5063 new String[] {
5064 "package " + _principalBeanPackage + ";"
5065 });
5066
5067
5069 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_principalBeanPackage, ".", "/") + "/PrincipalBean.java");
5070
5071 FileUtil.write(ejbFile, content, true);
5072 }
5073
5074 private void _createProps() throws IOException {
5075 if (_propsUtilPackage.equals("com.liferay.portal.util")) {
5076 return;
5077 }
5078
5079
5081 File propsFile = new File(_implDir + "/portlet-service.properties");
5082
5083 long buildNumber = 1;
5084
5085 if (propsFile.exists()) {
5086 Properties props = PropertiesUtil.load(FileUtil.read(propsFile));
5087
5088 buildNumber = GetterUtil.getLong(props.getProperty("build.number")) + 1;
5089 }
5090
5091 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/portlet-service.properties");
5092
5093 content = StringUtil.replace(
5094 content,
5095 new String[] {
5096 "${build.namespace}",
5097 "${build.number}",
5098 "${build.date}",
5099 "com.liferay.portal.spring.hibernate."
5100 },
5101 new String[] {
5102 _portletShortName,
5103 String.valueOf(buildNumber),
5104 String.valueOf(System.currentTimeMillis()),
5105 _springHibernatePackage + "."
5106 });
5107
5108
5110 FileUtil.write(propsFile, content, true);
5111 }
5112
5113 private void _createPropsUtil() throws IOException {
5114 if (_propsUtilPackage.equals("com.liferay.portal.util")) {
5115 return;
5116 }
5117
5118 StringMaker sm = new StringMaker();
5119
5120
5122 sm.append("package " + _propsUtilPackage + ";");
5123
5124
5126 sm.append("import com.germinus.easyconf.ComponentProperties;");
5127 sm.append("import com.liferay.util.ExtPropertiesLoader;");
5128 sm.append("import java.util.Properties;");
5129
5130
5132 sm.append("public class PropsUtil {");
5133
5134
5136 sm.append("public static final String SPRING_CONFIGS = \"spring.configs\";");
5137
5138 sm.append("public static final String SPRING_HIBERNATE_DATA_SOURCE = \"spring.hibernate.data.source\";");
5139
5140 sm.append("public static final String SPRING_HIBERNATE_SESSION_FACTORY = \"spring.hibernate.session.factory\";");
5141
5142 sm.append("public static final String HIBERNATE_CONFIGS = \"hibernate.configs\";");
5143
5144 sm.append("public static final String VALUE_OBJECT_FINDER_CACHE_ENABLED = \"value.object.finder.cache.enabled\";");
5145
5146 sm.append("public static final String XSS_ALLOW = \"xss.allow\";");
5147
5148
5150 sm.append("public static boolean containsKey(String key) {");
5151 sm.append("return _getInstance().containsKey(key);");
5152 sm.append("}");
5153
5154 sm.append("public static String get(String key) {");
5155 sm.append("return _getInstance().get(key);");
5156 sm.append("}");
5157
5158 sm.append("public static void set(String key, String value) {");
5159 sm.append("_getInstance().set(key, value);");
5160 sm.append("}");
5161
5162 sm.append("public static String[] getArray(String key) {");
5163 sm.append("return _getInstance().getArray(key);");
5164 sm.append("}");
5165
5166 sm.append("public static Properties getProperties() {");
5167 sm.append("return _getInstance().getProperties();");
5168 sm.append("}");
5169
5170 sm.append("public static ComponentProperties getComponentProperties() {");
5171 sm.append("return _getInstance().getComponentProperties();");
5172 sm.append("}");
5173
5174 sm.append("private static ExtPropertiesLoader _getInstance() {");
5175 sm.append("return ExtPropertiesLoader.getInstance(\"portlet-service\");");
5176 sm.append("}");
5177
5178
5180 sm.append("}");
5181
5182
5184 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_propsUtilPackage, ".", "/") + "/PropsUtil.java");
5185
5186 writeFile(ejbFile, sm.toString());
5187 }
5188
5189 private void _createRemotingXML() throws Exception {
5190 StringMaker sm = new StringMaker();
5191
5192 Document doc = PortalUtil.readDocumentFromFile(
5193 new File(_springFileName), true);
5194
5195 Iterator itr = doc.getRootElement().elements("bean").iterator();
5196
5197 while (itr.hasNext()) {
5198 Element beanEl = (Element)itr.next();
5199
5200 String beanId = beanEl.attributeValue("id");
5201
5202 if (beanId.endsWith("ServiceFactory") && !beanId.endsWith("LocalServiceFactory")) {
5203 String serviceName = beanId.substring(0, beanId.length() - 7);
5204
5205 String serviceMapping = serviceName;
5206 serviceMapping = StringUtil.replace(serviceMapping, ".service.", ".service.spring.");
5207 serviceMapping = StringUtil.replace(serviceMapping, ".", "_");
5208
5209 sm.append("\t<bean name=\"/").append(serviceMapping).append("-burlap\" class=\"org.springframework.remoting.caucho.BurlapServiceExporter\">\n");
5210 sm.append("\t\t<property name=\"service\" ref=\"").append(serviceName).append(".transaction\" />\n");
5211 sm.append("\t\t<property name=\"serviceInterface\" value=\"").append(serviceName).append("\" />\n");
5212 sm.append("\t</bean>\n");
5213
5214 sm.append("\t<bean name=\"/").append(serviceMapping).append("-hessian\" class=\"org.springframework.remoting.caucho.HessianServiceExporter\">\n");
5215 sm.append("\t\t<property name=\"service\" ref=\"").append(serviceName).append(".transaction\" />\n");
5216 sm.append("\t\t<property name=\"serviceInterface\" value=\"").append(serviceName).append("\" />\n");
5217 sm.append("\t</bean>\n");
5218
5219 sm.append("\t<bean name=\"/").append(serviceMapping).append("-http\" class=\"org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter\">\n");
5220 sm.append("\t\t<property name=\"service\" ref=\"").append(serviceName).append(".transaction\" />\n");
5221 sm.append("\t\t<property name=\"serviceInterface\" value=\"").append(serviceName).append("\" />\n");
5222 sm.append("\t</bean>\n");
5223 }
5224 }
5225
5226 File outputFile = new File(
5227 "../tunnel-web/docroot/WEB-INF/remoting-servlet.xml");
5228
5229 if (!outputFile.exists()) {
5230 outputFile = new File(
5231 "../ext-web/docroot/WEB-INF/remoting-servlet-ext.xml");
5232 }
5233
5234 if (!outputFile.exists()) {
5235 return;
5236 }
5237
5238 String content = FileUtil.read(outputFile);
5239 String newContent = content;
5240
5241 int x = content.indexOf("<bean ");
5242 int y = content.lastIndexOf("</bean>") + 8;
5243
5244 if (x != -1) {
5245 newContent =
5246 content.substring(0, x - 1) + sm.toString() +
5247 content.substring(y, content.length());
5248 }
5249 else {
5250 x = content.indexOf("</beans>");
5251
5252 newContent =
5253 content.substring(0, x) + sm.toString() +
5254 content.substring(x, content.length());
5255 }
5256
5257 if (!content.equals(newContent)) {
5258 FileUtil.write(outputFile, newContent);
5259
5260 System.out.println(outputFile.toString());
5261 }
5262 }
5263
5264 private void _createService(Entity entity, int sessionType) throws IOException {
5265 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + (sessionType != _REMOTE ? "Local" : "") + "ServiceImpl.java");
5266
5267 JavaMethod[] methods = javaClass.getMethods();
5268
5269 if (sessionType == _LOCAL) {
5270 if (javaClass.getSuperClass().getValue().endsWith(entity.getName() + "LocalServiceBaseImpl")) {
5271 JavaClass parentJavaClass = _getJavaClass(_outputPath + "/service/base/" + entity.getName() + "LocalServiceBaseImpl.java");
5272
5273 JavaMethod[] parentMethods = parentJavaClass.getMethods();
5274
5275 JavaMethod[] allMethods = new JavaMethod[parentMethods.length + methods.length];
5276
5277 ArrayUtil.combine(parentMethods, methods, allMethods);
5278
5279 methods = allMethods;
5280 }
5281 }
5282
5283 StringMaker sm = new StringMaker();
5284
5285
5287 sm.append("package " + _packagePath + ".service;");
5288
5289
5291 sm.append("public interface " + entity.getName() + _getSessionTypeName(sessionType) + "Service {");
5292
5293
5295 for (int i = 0; i < methods.length; i++) {
5296 JavaMethod javaMethod = methods[i];
5297
5298 String methodName = javaMethod.getName();
5299
5300 if (!javaMethod.isConstructor() && javaMethod.isPublic() && _isCustomMethod(javaMethod)) {
5301 sm.append("public " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
5302
5303 JavaParameter[] parameters = javaMethod.getParameters();
5304
5305 for (int j = 0; j < parameters.length; j++) {
5306 JavaParameter javaParameter = parameters[j];
5307
5308 sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
5309
5310 if ((j + 1) != parameters.length) {
5311 sm.append(", ");
5312 }
5313 }
5314
5315 sm.append(")");
5316
5317 Type[] thrownExceptions = javaMethod.getExceptions();
5318
5319 Set newExceptions = new LinkedHashSet();
5320
5321 for (int j = 0; j < thrownExceptions.length; j++) {
5322 Type thrownException = thrownExceptions[j];
5323
5324 newExceptions.add(thrownException.getValue());
5325 }
5326
5327 if (sessionType != _LOCAL) {
5328 newExceptions.add("java.rmi.RemoteException");
5329 }
5330
5331 if (newExceptions.size() > 0) {
5332 sm.append(" throws ");
5333
5334 Iterator itr = newExceptions.iterator();
5335
5336 while (itr.hasNext()) {
5337 sm.append(itr.next());
5338
5339 if (itr.hasNext()) {
5340 sm.append(", ");
5341 }
5342 }
5343 }
5344
5345 sm.append(";");
5346 }
5347 }
5348
5349
5351 sm.append("}");
5352
5353
5355 File ejbFile = new File(_serviceOutputPath + "/service/" + entity.getName() + _getSessionTypeName(sessionType) + "Service.java");
5356
5357 Map jalopySettings = new HashMap();
5358
5359 String serviceComments = null;
5360
5361 if (sessionType == _REMOTE) {
5362 serviceComments = "This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.";
5363 }
5364 else {
5365 serviceComments = "This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.";
5366 }
5367
5368 String[] classComments = {
5369 _DEFAULT_CLASS_COMMENTS,
5370 "This interface defines the service. The default implementation is <code>" + _packagePath + ".service.impl." + entity.getName() + _getSessionTypeName(sessionType) + "ServiceImpl</code>. Modify methods in that class and rerun ServiceBuilder to populate this class and all other generated classes.",
5371 serviceComments
5372 };
5373
5374 String[] see = {
5375 _packagePath + ".service." + entity.getName() + "ServiceFactory",
5376 _packagePath + ".service." + entity.getName() + "ServiceUtil"
5377 };
5378
5379 jalopySettings.put("classComments", classComments);
5380 jalopySettings.put("see", see);
5381
5382 writeFile(ejbFile, sm.toString(), jalopySettings);
5383
5384
5391 }
5392
5393 private void _createServiceBaseImpl(Entity entity) throws IOException {
5394 List regularColList = entity.getRegularColList();
5395
5396 StringMaker sm = new StringMaker();
5397
5398
5400 sm.append("package " + _packagePath + ".service.base;");
5401
5402
5404 sm.append("import " + _packagePath + ".service." + entity.getName() + "LocalService;");
5405
5406 if (entity.hasColumns()) {
5407 sm.append("import " + _packagePath + ".model." + entity.getName() + ";");
5408 sm.append("import " + _packagePath + ".model.impl." + entity.getName() + "Impl;");
5409 sm.append("import " + _packagePath + ".service.persistence." + entity.getName() + "Util;");
5410 sm.append("import com.liferay.portal.SystemException;");
5411 sm.append("import com.liferay.portal.kernel.dao.DynamicQueryInitializer;");
5412 sm.append("import java.util.List;");
5413 }
5414
5415
5417 sm.append("public abstract class " + entity.getName() + "LocalServiceBaseImpl implements " + entity.getName() + "LocalService {");
5418
5419
5421 if (entity.hasColumns()) {
5422 sm.append("public " + entity.getName() + " add" + entity.getName() + "(" + entity.getName() + " model) throws SystemException {");
5423 sm.append(entity.getName() + " " + entity.getVarName() + " = new " + entity.getName() + "Impl();");
5424 sm.append(entity.getVarName() + ".setNew(true);");
5425
5426 for (int i = 0; i < regularColList.size(); i++) {
5427 EntityColumn col = (EntityColumn)regularColList.get(i);
5428
5429 sm.append(entity.getVarName() + ".set" + col.getMethodName() + "(model.get" + col.getMethodName() + "());");
5430 }
5431
5432 sm.append("return " + entity.getName() + "Util.update(" + entity.getVarName() + ");");
5433 sm.append("}");
5434
5435 sm.append("public List dynamicQuery(DynamicQueryInitializer queryInitializer) throws SystemException {");
5436 sm.append("return " + entity.getName() + "Util.findWithDynamicQuery(queryInitializer);");
5437 sm.append("}");
5438
5439 sm.append("public List dynamicQuery(DynamicQueryInitializer queryInitializer, int begin, int end) throws SystemException {");
5440 sm.append("return " + entity.getName() + "Util.findWithDynamicQuery(queryInitializer, begin, end);");
5441 sm.append("}");
5442
5443 sm.append("public " + entity.getName() + " update" + entity.getName() + "(" + entity.getName() + " model) throws SystemException {");
5444 sm.append(entity.getName() + " " + entity.getVarName() + " = new " + entity.getName() + "Impl();");
5445 sm.append(entity.getVarName() + ".setNew(false);");
5446
5447 for (int i = 0; i < regularColList.size(); i++) {
5448 EntityColumn col = (EntityColumn)regularColList.get(i);
5449
5450 sm.append(entity.getVarName() + ".set" + col.getMethodName() + "(model.get" + col.getMethodName() + "());");
5451 }
5452
5453 sm.append("return " + entity.getName() + "Util.update(" + entity.getVarName() + ");");
5454 sm.append("}");
5455 }
5456
5457
5459 sm.append("}");
5460
5461
5463 File ejbFile = new File(_outputPath + "/service/base/" + entity.getName() + "LocalServiceBaseImpl.java");
5464
5465 writeFile(ejbFile, sm.toString());
5466 }
5467
5468 private void _createServiceFactory(Entity entity, int sessionType) throws IOException {
5469 StringMaker sm = new StringMaker();
5470
5471
5473 sm.append("package " + _packagePath + ".service;");
5474
5475
5477 sm.append("public class " + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory {");
5478
5479
5481 sm.append("public static " + entity.getName() + _getSessionTypeName(sessionType) + "Service getService() {");
5482 sm.append("return _getFactory()._service;");
5483 sm.append("}");
5484
5485 sm.append("public static " + entity.getName() + _getSessionTypeName(sessionType) + "Service getTxImpl() {");
5486 sm.append("if (_txImpl == null) {");
5487 sm.append("_txImpl = (" + entity.getName() + _getSessionTypeName(sessionType) + "Service)" + _beanLocatorUtilPackage + ".BeanLocatorUtil.locate(_TX_IMPL);");
5488 sm.append("}");
5489 sm.append("return _txImpl;");
5490 sm.append("}");
5491
5492 sm.append("public void setService(" + entity.getName() + _getSessionTypeName(sessionType) + "Service service) {");
5493 sm.append("_service = service;");
5494 sm.append("}");
5495
5496 sm.append("private static " + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory _getFactory() {");
5497 sm.append("if (_factory == null) {");
5498 sm.append("_factory = (" + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory)" + _beanLocatorUtilPackage + ".BeanLocatorUtil.locate(_FACTORY);");
5499 sm.append("}");
5500 sm.append("return _factory;");
5501 sm.append("}");
5502
5503
5505 sm.append("private static final String _FACTORY = " + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory.class.getName();");
5506 sm.append("private static final String _TX_IMPL = " + entity.getName() + _getSessionTypeName(sessionType) + "Service.class.getName() + \".transaction\";");
5507
5508 sm.append("private static " + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory _factory;");
5509 sm.append("private static " + entity.getName() + _getSessionTypeName(sessionType) + "Service _txImpl;");
5510
5511 sm.append("private " + entity.getName() + _getSessionTypeName(sessionType) + "Service _service;");
5512
5513
5515 sm.append("}");
5516
5517
5519 File ejbFile = new File(_serviceOutputPath + "/service/" + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory.java");
5520
5521 Map jalopySettings = new HashMap();
5522
5523 String[] classComments = {
5524 _DEFAULT_CLASS_COMMENTS,
5525 "This class is responsible for the lookup of the implementation for <code>" + _packagePath + ".service." + entity.getName() + "Service</code>. Spring manages the lookup and lifecycle of the beans. This means you can modify the Spring configuration files to return a different implementation or to inject additional behavior.",
5526 "See the <code>spring.configs</code> property in portal.properties for additional information on how to customize the Spring XML files."
5527 };
5528
5529 String[] see = {
5530 _packagePath + ".service." + entity.getName() + "Service",
5531 _packagePath + ".service." + entity.getName() + "ServiceUtil"
5532 };
5533
5534 jalopySettings.put("classComments", classComments);
5535 jalopySettings.put("see", see);
5536
5537 writeFile(ejbFile, sm.toString(), jalopySettings);
5538
5539
5546 }
5547
5548 private void _createServiceHttp(Entity entity) throws IOException {
5549 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + "ServiceImpl.java");
5550
5551 JavaMethod[] methods = javaClass.getMethods();
5552
5553 StringMaker sm = new StringMaker();
5554
5555
5557 sm.append("package " + _packagePath + ".service.http;");
5558
5559
5561 if (_hasHttpMethods(javaClass)) {
5562 sm.append("import " + _packagePath + ".service." + entity.getName() + "ServiceUtil;");
5563 }
5564
5565 sm.append("import com.liferay.portal.kernel.log.Log;");
5566 sm.append("import com.liferay.portal.kernel.log.LogFactoryUtil;");
5567 sm.append("import com.liferay.portal.kernel.util.BooleanWrapper;");
5568 sm.append("import com.liferay.portal.kernel.util.DoubleWrapper;");
5569 sm.append("import com.liferay.portal.kernel.util.FloatWrapper;");
5570 sm.append("import com.liferay.portal.kernel.util.IntegerWrapper;");
5571 sm.append("import com.liferay.portal.kernel.util.LongWrapper;");
5572 sm.append("import com.liferay.portal.kernel.util.MethodWrapper;");
5573 sm.append("import com.liferay.portal.kernel.util.NullWrapper;");
5574 sm.append("import com.liferay.portal.kernel.util.ShortWrapper;");
5575 sm.append("import com.liferay.portal.security.auth.HttpPrincipal;");
5576 sm.append("import com.liferay.portal.service.http.TunnelUtil;");
5577
5578
5580 sm.append("public class " + entity.getName() + "ServiceHttp {");
5581
5582
5584 for (int i = 0; i < methods.length; i++) {
5585 JavaMethod javaMethod = methods[i];
5586
5587 String methodName = javaMethod.getName();
5588
5589 if (!javaMethod.isConstructor() && javaMethod.isPublic() && _isCustomMethod(javaMethod)) {
5590 Type returnType = javaMethod.getReturns();
5591 String returnTypeName = returnType.getValue() + _getDimensions(returnType);
5592
5593 sm.append("public static " + returnTypeName + " " + methodName + "(HttpPrincipal httpPrincipal");
5594
5595 JavaParameter[] parameters = javaMethod.getParameters();
5596
5597 for (int j = 0; j < parameters.length; j++) {
5598 JavaParameter javaParameter = parameters[j];
5599
5600 if (j == 0) {
5601 sm.append(", ");
5602 }
5603
5604 sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
5605
5606 if ((j + 1) != parameters.length) {
5607 sm.append(", ");
5608 }
5609 }
5610
5611 sm.append(")");
5612
5613 Type[] thrownExceptions = javaMethod.getExceptions();
5614
5615 Set newExceptions = new LinkedHashSet();
5616
5617 for (int j = 0; j < thrownExceptions.length; j++) {
5618 Type thrownException = thrownExceptions[j];
5619
5620 newExceptions.add(thrownException.getValue());
5621 }
5622
5623 newExceptions.add("com.liferay.portal.SystemException");
5624
5625 if (newExceptions.size() > 0) {
5626 sm.append(" throws ");
5627
5628 Iterator itr = newExceptions.iterator();
5629
5630 while (itr.hasNext()) {
5631 sm.append(itr.next());
5632
5633 if (itr.hasNext()) {
5634 sm.append(", ");
5635 }
5636 }
5637 }
5638
5639 sm.append("{");
5640 sm.append("try {");
5641
5642 for (int j = 0; j < parameters.length; j++) {
5643 JavaParameter javaParameter = parameters[j];
5644
5645 String parameterTypeName =
5646 javaParameter.getType().getValue() +
5647 _getDimensions(javaParameter.getType());
5648
5649 sm.append("Object paramObj" + j + " = ");
5650
5651 if (parameterTypeName.equals("boolean")) {
5652 sm.append("new BooleanWrapper(" + javaParameter.getName() + ");");
5653 }
5654 else if (parameterTypeName.equals("double")) {
5655 sm.append("new DoubleWrapper(" + javaParameter.getName() + ");");
5656 }
5657 else if (parameterTypeName.equals("float")) {
5658 sm.append("new FloatWrapper(" + javaParameter.getName() + ");");
5659 }
5660 else if (parameterTypeName.equals("int")) {
5661 sm.append("new IntegerWrapper(" + javaParameter.getName() + ");");
5662 }
5663 else if (parameterTypeName.equals("long")) {
5664 sm.append("new LongWrapper(" + javaParameter.getName() + ");");
5665 }
5666 else if (parameterTypeName.equals("short")) {
5667 sm.append("new ShortWrapper(" + javaParameter.getName() + ");");
5668 }
5669 else {
5670 sm.append(javaParameter.getName() + ";");
5671
5672 sm.append("if (" + javaParameter.getName() + " == null) {");
5673 sm.append("paramObj" + j + " = new NullWrapper(\"" + _getClassName(javaParameter.getType()) + "\");");
5674 sm.append("}");
5675 }
5676 }
5677
5678 sm.append("MethodWrapper methodWrapper = new MethodWrapper(");
5679 sm.append(entity.getName() + "ServiceUtil.class.getName(),");
5680 sm.append("\"" + methodName + "\",");
5681
5682 if (parameters.length == 0) {
5683 sm.append("new Object[0]);");
5684 }
5685 else {
5686 sm.append("new Object[] {");
5687
5688 for (int j = 0; j < parameters.length; j++) {
5689 sm.append("paramObj" + j);
5690
5691 if ((j + 1) != parameters.length) {
5692 sm.append(", ");
5693 }
5694 }
5695
5696 sm.append("});");
5697 }
5698
5699 if (!returnTypeName.equals("void")) {
5700 sm.append("Object returnObj = null;");
5701 }
5702
5703 sm.append("try {");
5704
5705 if (!returnTypeName.equals("void")) {
5706 sm.append("returnObj =");
5707 }
5708
5709 sm.append("TunnelUtil.invoke(httpPrincipal, methodWrapper);");
5710 sm.append("}");
5711 sm.append("catch (Exception e) {");
5712
5713 Iterator itr = newExceptions.iterator();
5714
5715 while (itr.hasNext()) {
5716 String exceptionType = (String)itr.next();
5717
5718 sm.append("if (e instanceof " + exceptionType + ") {");
5719 sm.append("throw (" + exceptionType + ")e;");
5720 sm.append("}");
5721 }
5722
5723 sm.append("throw new com.liferay.portal.SystemException(e);");
5724 sm.append("}");
5725
5726 if (!returnTypeName.equals("void")) {
5727 if (returnTypeName.equals("boolean")) {
5728 sm.append("return ((Boolean)returnObj).booleanValue();");
5729 }
5730 else if (returnTypeName.equals("double")) {
5731 sm.append("return ((Double)returnObj).doubleValue();");
5732 }
5733 else if (returnTypeName.equals("float")) {
5734 sm.append("return ((Float)returnObj).floatValue();");
5735 }
5736 else if (returnTypeName.equals("int")) {
5737 sm.append("return ((Integer)returnObj).intValue();");
5738 }
5739 else if (returnTypeName.equals("long")) {
5740 sm.append("return ((Long)returnObj).longValue();");
5741 }
5742 else if (returnTypeName.equals("short")) {
5743 sm.append("return ((Short)returnObj).shortValue();");
5744 }
5745 else {
5746 sm.append("return (" + returnTypeName + ")returnObj;");
5747 }
5748 }
5749
5750 sm.append("}");
5751 sm.append("catch (com.liferay.portal.SystemException se) {");
5752 sm.append("_log.error(se, se);");
5753 sm.append("throw se;");
5754 sm.append("}");
5755 sm.append("}");
5756 }
5757 }
5758
5759
5761 if (sm.indexOf("_log.") != -1) {
5762 sm.append("private static Log _log = LogFactoryUtil.getLog(" + entity.getName() + "ServiceHttp.class);");
5763 }
5764
5765
5767 sm.append("}");
5768
5769
5771 File ejbFile = new File(_outputPath + "/service/http/" + entity.getName() + "ServiceHttp.java");
5772
5773 Map jalopySettings = new HashMap();
5774
5775 String[] classComments = {
5776 _DEFAULT_CLASS_COMMENTS,
5777 "This class provides a HTTP utility for the <code>" + _packagePath + ".service." + entity.getName() + "ServiceUtil</code> service utility. The static methods of this class calls the same methods of the service utility. However, the signatures are different because it requires an additional <code>com.liferay.portal.security.auth.HttpPrincipal</code> parameter.",
5778 "The benefits of using the HTTP utility is that it is fast and allows for tunneling without the cost of serializing to text. The drawback is that it only works with Java.",
5779 "Set the property <code>tunnel.servlet.hosts.allowed</code> in portal.properties to configure security.",
5780 "The HTTP utility is only generated for remote services."
5781 };
5782
5783 String[] see = {
5784 "com.liferay.portal.security.auth.HttpPrincipal",
5785 _packagePath + ".service." + entity.getName() + "ServiceUtil",
5786 _packagePath + ".service.http." + entity.getName() + "ServiceSoap"
5787 };
5788
5789 jalopySettings.put("classComments", classComments);
5790 jalopySettings.put("see", see);
5791
5792 writeFile(ejbFile, sm.toString(), jalopySettings);
5793 }
5794
5795 private void _createServiceImpl(Entity entity, int sessionType) throws IOException {
5796 StringMaker sm = new StringMaker();
5797
5798
5800 sm.append("package " + _packagePath + ".service.impl;");
5801
5802
5804 sm.append("import " + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "Service;");
5805
5806 if (sessionType == _REMOTE) {
5807 sm.append("import " + _principalBeanPackage + ".PrincipalBean;");
5808 }
5809 else {
5810 sm.append("import " + _packagePath + ".service.base." + entity.getName() + "LocalServiceBaseImpl;");
5811 }
5812
5813
5815 if (sessionType == _REMOTE) {
5816 sm.append("public class " + entity.getName() + "ServiceImpl extends PrincipalBean implements " + entity.getName() + "Service {");
5817 }
5818 else {
5819 sm.append("public class " + entity.getName() + "LocalServiceImpl extends " + entity.getName() + "LocalServiceBaseImpl {");
5820 }
5821
5822
5824 sm.append("}");
5825
5826
5828 File ejbFile = new File(_outputPath + "/service/impl/" + entity.getName() + _getSessionTypeName(sessionType) + "ServiceImpl.java");
5829
5830 if (!ejbFile.exists()) {
5831 writeFile(ejbFile, sm.toString());
5832 }
5833 }
5834
5835 private void _createServiceJSON(Entity entity) throws IOException {
5836 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + "ServiceImpl.java");
5837
5838 JavaMethod[] methods = javaClass.getMethods();
5839
5840 StringMaker sm = new StringMaker();
5841
5842
5844 sm.append("package " + _packagePath + ".service.http;");
5845
5846
5848 if (_hasSoapMethods(javaClass)) {
5849 sm.append("import " + _packagePath + ".service." + entity.getName() + "ServiceUtil;");
5850 }
5851
5852 sm.append("import org.json.JSONArray;");
5853 sm.append("import org.json.JSONObject;");
5854
5855
5857 sm.append("public class " + entity.getName() + "ServiceJSON {");
5858
5859
5861 for (int i = 0; i < methods.length; i++) {
5862 JavaMethod javaMethod = methods[i];
5863
5864 String methodName = javaMethod.getName();
5865
5866 if (!javaMethod.isConstructor() && javaMethod.isPublic() && _isCustomMethod(javaMethod) && _isSoapMethod(javaMethod)) {
5867 String returnValueName = javaMethod.getReturns().getValue();
5868 String returnValueDimension = _getDimensions(javaMethod.getReturns());
5869
5870 String extendedModelName = _packagePath + ".model." + entity.getName();
5871 String soapModelName = "JSONObject";
5872
5873 sm.append("public static ");
5874
5875 if (returnValueName.equals(extendedModelName)) {
5876 sm.append(soapModelName + returnValueDimension);
5877 }
5878 else if (returnValueName.equals("java.util.List")) {
5879 if (entity.hasColumns()) {
5880 sm.append("JSONArray");
5881 }
5882 else {
5883 sm.append("java.util.List");
5884 }
5885 }
5886 else if (returnValueName.equals("com.liferay.portal.kernel.json.JSONArrayWrapper")) {
5887 sm.append("JSONArray");
5888 }
5889 else if (returnValueName.equals("com.liferay.portal.kernel.json.JSONObjectWrapper")) {
5890 sm.append("JSONObject");
5891 }
5892 else {
5893 sm.append(returnValueName + returnValueDimension);
5894 }
5895
5896 sm.append(" " + methodName + "(");
5897
5898 JavaParameter[] parameters = javaMethod.getParameters();
5899
5900 for (int j = 0; j < parameters.length; j++) {
5901 JavaParameter javaParameter = parameters[j];
5902
5903 String parameterTypeName = javaParameter.getType().getValue() + _getDimensions(javaParameter.getType());
5904
5905 if (parameterTypeName.equals("java.util.Locale")) {
5906 parameterTypeName = "String";
5907 }
5908
5909 sm.append(parameterTypeName + " " + javaParameter.getName());
5910
5911 if ((j + 1) != parameters.length) {
5912 sm.append(", ");
5913 }
5914 }
5915
5916 sm.append(")");
5917
5918 Type[] thrownExceptions = javaMethod.getExceptions();
5919
5920 Set newExceptions = new LinkedHashSet();
5921
5922 for (int j = 0; j < thrownExceptions.length; j++) {
5923 Type thrownException = thrownExceptions[j];
5924
5925 newExceptions.add(thrownException.getValue());
5926 }
5927
5928 newExceptions.add("java.rmi.RemoteException");
5929
5930 if (newExceptions.size() > 0) {
5931 sm.append(" throws ");
5932
5933 Iterator itr = newExceptions.iterator();
5934
5935 while (itr.hasNext()) {
5936 sm.append(itr.next());
5937
5938 if (itr.hasNext()) {
5939 sm.append(", ");
5940 }
5941 }
5942 }
5943
5944 sm.append("{");
5945
5946 if (!returnValueName.equals("void")) {
5947 sm.append(returnValueName + returnValueDimension + " returnValue = ");
5948 }
5949
5950 sm.append(entity.getName() + "ServiceUtil." + methodName + "(");
5951
5952 for (int j = 0; j < parameters.length; j++) {
5953 JavaParameter javaParameter = parameters[j];
5954
5955 String parameterTypeName =
5956 javaParameter.getType().getValue() +
5957 _getDimensions(javaParameter.getType());
5958
5959 if (parameterTypeName.equals("java.util.Locale")) {
5960 sm.append("new java.util.Locale(");
5961 }
5962
5963 sm.append(javaParameter.getName());
5964
5965 if (parameterTypeName.equals("java.util.Locale")) {
5966 sm.append(")");
5967 }
5968
5969 if ((j + 1) != parameters.length) {
5970 sm.append(", ");
5971 }
5972 }
5973
5974 sm.append(");");
5975
5976 if (!returnValueName.equals("void")) {
5977 if (returnValueName.equals(extendedModelName)) {
5978 sm.append("return " + entity.getName() + "JSONSerializer.toJSONObject(returnValue);");
5979 }
5980 else if (entity.hasColumns() && returnValueName.equals("java.util.List")) {
5981 sm.append("return " + entity.getName() + "JSONSerializer.toJSONArray(returnValue);");
5982 }
5983 else if (returnValueName.equals("com.liferay.portal.kernel.json.JSONArrayWrapper")) {
5984 sm.append("return (JSONArray)returnValue.getValue();");
5985 }
5986 else if (returnValueName.equals("com.liferay.portal.kernel.json.JSONObjectWrapper")) {
5987 sm.append("return (JSONObject)returnValue.getValue();");
5988 }
5989 else {
5990 sm.append("return returnValue;");
5991 }
5992 }
5993
5994 sm.append("}");
5995 }
5996 }
5997
5998
6000 if (sm.indexOf("_log.") != -1) {
6001 sm.append("private static Log _log = LogFactoryUtil.getLog(" + entity.getName() + "ServiceJSON.class);");
6002 }
6003
6004
6006 sm.append("}");
6007
6008
6010 File ejbFile = new File(_outputPath + "/service/http/" + entity.getName() + "ServiceJSON.java");
6011
6012 Map jalopySettings = new HashMap();
6013
6014 String[] classComments = {
6015 _DEFAULT_CLASS_COMMENTS,
6016 "This class provides a JSON utility for the <code>" + _packagePath + ".service." + entity.getName() + "ServiceUtil</code> service utility. The static methods of this class calls the same methods of the service utility. However, the signatures are different because it is difficult for JSON to support certain types.",
6017 "ServiceBuilder follows certain rules in translating the methods. For example, if the method in the service utility returns a <code>java.util.List</code>, that is translated to a <code>org.json.JSONArray</code>. If the method in the service utility returns a <code>" + _packagePath + ".model." + entity.getName() + "</code>, that is translated to a <code>org.json.JSONObject</code>. Methods that JSON cannot safely use are skipped. The logic for the translation is encapsulated in <code>" + _packagePath + ".service.http." + entity.getName() + "JSONSerializer</code>.",
6018 "This allows you to call the the backend services directly from JavaScript. See <code>portal-web/docroot/html/portlet/tags_admin/unpacked.js</code> for a reference of how that portlet uses the generated JavaScript in <code>portal-web/docroot/html/js/service.js</code> to call the backend services directly from JavaScript.",
6019 "The JSON utility is only generated for remote services."
6020 };
6021
6022 String[] see = {
6023 _packagePath + ".service." + entity.getName() + "ServiceUtil",
6024 _packagePath + ".service.http." + entity.getName() + "JSONSerializer"
6025 };
6026
6027 jalopySettings.put("classComments", classComments);
6028 jalopySettings.put("see", see);
6029
6030 writeFile(ejbFile, sm.toString(), jalopySettings);
6031 }
6032
6033 private void _createServiceJSONSerializer(Entity entity) throws IOException {
6034 List regularColList = entity.getRegularColList();
6035
6036 StringMaker sm = new StringMaker();
6037
6038
6040 sm.append("package " + _packagePath + ".service.http;");
6041
6042
6044 sm.append("import " + _packagePath + ".model." + entity.getName() + ";");
6045 sm.append("import com.liferay.util.JSONUtil;");
6046 sm.append("import java.util.Date;");
6047 sm.append("import java.util.List;");
6048 sm.append("import org.json.JSONArray;");
6049 sm.append("import org.json.JSONObject;");
6050
6051
6053 sm.append("public class " + entity.getName() + "JSONSerializer {");
6054
6055
6057 sm.append("public static JSONObject toJSONObject(" + entity.getName() + " model) {");
6058 sm.append("JSONObject jsonObj = new JSONObject();");
6059
6060 for (int i = 0; i < regularColList.size(); i++) {
6061 EntityColumn col = (EntityColumn)regularColList.get(i);
6062
6063 sm.append("JSONUtil.put(jsonObj, \"" + col.getName() + "\", model.get" + col.getMethodName() + "());");
6064 }
6065
6066 sm.append("return jsonObj;");
6067 sm.append("}");
6068
6069 sm.append("public static JSONArray toJSONArray(List models) {");
6070 sm.append("JSONArray jsonArray = new JSONArray();");
6071 sm.append("for (int i = 0; i < models.size(); i++) {");
6072 sm.append(entity.getName() + " model = (" + entity.getName() + ")models.get(i);");
6073 sm.append("jsonArray.put(toJSONObject(model));");
6074 sm.append("}");
6075 sm.append("return jsonArray;");
6076 sm.append("}");
6077
6078
6080 sm.append("}");
6081
6082
6084 File ejbFile = new File(_outputPath + "/service/http/" + entity.getName() + "JSONSerializer.java");
6085
6086 Map jalopySettings = new HashMap();
6087
6088 String[] classComments = {
6089 _DEFAULT_CLASS_COMMENTS,
6090 "This class is used by <code>" + _packagePath + ".service.http." + entity.getName() + "ServiceJSON</code> to translate objects."
6091 };
6092
6093 String[] see = {
6094 _packagePath + ".service.http." + entity.getName() + "ServiceJSON"
6095 };
6096
6097 jalopySettings.put("classComments", classComments);
6098 jalopySettings.put("see", see);
6099
6100 writeFile(ejbFile, sm.toString(), jalopySettings);
6101 }
6102
6103 private void _createServiceSoap(Entity entity) throws IOException {
6104 JavaClass javaClass = _getJavaClass(_outputPath + "/service/impl/" + entity.getName() + "ServiceImpl.java");
6105
6106 JavaMethod[] methods = javaClass.getMethods();
6107
6108 StringMaker sm = new StringMaker();
6109
6110
6112 sm.append("package " + _packagePath + ".service.http;");
6113
6114
6116 if (_hasSoapMethods(javaClass)) {
6117 sm.append("import " + _packagePath + ".service." + entity.getName() + "ServiceUtil;");
6118 }
6119
6120 sm.append("import com.liferay.portal.kernel.log.Log;");
6121 sm.append("import com.liferay.portal.kernel.log.LogFactoryUtil;");
6122 sm.append("import java.rmi.RemoteException;");
6123
6124
6126 sm.append("public class " + entity.getName() + "ServiceSoap {");
6127
6128
6130 for (int i = 0; i < methods.length; i++) {
6131 JavaMethod javaMethod = methods[i];
6132
6133 String methodName = javaMethod.getName();
6134
6135 if (!javaMethod.isConstructor() && javaMethod.isPublic() && _isCustomMethod(javaMethod) && _isSoapMethod(javaMethod)) {
6136 String returnValueName = javaMethod.getReturns().getValue();
6137 String returnValueDimension = _getDimensions(javaMethod.getReturns());
6138
6139 String extendedModelName = _packagePath + ".model." + entity.getName();
6140 String soapModelName = _packagePath + ".model." + entity.getName() + "Soap";
6141
6142 sm.append("public static ");
6143
6144 if (returnValueName.equals(extendedModelName)) {
6145 sm.append(soapModelName + returnValueDimension);
6146 }
6147 else if (returnValueName.equals("java.util.List")) {
6148 if (entity.hasColumns()) {
6149 sm.append(soapModelName + "[]");
6150 }
6151 else {
6152 sm.append("java.util.List");
6153 }
6154 }
6155 else {
6156 sm.append(returnValueName + returnValueDimension);
6157 }
6158
6159 sm.append(" " + methodName + "(");
6160
6161 JavaParameter[] parameters = javaMethod.getParameters();
6162
6163 for (int j = 0; j < parameters.length; j++) {
6164 JavaParameter javaParameter = parameters[j];
6165
6166 String parameterTypeName = javaParameter.getType().getValue() + _getDimensions(javaParameter.getType());
6167
6168 if (parameterTypeName.equals("java.util.Locale")) {
6169 parameterTypeName = "String";
6170 }
6171
6172 sm.append(parameterTypeName + " " + javaParameter.getName());
6173
6174 if ((j + 1) != parameters.length) {
6175 sm.append(", ");
6176 }
6177 }
6178
6179 sm.append(") throws RemoteException {");
6180 sm.append("try {");
6181
6182 if (!returnValueName.equals("void")) {
6183 sm.append(returnValueName + returnValueDimension + " returnValue = ");
6184 }
6185
6186 sm.append(entity.getName() + "ServiceUtil." + methodName + "(");
6187
6188 for (int j = 0; j < parameters.length; j++) {
6189 JavaParameter javaParameter = parameters[j];
6190
6191 String parameterTypeName =
6192 javaParameter.getType().getValue() +
6193 _getDimensions(javaParameter.getType());
6194
6195 if (parameterTypeName.equals("java.util.Locale")) {
6196 sm.append("new java.util.Locale(");
6197 }
6198
6199 sm.append(javaParameter.getName());
6200
6201 if (parameterTypeName.equals("java.util.Locale")) {
6202 sm.append(")");
6203 }
6204
6205 if ((j + 1) != parameters.length) {
6206 sm.append(", ");
6207 }
6208 }
6209
6210 sm.append(");");
6211
6212 if (!returnValueName.equals("void")) {
6213 if (returnValueName.equals(extendedModelName)) {
6214 sm.append("return " + soapModelName + ".toSoapModel(returnValue);");
6215 }
6216 else if (entity.hasColumns() && returnValueName.equals("java.util.List")) {
6217 sm.append("return " + soapModelName + ".toSoapModels(returnValue);");
6218 }
6219 else {
6220 sm.append("return returnValue;");
6221 }
6222 }
6223
6224 sm.append("}");
6225
6226 sm.append("catch (Exception e) {");
6227 sm.append("_log.error(e, e);");
6228 sm.append("throw new RemoteException(e.getMessage());");
6229 sm.append("}");
6230 sm.append("}");
6231 }
6232 }
6233
6234
6236 if (sm.indexOf("_log.") != -1) {
6237 sm.append("private static Log _log = LogFactoryUtil.getLog(" + entity.getName() + "ServiceSoap.class);");
6238 }
6239
6240
6242 sm.append("}");
6243
6244
6246 File ejbFile = new File(_outputPath + "/service/http/" + entity.getName() + "ServiceSoap.java");
6247
6248 Map jalopySettings = new HashMap();
6249
6250 String[] classComments = {
6251 _DEFAULT_CLASS_COMMENTS,
6252 "This class provides a SOAP utility for the <code>" + _packagePath + ".service." + entity.getName() + "ServiceUtil</code> service utility. The static methods of this class calls the same methods of the service utility. However, the signatures are different because it is difficult for SOAP to support certain types.",
6253 "ServiceBuilder follows certain rules in translating the methods. For example, if the method in the service utility returns a <code>java.util.List</code>, that is translated to an array of <code>" + _packagePath + ".model." + entity.getName() + "Soap</code>. If the method in the service utility returns a <code>" + _packagePath + ".model." + entity.getName() + "</code>, that is translated to a <code>" + _packagePath + ".model." + entity.getName() + "Soap</code>. Methods that SOAP cannot safely wire are skipped.",
6254 "The benefits of using the SOAP utility is that it is cross platform compatible. SOAP allows different languages like Java, .NET, C++, PHP, and even Perl, to call the generated services. One drawback of SOAP is that it is slow because it needs to serialize all calls into a text format (XML).",
6255 "You can see a list of services at http://localhost:8080/tunnel-web/secure/axis. Set the property <code>tunnel.servlet.hosts.allowed</code> in portal.properties to configure security.",
6256 "The SOAP utility is only generated for remote services."
6257 };
6258
6259 String[] see = {
6260 _packagePath + ".service." + entity.getName() + "ServiceUtil",
6261 _packagePath + ".service.http." + entity.getName() + "ServiceHttp",
6262 _packagePath + ".service.model." + entity.getName() + "Soap"
6263 };
6264
6265 jalopySettings.put("classComments", classComments);
6266 jalopySettings.put("see", see);
6267
6268 writeFile(ejbFile, sm.toString(), jalopySettings);
6269 }
6270
6271 private void _createServiceUtil(Entity entity, int sessionType) throws IOException {
6272 JavaClass javaClass = _getJavaClass(_serviceOutputPath + "/service/" + entity.getName() + (sessionType != _REMOTE ? "Local" : "") + "Service.java");
6273
6274 JavaMethod[] methods = javaClass.getMethods();
6275
6276 StringMaker sm = new StringMaker();
6277
6278
6280 sm.append("package " + _packagePath + ".service;");
6281
6282
6284 sm.append("public class " + entity.getName() + _getSessionTypeName(sessionType) + "ServiceUtil {");
6285
6286
6288 for (int i = 0; i < methods.length; i++) {
6289 JavaMethod javaMethod = methods[i];
6290
6291 String methodName = javaMethod.getName();
6292
6293 if (!javaMethod.isConstructor() && javaMethod.isPublic() && _isCustomMethod(javaMethod)) {
6294 sm.append("public static " + javaMethod.getReturns().getValue() + _getDimensions(javaMethod.getReturns()) + " " + methodName + "(");
6295
6296 JavaParameter[] parameters = javaMethod.getParameters();
6297
6298 for (int j = 0; j < parameters.length; j++) {
6299 JavaParameter javaParameter = parameters[j];
6300
6301 sm.append(javaParameter.getType().getValue() + _getDimensions(javaParameter.getType()) + " " + javaParameter.getName());
6302
6303 if ((j + 1) != parameters.length) {
6304 sm.append(", ");
6305 }
6306 }
6307
6308 sm.append(")");
6309
6310 Type[] thrownExceptions = javaMethod.getExceptions();
6311
6312 Set newExceptions = new LinkedHashSet();
6313
6314 for (int j = 0; j < thrownExceptions.length; j++) {
6315 Type thrownException = thrownExceptions[j];
6316
6317 newExceptions.add(thrownException.getValue());
6318 }
6319
6320 if (newExceptions.size() > 0) {
6321 sm.append(" throws ");
6322
6323 Iterator itr = newExceptions.iterator();
6324
6325 while (itr.hasNext()) {
6326 sm.append(itr.next());
6327
6328 if (itr.hasNext()) {
6329 sm.append(", ");
6330 }
6331 }
6332 }
6333
6334 sm.append("{");
6335 sm.append(entity.getName() + _getSessionTypeName(sessionType) + "Service " + entity.getVarName() + _getSessionTypeName(sessionType) + "Service = " + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory.getService();");
6336
6337 if (!javaMethod.getReturns().getValue().equals("void")) {
6338 sm.append("return ");
6339 }
6340
6341 sm.append(entity.getVarName() + _getSessionTypeName(sessionType) + "Service." + methodName + "(");
6342
6343 for (int j = 0; j < parameters.length; j++) {
6344 JavaParameter javaParameter = parameters[j];
6345
6346 sm.append(javaParameter.getName());
6347
6348 if ((j + 1) != parameters.length) {
6349 sm.append(", ");
6350 }
6351 }
6352
6353 sm.append(");");
6354 sm.append("}");
6355 }
6356 }
6357
6358
6360 sm.append("}");
6361
6362
6364 File ejbFile = new File(_serviceOutputPath + "/service/" + entity.getName() + _getSessionTypeName(sessionType) + "ServiceUtil.java");
6365
6366 Map jalopySettings = new HashMap();
6367
6368 String[] classComments = {
6369 _DEFAULT_CLASS_COMMENTS,
6370 "This class provides static methods for the <code>" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "Service</code> bean. The static methods of this class calls the same methods of the bean instance. It's convenient to be able to just write one line to call a method on a bean instead of writing a lookup call and a method call.",
6371 "<code>" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory</code> is responsible for the lookup of the bean."
6372 };
6373
6374 String[] see = {
6375 _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "Service",
6376 _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory"
6377 };
6378
6379 jalopySettings.put("classComments", classComments);
6380 jalopySettings.put("see", see);
6381
6382 writeFile(ejbFile, sm.toString(), jalopySettings);
6383
6384
6391 }
6392
6393 private void _createSpringDataSourceXML() throws IOException {
6394 if (Validator.isNull(_springDataSourceFileName)) {
6395 return;
6396 }
6397
6398
6400 String content = StringUtil.read(getClass().getClassLoader(), "META-INF/data-source-spring.xml");
6401
6402 content = StringUtil.replace(
6403 content,
6404 new String[] {
6405 "com.liferay.portal.spring.hibernate.",
6406 "com.liferay.util.spring.jndi.JndiObjectFactoryBean"
6407 },
6408 new String[] {
6409 _springHibernatePackage + ".",
6410 "com.liferay.util.spring.jndi.PortalDataSourceFactoryBean"
6411 });
6412
6413
6415 File ejbFile = new File(_springDataSourceFileName);
6416
6417 FileUtil.write(ejbFile, content, true);
6418 }
6419
6420 private void _createSpringUtil() throws IOException {
6421 if (_springUtilPackage.equals("com.liferay.portal.spring.util")) {
6422 return;
6423 }
6424
6425
6427 String content = StringUtil.read(getClass().getClassLoader(), "com/liferay/portal/tools/dependencies/SpringUtil.java");
6428
6429 content = StringUtil.replace(
6430 content,
6431 new String[] {
6432 "package com.liferay.portal.spring.util;",
6433 "import com.liferay.portal.util.PropsUtil;"
6434 },
6435 new String[] {
6436 "package " + _springUtilPackage + ";",
6437 "import " + _propsUtilPackage + ".PropsUtil;"
6438 });
6439
6440
6442 File ejbFile = new File(_implDir + "/" + StringUtil.replace(_springUtilPackage, ".", "/") + "/SpringUtil.java");
6443
6444 FileUtil.write(ejbFile, content, true);
6445 }
6446
6447 private void _createSpringXML() throws IOException {
6448 StringMaker sm = new StringMaker();
6449
6450 for (int i = 0; i < _ejbList.size(); i++) {
6451 Entity entity = (Entity)_ejbList.get(i);
6452
6453 if (entity.hasLocalService()) {
6454 _createSpringXMLSession(entity, sm, _LOCAL);
6455 }
6456
6457 if (entity.hasRemoteService()) {
6458 _createSpringXMLSession(entity, sm, _REMOTE);
6459 }
6460
6461 _createSpringXMLSession(entity, sm);
6462 }
6463
6464 File xmlFile = new File(_springFileName);
6465
6466 if (!xmlFile.exists()) {
6467 String content =
6468 "<?xml version=\"1.0\"?>\n" +
6469 "<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">\n" +
6470 "\n" +
6471 "<beans>\n" +
6472 "</beans>";
6473
6474 FileUtil.write(xmlFile, content);
6475 }
6476
6477 String oldContent = FileUtil.read(xmlFile);
6478 String newContent = _fixSpringXML(oldContent);
6479
6480 int x = oldContent.indexOf("<beans>");
6481 int y = oldContent.lastIndexOf("</beans>");
6482
6483 int firstSession = newContent.indexOf(
6484 "<bean id=\"" + _packagePath + ".service.", x);
6485
6486 int lastSession = newContent.lastIndexOf(
6487 "<bean id=\"" + _packagePath + ".service.", y);
6488
6489 if (firstSession == -1 || firstSession > y) {
6490 x = newContent.indexOf("</beans>");
6491 newContent =
6492 newContent.substring(0, x) + sm.toString() +
6493 newContent.substring(x, newContent.length());
6494 }
6495 else {
6496 firstSession = newContent.lastIndexOf("<bean", firstSession) - 1;
6497 lastSession = newContent.indexOf("</bean>", lastSession) + 8;
6498
6499 newContent =
6500 newContent.substring(0, firstSession) + sm.toString() +
6501 newContent.substring(lastSession, newContent.length());
6502 }
6503
6504 if (!oldContent.equals(newContent)) {
6505 FileUtil.write(xmlFile, newContent);
6506 }
6507 }
6508
6509 private void _createSpringXMLSession(Entity entity, StringMaker sm, int sessionType) {
6510 List txRequiredList = entity.getTxRequiredList();
6511
6512 sm.append("\t<bean id=\"" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "Service.professional\" class=\"" + _packagePath + ".service.impl." + entity.getName() + _getSessionTypeName(sessionType) + "ServiceImpl\" lazy-init=\"true\" />\n");
6513
6514 sm.append("\t<bean id=\"" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "Service.transaction\" class=\"org.springframework.transaction.interceptor.TransactionProxyFactoryBean\" lazy-init=\"true\">\n");
6515 sm.append("\t\t<property name=\"transactionManager\">\n");
6516 sm.append("\t\t\t<ref bean=\"" + entity.getTXManager() + "\" />\n");
6517 sm.append("\t\t</property>\n");
6518 sm.append("\t\t<property name=\"target\">\n");
6519 sm.append("\t\t\t<ref bean=\"" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "Service.professional\" />\n");
6520 sm.append("\t\t</property>\n");
6521 sm.append("\t\t<property name=\"transactionAttributes\">\n");
6522 sm.append("\t\t\t<props>\n");
6523
6524 for (int i = 0; i < txRequiredList.size(); i++) {
6525 String txRequired = (String)txRequiredList.get(i);
6526
6527 sm.append("\t\t\t\t<prop key=\"" + txRequired + "\">PROPAGATION_REQUIRED</prop>\n");
6528 }
6529
6530 sm.append("\t\t\t\t<prop key=\"add*\">PROPAGATION_REQUIRED</prop>\n");
6531 sm.append("\t\t\t\t<prop key=\"check*\">PROPAGATION_REQUIRED</prop>\n");
6532 sm.append("\t\t\t\t<prop key=\"clear*\">PROPAGATION_REQUIRED</prop>\n");
6533 sm.append("\t\t\t\t<prop key=\"delete*\">PROPAGATION_REQUIRED</prop>\n");
6534 sm.append("\t\t\t\t<prop key=\"set*\">PROPAGATION_REQUIRED</prop>\n");
6535 sm.append("\t\t\t\t<prop key=\"update*\">PROPAGATION_REQUIRED</prop>\n");
6536 sm.append("\t\t\t\t<prop key=\"*\">PROPAGATION_SUPPORTS,readOnly</prop>\n");
6537 sm.append("\t\t\t</props>\n");
6539 sm.append("\t\t</property>\n");
6540 sm.append("\t</bean>\n");
6541
6542 sm.append("\t<bean id=\"" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory\" class=\"" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "ServiceFactory\" lazy-init=\"true\">\n");
6543 sm.append("\t\t<property name=\"service\">\n");
6544 sm.append("\t\t\t<ref bean=\"" + _packagePath + ".service." + entity.getName() + _getSessionTypeName(sessionType) + "Service.transaction\" />\n");
6545 sm.append("\t\t</property>\n");
6546 sm.append("\t</bean>\n");
6547 }
6548
6549 private void _createSpringXMLSession(Entity entity, StringMaker sm) {
6550 if (entity.hasColumns()) {
6551 sm.append("\t<bean id=\"" + _packagePath + ".service.persistence." + entity.getName() + "PersistenceImpl\" class=\"" + entity.getPersistenceClass() + "\" lazy-init=\"true\">\n");
6552 sm.append("\t\t<property name=\"dataSource\">\n");
6553 sm.append("\t\t\t<ref bean=\"" + entity.getDataSource() + "\" />\n");
6554 sm.append("\t\t</property>\n");
6555 sm.append("\t\t<property name=\"sessionFactory\">\n");
6556 sm.append("\t\t\t<ref bean=\"" + entity.getSessionFactory() + "\" />\n");
6557 sm.append("\t\t</property>\n");
6558 sm.append("\t</bean>\n");
6559
6560 sm.append("\t<bean id=\"" + _packagePath + ".service.persistence." + entity.getName() + "Util\" class=\"" + _packagePath + ".service.persistence." + entity.getName() + "Util\" lazy-init=\"true\">\n");
6561 sm.append("\t\t<property name=\"persistence\">\n");
6562 sm.append("\t\t\t<ref bean=\"" + _packagePath + ".service.persistence." + entity.getName() + "PersistenceImpl\" />\n");
6563 sm.append("\t\t</property>\n");
6564 sm.append("\t</bean>\n");
6565 }
6566 }
6567
6568 private void _createSQLIndexes() throws IOException {
6569 if (!FileUtil.exists(_sqlDir)) {
6570 return;
6571 }
6572
6573
6575 File sqlFile = new File(_sqlDir + "/indexes.sql");
6576
6577 if (!sqlFile.exists()) {
6578 FileUtil.write(sqlFile, "");
6579 }
6580
6581 Map indexSQLs = new TreeMap();
6582
6583 BufferedReader br = new BufferedReader(new FileReader(sqlFile));
6584
6585 while (true) {
6586 String indexSQL = br.readLine();
6587
6588 if (indexSQL == null) {
6589 break;
6590 }
6591
6592 if (Validator.isNotNull(indexSQL.trim())) {
6593 int pos = indexSQL.indexOf(" on ");
6594
6595 String indexSpec = indexSQL.substring(pos + 4);
6596
6597 indexSQLs.put(indexSpec, indexSQL);
6598 }
6599 }
6600
6601 br.close();
6602
6603
6605 File propsFile = new File(_sqlDir + "/indexes.properties");
6606
6607 if (!propsFile.exists()) {
6608 FileUtil.write(propsFile, "");
6609 }
6610
6611 Map indexProps = new TreeMap();
6612
6613 br = new BufferedReader(new FileReader(propsFile));
6614
6615 while (true) {
6616 String indexMapping = br.readLine();
6617
6618 if (indexMapping == null) {
6619 break;
6620 }
6621
6622 if (Validator.isNotNull(indexMapping.trim())) {
6623 String[] splitIndexMapping = indexMapping.split("\\=");
6624
6625 indexProps.put(splitIndexMapping[1], splitIndexMapping[0]);
6626 }
6627 }
6628
6629 br.close();
6630
6631
6633 for (int i = 0; i < _ejbList.size(); i++) {
6634 Entity entity = (Entity)_ejbList.get(i);
6635
6636 List finderList = entity.getFinderList();
6637
6638 for (int j = 0; j < finderList.size(); j++) {
6639 EntityFinder finder = (EntityFinder)finderList.get(j);
6640
6641 if (finder.isDBIndex()) {
6642 StringMaker sm = new StringMaker();
6643
6644 sm.append(entity.getTable() + " (");
6645
6646 List finderColsList = finder.getColumns();
6647
6648 for (int k = 0; k < finderColsList.size(); k++) {
6649 EntityColumn col = (EntityColumn)finderColsList.get(k);
6650
6651 sm.append(col.getDBName());
6652
6653 if ((k + 1) != finderColsList.size()) {
6654 sm.append(", ");
6655 }
6656 }
6657
6658 sm.append(");");
6659
6660 String indexSpec = sm.toString();
6661
6662 String indexHash =
6663 Integer.toHexString(indexSpec.hashCode()).toUpperCase();
6664
6665 String indexName = "IX_" + indexHash;
6666
6667 sm = new StringMaker();
6668
6669 sm.append("create index " + indexName + " on ");
6670 sm.append(indexSpec);
6671
6672 indexSQLs.put(indexSpec, sm.toString());
6673
6674 String finderName =
6675 entity.getTable() + StringPool.PERIOD +
6676 finder.getName();
6677
6678 indexProps.put(finderName, indexName);
6679 }
6680 }
6681 }
6682
6683 StringMaker sm = new StringMaker();
6684
6685 Iterator itr = indexSQLs.values().iterator();
6686
6687 String prevEntityName = null;
6688
6689 while (itr.hasNext()) {
6690 String indexSQL = (String)itr.next();
6691
6692 String entityName = indexSQL.split(" ")[4];
6693
6694 if ((prevEntityName != null) &&
6695 (!prevEntityName.equals(entityName))) {
6696
6697 sm.append("\n");
6698 }
6699
6700 sm.append(indexSQL);
6701
6702 if (itr.hasNext()) {
6703 sm.append("\n");
6704 }
6705
6706 prevEntityName = entityName;
6707 }
6708
6709 FileUtil.write(sqlFile, sm.toString(), true);
6710
6711
6713 sm = new StringMaker();
6714
6715 itr = indexProps.keySet().iterator();
6716
6717 prevEntityName = null;
6718
6719 while (itr.hasNext()) {
6720 String finderName = (String)itr.next();
6721
6722 String indexName = (String)indexProps.get(finderName);
6723
6724 String entityName = finderName.split("\\.")[0];
6725
6726 if ((prevEntityName != null) &&
6727 (!prevEntityName.equals(entityName))) {
6728
6729 sm.append("\n");
6730 }
6731
6732 sm.append(indexName + StringPool.EQUAL + finderName);
6733
6734 if (itr.hasNext()) {
6735 sm.append("\n");
6736 }
6737
6738 prevEntityName = entityName;
6739 }
6740
6741 FileUtil.write(propsFile, sm.toString(), true);
6742 }
6743
6744 private void _createSQLSequences() throws IOException {
6745 if (!FileUtil.exists(_sqlDir)) {
6746 return;
6747 }
6748
6749 File sqlFile = new File(_sqlDir + "/sequences.sql");
6750
6751 if (!sqlFile.exists()) {
6752 FileUtil.write(sqlFile, "");
6753 }
6754
6755 Set sequenceSQLs = new TreeSet();
6756
6757 BufferedReader br = new BufferedReader(new FileReader(sqlFile));
6758
6759 while (true) {
6760 String sequenceSQL = br.readLine();
6761
6762 if (sequenceSQL == null) {
6763 break;
6764 }
6765
6766 if (Validator.isNotNull(sequenceSQL)) {
6767 sequenceSQLs.add(sequenceSQL);
6768 }
6769 }
6770
6771 br.close();
6772
6773 for (int i = 0; i < _ejbList.size(); i++) {
6774 Entity entity = (Entity)_ejbList.get(i);
6775
6776 List columnList = entity.getColumnList();
6777
6778 for (int j = 0; j < columnList.size(); j++) {
6779 EntityColumn column = (EntityColumn)columnList.get(j);
6780
6781 if ("sequence".equals(column.getIdType())) {
6782 StringMaker sm = new StringMaker();
6783
6784 String sequenceName = column.getIdParam();
6785
6786 if (sequenceName.length() > 30) {
6787 sequenceName = sequenceName.substring(0, 30);
6788 }
6789
6790 sm.append("create sequence " + sequenceName + ";");
6791
6792 String sequenceSQL = sm.toString();
6793
6794 if (!sequenceSQLs.contains(sequenceSQL)) {
6795 sequenceSQLs.add(sequenceSQL);
6796 }
6797 }
6798 }
6799 }
6800
6801 StringMaker sm = new StringMaker();
6802
6803 Iterator itr = sequenceSQLs.iterator();
6804
6805 while (itr.hasNext()) {
6806 String sequenceSQL = (String)itr.next();
6807
6808 sm.append(sequenceSQL);
6809
6810 if (itr.hasNext()) {
6811 sm.append("\n");
6812 }
6813 }
6814
6815 FileUtil.write(sqlFile, sm.toString(), true);
6816 }
6817
6818 private void _createSQLTables() throws IOException {
6819 if (!FileUtil.exists(_sqlDir)) {
6820 return;
6821 }
6822
6823 File sqlFile = new File(_sqlDir + "/" + _sqlFileName);
6824
6825 if (!sqlFile.exists()) {
6826 FileUtil.write(sqlFile, StringPool.BLANK);
6827 }
6828
6829 for (int i = 0; i < _ejbList.size(); i++) {
6830 Entity entity = (Entity)_ejbList.get(i);
6831
6832 String createTableSQL = _getCreateTableSQL(entity);
6833
6834 if (Validator.isNotNull(createTableSQL)) {
6835 _createSQLTables(sqlFile, createTableSQL, entity, true);
6836
6837 File updateSQLFile = new File(_sqlDir + "/update-4.2.0-4.3.0.sql");
6838
6839 if (updateSQLFile.exists()) {
6840 _createSQLTables(updateSQLFile, createTableSQL, entity, false);
6841 }
6842 }
6843 }
6844 }
6845
6846 private void _createSQLTables(File sqlFile, String newCreateTableString, Entity entity, boolean addMissingTables) throws IOException {
6847 if (!sqlFile.exists()) {
6848 FileUtil.write(sqlFile, StringPool.BLANK);
6849 }
6850
6851 String content = FileUtil.read(sqlFile);
6852
6853 int x = content.indexOf(_CREATE_TABLE + entity.getTable() + " (");
6854 int y = content.indexOf(");", x);
6855
6856 if (x != -1) {
6857 String oldCreateTableString = content.substring(x + 1, y);
6858
6859 if (!oldCreateTableString.equals(newCreateTableString)) {
6860 content =
6861 content.substring(0, x) + newCreateTableString +
6862 content.substring(y + 2, content.length());
6863
6864 FileUtil.write(sqlFile, content);
6865 }
6866 }
6867 else if (addMissingTables) {
6868 StringMaker sm = new StringMaker();
6869
6870 BufferedReader br = new BufferedReader(new StringReader(content));
6871
6872 String line = null;
6873 boolean appendNewTable = true;
6874
6875 while ((line = br.readLine()) != null) {
6876 if (appendNewTable && line.startsWith(_CREATE_TABLE)) {
6877 x = _CREATE_TABLE.length();
6878 y = line.indexOf(" ", x);
6879
6880 String tableName = line.substring(x, y);
6881
6882 if (tableName.compareTo(entity.getTable()) > 0) {
6883 sm.append(newCreateTableString + "\n\n");
6884
6885 appendNewTable = false;
6886 }
6887 }
6888
6889 sm.append(line).append('\n');
6890 }
6891
6892 if (appendNewTable) {
6893 sm.append("\n" + newCreateTableString);
6894 }
6895
6896 br.close();
6897
6898 FileUtil.write(sqlFile, sm.toString(), true);
6899 }
6900 }
6901
6902 private String _fixHBMXML(String content) throws IOException {
6903 StringMaker sm = new StringMaker();
6904
6905 BufferedReader br = new BufferedReader(new StringReader(content));
6906
6907 String line = null;
6908
6909 while ((line = br.readLine()) != null) {
6910 if (line.startsWith("\t<class name=\"")) {
6911 line = StringUtil.replace(
6912 line,
6913 new String[] {
6914 ".service.persistence.", "HBM\" table=\""
6915 },
6916 new String[] {
6917 ".model.", "\" table=\""
6918 });
6919
6920 if (line.indexOf(".model.impl.") == -1) {
6921 line = StringUtil.replace(
6922 line,
6923 new String[] {
6924 ".model.", "\" table=\""
6925 },
6926 new String[] {
6927 ".model.impl.", "Impl\" table=\""
6928 });
6929 }
6930 }
6931
6932 sm.append(line);
6933 sm.append('\n');
6934 }
6935
6936 br.close();
6937
6938 return sm.toString().trim();
6939 }
6940
6941 private String _fixSpringXML(String content) throws IOException {
6942 return StringUtil.replace(content, ".service.spring.", ".service.");
6943 }
6944
6945 private String _getClassName(Type type) {
6946 int dimensions = type.getDimensions();
6947 String name = type.getValue();
6948
6949 if (dimensions > 0) {
6950 StringMaker sm = new StringMaker();
6951
6952 for (int i = 0; i < dimensions; i++) {
6953 sm.append("[");
6954 }
6955
6956 if (name.equals("boolean")) {
6957 return sm.toString() + "Z";
6958 }
6959 else if (name.equals("byte")) {
6960 return sm.toString() + "B";
6961 }
6962 else if (name.equals("char")) {
6963 return sm.toString() + "C";
6964 }
6965 else if (name.equals("double")) {
6966 return sm.toString() + "D";
6967 }
6968 else if (name.equals("float")) {
6969 return sm.toString() + "F";
6970 }
6971 else if (name.equals("int")) {
6972 return sm.toString() + "I";
6973 }
6974 else if (name.equals("long")) {
6975 return sm.toString() + "J";
6976 }
6977 else if (name.equals("short")) {
6978 return sm.toString() + "S";
6979 }
6980 else {
6981 return sm.toString() + "L" + name + ";";
6982 }
6983 }
6984
6985 return name;
6986 }
6987
6988 private String _getCreateTableSQL(Entity entity) {
6989 List pkList = entity.getPKList();
6990 List regularColList = entity.getRegularColList();
6991
6992 if (regularColList.size() == 0) {
6993 return null;
6994 }
6995
6996 StringMaker sm = new StringMaker();
6997
6998 sm.append(_CREATE_TABLE + entity.getTable() + " (\n");
6999
7000 for (int i = 0; i < regularColList.size(); i++) {
7001 EntityColumn col = (EntityColumn)regularColList.get(i);
7002
7003 String colName = col.getName();
7004 String colType = col.getType();
7005 String colIdType = col.getIdType();
7006
7007 sm.append("\t" + col.getDBName());
7008 sm.append(" ");
7009
7010 if (colType.equalsIgnoreCase("boolean")) {
7011 sm.append("BOOLEAN");
7012 }
7013 else if (colType.equalsIgnoreCase("double") ||
7014 colType.equalsIgnoreCase("float")) {
7015
7016 sm.append("DOUBLE");
7017 }
7018 else if (colType.equals("int") ||
7019 colType.equals("Integer") ||
7020 colType.equalsIgnoreCase("short")) {
7021
7022 sm.append("INTEGER");
7023 }
7024 else if (colType.equalsIgnoreCase("long")) {
7025 sm.append("LONG");
7026 }
7027 else if (colType.equals("String")) {
7028 Map hints = ModelHintsUtil.getHints(_packagePath + ".model." + entity.getName(), colName);
7029
7030 int maxLength = 75;
7031
7032 if (hints != null) {
7033 maxLength = GetterUtil.getInteger(
7034 (String)hints.get("max-length"), maxLength);
7035 }
7036
7037 if (maxLength < 4000) {
7038 sm.append("VARCHAR(" + maxLength + ")");
7039 }
7040 else if (maxLength == 4000) {
7041 sm.append("STRING");
7042 }
7043 else if (maxLength > 4000) {
7044 sm.append("TEXT");
7045 }
7046 }
7047 else if (colType.equals("Date")) {
7048 sm.append("DATE null");
7049 }
7050 else {
7051 sm.append("invalid");
7052 }
7053
7054 if (col.isPrimary()) {
7055 sm.append(" not null");
7056
7057 if (!entity.hasCompoundPK()) {
7058 sm.append(" primary key");
7059 }
7060 }
7061 else if (colType.equals("String")) {
7062 sm.append(" null");
7063 }
7064
7065 if (Validator.isNotNull(colIdType) &&
7066 colIdType.equals("identity")) {
7067
7068 sm.append(" IDENTITY");
7069 }
7070
7071 if (((i + 1) != regularColList.size()) ||
7072 (entity.hasCompoundPK())) {
7073
7074 sm.append(",");
7075 }
7076
7077 sm.append("\n");
7078 }
7079
7080 if (entity.hasCompoundPK()) {
7081 sm.append("\tprimary key (");
7082
7083 for (int j = 0; j < pkList.size(); j++) {
7084 EntityColumn pk = (EntityColumn)pkList.get(j);
7085
7086 sm.append(pk.getDBName());
7087
7088 if ((j + 1) != pkList.size()) {
7089 sm.append(", ");
7090 }
7091 }
7092
7093 sm.append(")\n");
7094 }
7095
7096 sm.append(");");
7097
7098 return sm.toString();
7099 }
7100
7101 private String _getDimensions(Type type) {
7102 String dimensions = "";
7103
7104 for (int i = 0; i < type.getDimensions(); i++) {
7105 dimensions += "[]";
7106 }
7107
7108 return dimensions;
7109 }
7110
7111 private JavaClass _getJavaClass(String fileName) throws IOException {
7112 int pos = fileName.indexOf(_implDir + "/") + _implDir.length();
7113
7114 String srcFile = fileName.substring(pos + 1, fileName.length());
7115 String className = StringUtil.replace(
7116 srcFile.substring(0, srcFile.length() - 5), "/", ".");
7117
7118 JavaDocBuilder builder = new JavaDocBuilder();
7119
7120 builder.addSource(new File(fileName));
7121
7122 return builder.getClassByName(className);
7123 }
7124
7125 private String _getNoSuchEntityException(Entity entity) {
7126 String noSuchEntityException = entity.getName();
7127
7128 if (Validator.isNull(entity.getPortletShortName()) || noSuchEntityException.startsWith(entity.getPortletShortName())) {
7129 noSuchEntityException = noSuchEntityException.substring(entity.getPortletShortName().length(), noSuchEntityException.length());
7130 }
7131
7132 noSuchEntityException = "NoSuch" + noSuchEntityException;
7133
7134 return noSuchEntityException;
7135 }
7136
7137 private String _getPrimitiveObj(String type) {
7138 if (type.equals("boolean")) {
7139 return "Boolean";
7140 }
7141 else if (type.equals("double")) {
7142 return "Double";
7143 }
7144 else if (type.equals("float")) {
7145 return "Float";
7146 }
7147 else if (type.equals("int")) {
7148 return "Integer";
7149 }
7150 else if (type.equals("long")) {
7151 return "Long";
7152 }
7153 else if (type.equals("short")) {
7154 return "Short";
7155 }
7156 else {
7157 return type;
7158 }
7159 }
7160
7161 private String _getSessionTypeName(int sessionType) {
7162 if (sessionType == _LOCAL) {
7163 return "Local";
7164 }
7165 else {
7166 return "";
7167 }
7168 }
7169
7170 private String _getSqlType(String model, String field, String type) {
7171 if (type.equals("boolean") || type.equals("Boolean")) {
7172 return "BOOLEAN";
7173 }
7174 else if (type.equals("double") || type.equals("Double")) {
7175 return "DOUBLE";
7176 }
7177 else if (type.equals("float") || type.equals("Float")) {
7178 return "FLOAT";
7179 }
7180 else if (type.equals("int") || type.equals("Integer")) {
7181 return "INTEGER";
7182 }
7183 else if (type.equals("long") || type.equals("Long")) {
7184 return "BIGINT";
7185 }
7186 else if (type.equals("short") || type.equals("Short")) {
7187 return "INTEGER";
7188 }
7189 else if (type.equals("Date")) {
7190 return "TIMESTAMP";
7191 }
7192 else if (type.equals("String")) {
7193 Map hints = ModelHintsUtil.getHints(model, field);
7194
7195 if (hints != null) {
7196 int maxLength = GetterUtil.getInteger(
7197 (String)hints.get("max-length"));
7198
7199 if (maxLength == 2000000) {
7200 return "CLOB";
7201 }
7202 }
7203
7204 return "VARCHAR";
7205 }
7206 else {
7207 return null;
7208 }
7209 }
7210
7211 private boolean _hasHttpMethods(JavaClass javaClass) {
7212 JavaMethod[] methods = javaClass.getMethods();
7213
7214 for (int i = 0; i < methods.length; i++) {
7215 JavaMethod javaMethod = methods[i];
7216
7217 if (!javaMethod.isConstructor() && javaMethod.isPublic() &&
7218 _isCustomMethod(javaMethod)) {
7219
7220 return true;
7221 }
7222 }
7223
7224 return false;
7225 }
7226
7227 private boolean _hasSoapMethods(JavaClass javaClass) {
7228 JavaMethod[] methods = javaClass.getMethods();
7229
7230 for (int i = 0; i < methods.length; i++) {
7231 JavaMethod javaMethod = methods[i];
7232
7233 if (!javaMethod.isConstructor() && javaMethod.isPublic() &&
7234 _isCustomMethod(javaMethod) && _isSoapMethod(javaMethod)) {
7235
7236 return true;
7237 }
7238 }
7239
7240 return false;
7241 }
7242
7243 private boolean _isCustomMethod(JavaMethod method) {
7244 String methodName = method.getName();
7245
7246 if (methodName.equals("hasAdministrator") ||
7247 methodName.equals("ejbCreate") ||
7248 methodName.equals("ejbRemove") ||
7249 methodName.equals("ejbActivate") ||
7250 methodName.equals("ejbPassivate") ||
7251 methodName.equals("getSessionContext") ||
7252 methodName.equals("setSessionContext") ||
7253 methodName.equals("hashCode") ||
7254 methodName.equals("getClass") ||
7255 methodName.equals("wait") ||
7256 methodName.equals("equals") ||
7257 methodName.equals("toString") ||
7258 methodName.equals("notify") ||
7259 methodName.equals("notifyAll")) {
7260
7261 return false;
7262 }
7263 else if (methodName.equals("getPermissionChecker")) {
7264 return false;
7265 }
7266 else if (methodName.equals("getUser") &&
7267 method.getParameters().length == 0) {
7268
7269 return false;
7270 }
7271 else if (methodName.equals("getUserId") &&
7272 method.getParameters().length == 0) {
7273
7274 return false;
7275 }
7276 else {
7277 return true;
7278 }
7279 }
7280
7281 private boolean _isSoapMethod(JavaMethod method) {
7282 String returnValueName = method.getReturns().getValue();
7283
7284 if (returnValueName.startsWith("java.io") ||
7285 returnValueName.equals("java.util.Map") ||
7286 returnValueName.equals("java.util.Properties") ||
7287 returnValueName.startsWith("javax")) {
7288
7289 return false;
7290 }
7291
7292 JavaParameter[] parameters = method.getParameters();
7293
7294 for (int i = 0; i < parameters.length; i++) {
7295 JavaParameter javaParameter = parameters[i];
7296
7297 String parameterTypeName =
7298 javaParameter.getType().getValue() +
7299 _getDimensions(javaParameter.getType());
7300
7301 if ((parameterTypeName.indexOf(
7302 "com.liferay.portal.model.") != -1) ||
7303 (parameterTypeName.equals(
7304 "com.liferay.portal.theme.ThemeDisplay")) ||
7305 (parameterTypeName.equals(
7306 "com.liferay.portlet.PortletPreferencesImpl")) ||
7307 parameterTypeName.startsWith("java.io") ||
7308 parameterTypeName.startsWith("java.util.Map") ||
7311 parameterTypeName.startsWith("java.util.Properties") ||
7312 parameterTypeName.startsWith("javax")) {
7313
7314 return false;
7315 }
7316 }
7317
7318 return true;
7319 }
7320
7321 private boolean _requiresNullCheck(EntityColumn col) {
7322 return !col.isPrimitiveType();
7323 }
7324
7325 private static final int _REMOTE = 0;
7326
7327 private static final int _LOCAL = 1;
7328
7329 private static final String _CREATE_TABLE = "create table ";
7330
7331 private static final String _DEFAULT_CLASS_COMMENTS = "ServiceBuilder generated this class. Modifications in this class will be overwritten the next time is generated.";
7332
7333 private Set _badTableNames;
7334 private Set _badCmpFields;
7335 private String _hbmFileName;
7336 private String _modelHintsFileName;
7337 private String _springFileName;
7338 private String _springDataSourceFileName;
7339 private String _apiDir;
7340 private String _implDir;
7341 private String _jsonFileName;
7342 private String _sqlDir;
7343 private String _sqlFileName;
7344 private boolean _autoNamespaceTables;
7345 private String _baseModelImplPackage;
7346 private String _basePersistencePackage;
7347 private String _beanLocatorUtilPackage;
7348 private String _principalBeanPackage;
7349 private String _propsUtilPackage;
7350 private String _springHibernatePackage;
7351 private String _springUtilPackage;
7352 private String _portletName = StringPool.BLANK;
7353 private String _portletShortName = StringPool.BLANK;
7354 private String _portletPackageName = StringPool.BLANK;
7355 private String _outputPath;
7356 private String _serviceOutputPath;
7357 private String _packagePath;
7358 private List _ejbList;
7359
7360}