1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.util.TextFormatter;
27  
28  import java.util.List;
29  
30  /**
31   * <a href="Entity.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   *
35   */
36  public class Entity {
37  
38      public static EntityColumn getColumn(String name, List columnList) {
39          int pos = columnList.indexOf(new EntityColumn(name));
40  
41          if (pos == -1) {
42              throw new RuntimeException("Column " + name + " not found");
43          }
44  
45          return (EntityColumn)columnList.get(pos);
46      }
47  
48      public Entity(String name) {
49          this(null, null, null, name, null, false, true, null, null, null, null,
50               null, null, null, null, null, null, null);
51      }
52  
53      public Entity(String packagePath, String portletName,
54                    String portletShortName, String name, String table,
55                    boolean localService, boolean remoteService,
56                    String persistenceClass, String dataSource,
57                    String sessionFactory, String txManager, List pkList,
58                    List regularColList, List collectionList, List columnList,
59                    EntityOrder order, List finderList, List txRequiredList) {
60  
61          _packagePath = packagePath;
62          _portletName = portletName;
63          _portletShortName = portletShortName;
64          _name = name;
65          _table = table;
66          _localService = localService;
67          _remoteService = remoteService;
68          _persistenceClass = persistenceClass;
69          _dataSource = GetterUtil.getString(dataSource, "liferayDataSource");
70          _sessionFactory = GetterUtil.getString(
71              sessionFactory, "liferaySessionFactory");
72          _txManager = GetterUtil.getString(
73              txManager, "liferayTransactionManager");
74          _pkList = pkList;
75          _regularColList = regularColList;
76          _collectionList = collectionList;
77          _columnList = columnList;
78          _order = order;
79          _finderList = finderList;
80          _txRequiredList = txRequiredList;
81      }
82  
83      public String getPackagePath() {
84          return _packagePath;
85      }
86  
87      public String getPortletName() {
88          return _portletName;
89      }
90  
91      public String getPortletShortName() {
92          return _portletShortName;
93      }
94  
95      public String getName() {
96          return _name;
97      }
98  
99      public String getNames() {
100         return TextFormatter.formatPlural(new String(_name));
101     }
102 
103     public String getVarName() {
104         return TextFormatter.format(_name, TextFormatter.I);
105     }
106 
107     public String getVarNames() {
108         return TextFormatter.formatPlural(new String(getVarName()));
109     }
110 
111     public String getTable() {
112         return _table;
113     }
114 
115     public boolean hasLocalService() {
116         return _localService;
117     }
118 
119     public boolean hasRemoteService() {
120         return _remoteService;
121     }
122 
123     public String getPersistenceClass() {
124         return _persistenceClass;
125     }
126 
127     public String getDataSource() {
128         return _dataSource;
129     }
130 
131     public String getSessionFactory() {
132         return _sessionFactory;
133     }
134 
135     public String getTXManager() {
136         return _txManager;
137     }
138 
139     public String getPKClassName() {
140         if (hasCompoundPK()) {
141             return _name + "PK";
142         }
143         else {
144             EntityColumn col = (EntityColumn)_pkList.get(0);
145 
146             return col.getType();
147         }
148     }
149 
150     public String getPKVarName() {
151         if (hasCompoundPK()) {
152             return getVarName() + "PK";
153         }
154         else {
155             EntityColumn col = (EntityColumn)_pkList.get(0);
156 
157             return col.getName();
158         }
159     }
160 
161     public boolean hasPrimitivePK() {
162         if (hasCompoundPK()) {
163             return false;
164         }
165         else {
166             EntityColumn col = (EntityColumn)_pkList.get(0);
167 
168             if (col.isPrimitiveType()) {
169                 return true;
170             }
171             else {
172                 return false;
173             }
174         }
175     }
176 
177     public boolean hasCompoundPK() {
178         if (_pkList.size() > 1) {
179             return true;
180         }
181         else {
182             return false;
183         }
184     }
185 
186     public List getPKList() {
187         return _pkList;
188     }
189 
190     public List getRegularColList() {
191         return _regularColList;
192     }
193 
194     public List getCollectionList() {
195         return _collectionList;
196     }
197 
198     public List getColumnList() {
199         return _columnList;
200     }
201 
202     public boolean hasColumns() {
203         if ((_columnList == null) || (_columnList.size() == 0)) {
204             return false;
205         }
206         else {
207             return true;
208         }
209     }
210 
211     public EntityOrder getOrder() {
212         return _order;
213     }
214 
215     public boolean isOrdered() {
216         if (_order != null) {
217             return true;
218         }
219         else {
220             return false;
221         }
222     }
223 
224     public List getFinderList() {
225         return _finderList;
226     }
227 
228     public List getTxRequiredList() {
229         return _txRequiredList;
230     }
231 
232     public EntityColumn getColumn(String name) {
233         return getColumn(name, _columnList);
234     }
235 
236     public EntityColumn getColumnByMappingTable(String mappingTable) {
237         for (int i = 0; i < _columnList.size(); i++) {
238             EntityColumn col = (EntityColumn)_columnList.get(i);
239 
240             if (col.getMappingTable() != null &&
241                 col.getMappingTable().equals(mappingTable)) {
242 
243                 return col;
244             }
245         }
246 
247         return null;
248     }
249 
250     public boolean equals(Object obj) {
251         Entity entity = (Entity)obj;
252 
253         String name = entity.getName();
254 
255         if (_name.equals(name)) {
256             return true;
257         }
258         else {
259             return false;
260         }
261     }
262 
263     private String _packagePath;
264     private String _portletName;
265     private String _portletShortName;
266     private String _name;
267     private String _table;
268     private boolean _localService;
269     private boolean _remoteService;
270     private String _persistenceClass;
271     private String _dataSource;
272     private String _sessionFactory;
273     private String _txManager;
274     private List _pkList;
275     private List _regularColList;
276     private List _collectionList;
277     private List _columnList;
278     private EntityOrder _order;
279     private List _finderList;
280     private List _txRequiredList;
281 
282 }