1   /**
2    * Copyright (c) 2000-2009 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.portlet.expando.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.auth.CompanyThreadLocal;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.expando.ColumnNameException;
31  import com.liferay.portlet.expando.ColumnTypeException;
32  import com.liferay.portlet.expando.DuplicateColumnNameException;
33  import com.liferay.portlet.expando.model.ExpandoColumn;
34  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
35  import com.liferay.portlet.expando.model.ExpandoTable;
36  import com.liferay.portlet.expando.model.ExpandoTableConstants;
37  import com.liferay.portlet.expando.model.ExpandoValue;
38  import com.liferay.portlet.expando.model.impl.ExpandoValueImpl;
39  import com.liferay.portlet.expando.service.base.ExpandoColumnLocalServiceBaseImpl;
40  
41  import java.util.Date;
42  import java.util.List;
43  
44  /**
45   * <a href="ExpandoColumnLocalServiceImpl.java.html"><b><i>View Source</i></b>
46   * </a>
47   *
48   * @author Raymond Augé
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class ExpandoColumnLocalServiceImpl
53      extends ExpandoColumnLocalServiceBaseImpl {
54  
55      public ExpandoColumn addColumn(long tableId, String name, int type)
56          throws PortalException, SystemException {
57  
58          return addColumn(tableId, name, type, null);
59      }
60  
61      public ExpandoColumn addColumn(
62              long tableId, String name, int type, Object defaultData)
63          throws PortalException, SystemException {
64  
65          ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
66  
67          ExpandoValue value = validate(0, tableId, name, type, defaultData);
68  
69          long columnId = counterLocalService.increment();
70  
71          ExpandoColumn column = expandoColumnPersistence.create(columnId);
72  
73          column.setCompanyId(table.getCompanyId());
74          column.setTableId(tableId);
75          column.setName(name);
76          column.setType(type);
77          column.setDefaultData(value.getData());
78  
79          expandoColumnPersistence.update(column, false);
80  
81          // Resources
82  
83          long companyId = CompanyThreadLocal.getCompanyId();
84  
85          resourceLocalService.addResources(
86              companyId, 0, 0, ExpandoColumn.class.getName(),
87              column.getColumnId(), false, false, false);
88  
89          return column;
90      }
91  
92      public void deleteColumn(long columnId)
93          throws PortalException, SystemException {
94  
95          // Values
96  
97          expandoValueLocalService.deleteColumnValues(columnId);
98  
99          // Column
100 
101         expandoColumnPersistence.remove(columnId);
102     }
103 
104     public void deleteColumn(long tableId, String name)
105         throws PortalException, SystemException {
106 
107         ExpandoColumn column = expandoColumnPersistence.findByT_N(
108             tableId, name);
109 
110         deleteColumn(column.getColumnId());
111     }
112 
113     public void deleteColumn(String className, String tableName, String name)
114         throws PortalException, SystemException {
115 
116         long classNameId = PortalUtil.getClassNameId(className);
117 
118         deleteColumn(classNameId, tableName, name);
119     }
120 
121     public void deleteColumn(long classNameId, String tableName, String name)
122         throws PortalException, SystemException {
123 
124         ExpandoTable table = expandoTableLocalService.getTable(
125             classNameId, tableName);
126 
127         deleteColumn(table.getTableId(), name);
128     }
129 
130     public void deleteColumns(long tableId)
131         throws PortalException, SystemException {
132 
133         List<ExpandoColumn> columns = expandoColumnPersistence.findByTableId(
134             tableId);
135 
136         for (ExpandoColumn column : columns) {
137             deleteColumn(column.getColumnId());
138         }
139     }
140 
141     public void deleteColumns(String className, String tableName)
142         throws PortalException, SystemException {
143 
144         long classNameId = PortalUtil.getClassNameId(className);
145 
146         deleteColumns(classNameId, tableName);
147     }
148 
149     public void deleteColumns(long classNameId, String tableName)
150         throws PortalException, SystemException {
151 
152         ExpandoTable table = expandoTableLocalService.getTable(
153             classNameId, tableName);
154 
155         deleteColumns(table.getTableId());
156     }
157 
158     public ExpandoColumn getColumn(long columnId)
159         throws PortalException, SystemException {
160 
161         return expandoColumnPersistence.findByPrimaryKey(columnId);
162     }
163 
164     public ExpandoColumn getColumn(long tableId, String name)
165         throws PortalException, SystemException {
166 
167         return expandoColumnPersistence.findByT_N(tableId, name);
168     }
169 
170     public ExpandoColumn getColumn(
171             String className, String tableName, String name)
172         throws SystemException {
173 
174         long classNameId = PortalUtil.getClassNameId(className);
175 
176         return getColumn(classNameId, tableName, name);
177     }
178 
179     public ExpandoColumn getColumn(
180             long classNameId, String tableName, String name)
181         throws SystemException {
182 
183         long companyId = CompanyThreadLocal.getCompanyId();
184 
185         return expandoColumnFinder.fetchByTC_TC_TN_CN(
186             companyId, classNameId, tableName, name);
187     }
188 
189     public List<ExpandoColumn> getColumns(long tableId)
190         throws SystemException {
191 
192         return expandoColumnPersistence.findByTableId(tableId);
193     }
194 
195     public List<ExpandoColumn> getColumns(String className, String tableName)
196         throws SystemException {
197 
198         long classNameId = PortalUtil.getClassNameId(className);
199 
200         return getColumns(classNameId, tableName);
201     }
202 
203     public List<ExpandoColumn> getColumns(long classNameId, String tableName)
204         throws SystemException {
205 
206         long companyId = CompanyThreadLocal.getCompanyId();
207 
208         return expandoColumnFinder.findByTC_TC_TN(
209             companyId, classNameId, tableName);
210     }
211 
212     public int getColumnsCount(long tableId) throws SystemException {
213         return expandoColumnPersistence.countByTableId(tableId);
214     }
215 
216     public int getColumnsCount(String className, String tableName)
217         throws SystemException {
218 
219         long classNameId = PortalUtil.getClassNameId(className);
220 
221         return getColumnsCount(classNameId, tableName);
222     }
223 
224     public int getColumnsCount(long classNameId, String tableName)
225         throws SystemException {
226 
227         long companyId = CompanyThreadLocal.getCompanyId();
228 
229         return expandoColumnFinder.countByTC_TC_TN(
230             companyId, classNameId, tableName);
231     }
232 
233     public ExpandoColumn getDefaultTableColumn(String className, String name)
234         throws SystemException {
235 
236         long classNameId = PortalUtil.getClassNameId(className);
237 
238         return getColumn(
239             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
240     }
241 
242     public ExpandoColumn getDefaultTableColumn(long classNameId, String name)
243         throws SystemException {
244 
245         return getColumn(
246             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
247     }
248 
249     public List<ExpandoColumn> getDefaultTableColumns(String className)
250         throws SystemException {
251 
252         long classNameId = PortalUtil.getClassNameId(className);
253 
254         return getColumns(
255             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
256     }
257 
258     public List<ExpandoColumn> getDefaultTableColumns(long classNameId)
259         throws SystemException {
260 
261         long companyId = CompanyThreadLocal.getCompanyId();
262 
263         return expandoColumnFinder.findByTC_TC_TN(
264             companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
265     }
266 
267     public int getDefaultTableColumnsCount(String className)
268         throws SystemException {
269 
270         long classNameId = PortalUtil.getClassNameId(className);
271 
272         return getColumnsCount(
273             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
274     }
275 
276     public int getDefaultTableColumnsCount(long classNameId)
277         throws SystemException {
278 
279         long companyId = CompanyThreadLocal.getCompanyId();
280 
281         return expandoColumnFinder.countByTC_TC_TN(
282             companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
283     }
284 
285     public ExpandoColumn updateColumn(long columnId, String name, int type)
286         throws PortalException, SystemException {
287 
288         return updateColumn(columnId, name, type, null);
289     }
290 
291     public ExpandoColumn updateColumn(
292             long columnId, String name, int type, Object defaultData)
293         throws PortalException, SystemException {
294 
295         ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
296             columnId);
297 
298         ExpandoValue value = validate(
299             columnId, column.getTableId(), name, type, defaultData);
300 
301         column.setName(name);
302         column.setType(type);
303         column.setDefaultData(value.getData());
304 
305         expandoColumnPersistence.update(column, false);
306 
307         return column;
308     }
309 
310     public ExpandoColumn updateTypeSettings(long columnId, String typeSettings)
311         throws PortalException, SystemException {
312 
313         ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
314             columnId);
315 
316         column.setTypeSettings(typeSettings);
317 
318         expandoColumnPersistence.update(column, false);
319 
320         return column;
321     }
322 
323     protected ExpandoValue validate(
324             long columnId, long tableId, String name, int type,
325             Object defaultData)
326         throws PortalException, SystemException {
327 
328         if (Validator.isNull(name)) {
329             throw new ColumnNameException();
330         }
331 
332         ExpandoColumn column = expandoColumnPersistence.fetchByT_N(
333             tableId, name);
334 
335         if ((column != null) && (column.getColumnId() != columnId)) {
336             throw new DuplicateColumnNameException();
337         }
338 
339         if ((type != ExpandoColumnConstants.BOOLEAN) &&
340             (type != ExpandoColumnConstants.BOOLEAN_ARRAY) &&
341             (type != ExpandoColumnConstants.DATE) &&
342             (type != ExpandoColumnConstants.DATE_ARRAY) &&
343             (type != ExpandoColumnConstants.DOUBLE) &&
344             (type != ExpandoColumnConstants.DOUBLE_ARRAY) &&
345             (type != ExpandoColumnConstants.FLOAT) &&
346             (type != ExpandoColumnConstants.FLOAT_ARRAY) &&
347             (type != ExpandoColumnConstants.INTEGER) &&
348             (type != ExpandoColumnConstants.INTEGER_ARRAY) &&
349             (type != ExpandoColumnConstants.LONG) &&
350             (type != ExpandoColumnConstants.LONG_ARRAY) &&
351             (type != ExpandoColumnConstants.SHORT) &&
352             (type != ExpandoColumnConstants.SHORT_ARRAY) &&
353             (type != ExpandoColumnConstants.STRING) &&
354             (type != ExpandoColumnConstants.STRING_ARRAY)) {
355 
356             throw new ColumnTypeException();
357         }
358 
359         ExpandoValue value = new ExpandoValueImpl();
360 
361         if (Validator.isNotNull(defaultData)) {
362             value.setColumnId(columnId);
363 
364             if (type == ExpandoColumnConstants.BOOLEAN) {
365                 value.setBoolean((Boolean)defaultData);
366             }
367             else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
368                 value.setBooleanArray((boolean[])defaultData);
369             }
370             else if (type == ExpandoColumnConstants.DATE) {
371                 value.setDate((Date)defaultData);
372             }
373             else if (type == ExpandoColumnConstants.DATE_ARRAY) {
374                 value.setDateArray((Date[])defaultData);
375             }
376             else if (type == ExpandoColumnConstants.DOUBLE) {
377                 value.setDouble((Double)defaultData);
378             }
379             else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
380                 value.setDoubleArray((double[])defaultData);
381             }
382             else if (type == ExpandoColumnConstants.FLOAT) {
383                 value.setFloat((Float)defaultData);
384             }
385             else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
386                 value.setFloatArray((float[])defaultData);
387             }
388             else if (type == ExpandoColumnConstants.INTEGER) {
389                 value.setInteger((Integer)defaultData);
390             }
391             else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
392                 value.setIntegerArray((int[])defaultData);
393             }
394             else if (type == ExpandoColumnConstants.LONG) {
395                 value.setLong((Long)defaultData);
396             }
397             else if (type == ExpandoColumnConstants.LONG_ARRAY) {
398                 value.setLongArray((long[])defaultData);
399             }
400             else if (type == ExpandoColumnConstants.SHORT) {
401                 value.setShort((Short)defaultData);
402             }
403             else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
404                 value.setShortArray((short[])defaultData);
405             }
406             else if (type == ExpandoColumnConstants.STRING) {
407                 value.setString((String)defaultData);
408             }
409             else if (type == ExpandoColumnConstants.STRING_ARRAY) {
410                 value.setStringArray((String[])defaultData);
411             }
412         }
413 
414         return value;
415     }
416 
417 }