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