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