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          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(long classNameId, String tableName, String name)
114         throws PortalException, SystemException {
115 
116         ExpandoTable table = expandoTableLocalService.getTable(
117             classNameId, tableName);
118 
119         deleteColumn(table.getTableId(), name);
120     }
121 
122     public void deleteColumn(String className, String tableName, String name)
123         throws PortalException, SystemException {
124 
125         long classNameId = PortalUtil.getClassNameId(className);
126 
127         deleteColumn(classNameId, tableName, 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(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 (Validator.isNotNull(defaultData)) {
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 }