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