1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.util.PortalUtil;
21  import com.liferay.portlet.expando.ColumnNameException;
22  import com.liferay.portlet.expando.ColumnTypeException;
23  import com.liferay.portlet.expando.DuplicateColumnNameException;
24  import com.liferay.portlet.expando.model.ExpandoColumn;
25  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
26  import com.liferay.portlet.expando.model.ExpandoTable;
27  import com.liferay.portlet.expando.model.ExpandoTableConstants;
28  import com.liferay.portlet.expando.service.base.ExpandoColumnLocalServiceBaseImpl;
29  
30  import java.util.Collections;
31  import java.util.List;
32  
33  /**
34   * <a href="ExpandoColumnLocalServiceImpl.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Raymond Augé
38   * @author Brian Wing Shun Chan
39   */
40  public class ExpandoColumnLocalServiceImpl
41      extends ExpandoColumnLocalServiceBaseImpl {
42  
43      public ExpandoColumn addColumn(long tableId, String name, int type)
44          throws PortalException, SystemException {
45  
46          validate(0, tableId, name, type);
47  
48          long columnId = counterLocalService.increment();
49  
50          ExpandoColumn column = expandoColumnPersistence.create(columnId);
51  
52          column.setTableId(tableId);
53          column.setName(name);
54          column.setType(type);
55  
56          expandoColumnPersistence.update(column, false);
57  
58          return column;
59      }
60  
61      public void deleteColumn(ExpandoColumn column) throws SystemException {
62  
63          // Column
64  
65          expandoColumnPersistence.remove(column);
66  
67          // Values
68  
69          expandoValueLocalService.deleteColumnValues(column.getColumnId());
70      }
71  
72      public void deleteColumn(long columnId)
73          throws PortalException, SystemException {
74  
75          ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
76              columnId);
77  
78          deleteColumn(column);
79      }
80  
81      public void deleteColumn(long tableId, String name)
82          throws PortalException, SystemException {
83  
84          ExpandoColumn column = expandoColumnPersistence.findByT_N(
85              tableId, name);
86  
87          deleteColumn(column);
88      }
89  
90      public void deleteColumn(long classNameId, String tableName, String name)
91          throws PortalException, SystemException {
92  
93          ExpandoTable table = expandoTablePersistence.findByC_N(
94              classNameId, tableName);
95  
96          deleteColumn(table.getTableId(), name);
97      }
98  
99      public void deleteColumn(String className, String tableName, String name)
100         throws PortalException, SystemException {
101 
102         long classNameId = PortalUtil.getClassNameId(className);
103 
104         deleteColumn(classNameId, tableName, name);
105     }
106 
107     public void deleteColumns(long tableId) throws SystemException {
108         List<ExpandoColumn> columns = expandoColumnPersistence.findByTableId(
109             tableId);
110 
111         for (ExpandoColumn column : columns) {
112             deleteColumn(column);
113         }
114     }
115 
116     public void deleteColumns(long classNameId, String tableName)
117         throws PortalException, SystemException {
118 
119         ExpandoTable table = expandoTablePersistence.findByC_N(
120             classNameId, tableName);
121 
122         deleteColumns(table.getTableId());
123     }
124 
125     public void deleteColumns(String className, String tableName)
126         throws PortalException, SystemException {
127 
128         long classNameId = PortalUtil.getClassNameId(className);
129 
130         deleteColumns(classNameId, tableName);
131     }
132 
133     public ExpandoColumn getColumn(long columnId)
134         throws PortalException, SystemException {
135 
136         return expandoColumnPersistence.findByPrimaryKey(columnId);
137     }
138 
139     public ExpandoColumn getColumn(long tableId, String name)
140         throws PortalException, SystemException {
141 
142         return expandoColumnPersistence.findByT_N(tableId, name);
143     }
144 
145     public ExpandoColumn getColumn(
146             long classNameId, String tableName, String name)
147         throws SystemException {
148 
149         ExpandoTable table = expandoTablePersistence.fetchByC_N(
150             classNameId, tableName);
151 
152         if (table == null) {
153             return null;
154         }
155 
156         return expandoColumnPersistence.fetchByT_N(table.getTableId(), name);
157     }
158 
159     public ExpandoColumn getColumn(
160             String className, String tableName, String name)
161         throws SystemException {
162 
163         long classNameId = PortalUtil.getClassNameId(className);
164 
165         return getColumn(classNameId, tableName, name);
166     }
167 
168     public List<ExpandoColumn> getColumns(long tableId)
169         throws SystemException {
170 
171         return expandoColumnPersistence.findByTableId(tableId);
172     }
173 
174     public List<ExpandoColumn> getColumns(long classNameId, String tableName)
175         throws SystemException {
176 
177         ExpandoTable table = expandoTablePersistence.fetchByC_N(
178             classNameId, tableName);
179 
180         if (table == null) {
181             return Collections.EMPTY_LIST;
182         }
183 
184         return expandoColumnPersistence.findByTableId(table.getTableId());
185     }
186 
187     public List<ExpandoColumn> getColumns(String className, String tableName)
188         throws SystemException {
189 
190         long classNameId = PortalUtil.getClassNameId(className);
191 
192         return getColumns(classNameId, tableName);
193     }
194 
195     public int getColumnsCount(long tableId) throws SystemException {
196         return expandoColumnPersistence.countByTableId(tableId);
197     }
198 
199     public int getColumnsCount(long classNameId, String tableName)
200         throws SystemException {
201 
202         ExpandoTable table = expandoTablePersistence.fetchByC_N(
203             classNameId, tableName);
204 
205         if (table == null) {
206             return 0;
207         }
208 
209         return expandoColumnPersistence.countByTableId(table.getTableId());
210     }
211 
212     public int getColumnsCount(String className, String tableName)
213         throws SystemException {
214 
215         long classNameId = PortalUtil.getClassNameId(className);
216 
217         return getColumnsCount(classNameId, tableName);
218     }
219 
220     public ExpandoColumn getDefaultTableColumn(long classNameId, String name)
221         throws SystemException {
222 
223         return getColumn(
224             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
225     }
226 
227     public ExpandoColumn getDefaultTableColumn(String className, String name)
228         throws SystemException {
229 
230         long classNameId = PortalUtil.getClassNameId(className);
231 
232         return getColumn(
233             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
234     }
235 
236     public List<ExpandoColumn> getDefaultTableColumns(long classNameId)
237         throws SystemException {
238 
239         ExpandoTable table = expandoTablePersistence.fetchByC_N(
240             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
241 
242         if (table == null) {
243             return Collections.EMPTY_LIST;
244         }
245 
246         return expandoColumnPersistence.findByTableId(table.getTableId());
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 int getDefaultTableColumnsCount(long classNameId)
259         throws SystemException {
260 
261         ExpandoTable table = expandoTablePersistence.fetchByC_N(
262             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
263 
264         if (table == null) {
265             return 0;
266         }
267 
268         return expandoColumnPersistence.countByTableId(table.getTableId());
269     }
270 
271     public int getDefaultTableColumnsCount(String className)
272         throws SystemException {
273 
274         long classNameId = PortalUtil.getClassNameId(className);
275 
276         return getColumnsCount(
277             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
278     }
279 
280     public ExpandoColumn updateColumn(long columnId, String name, int type)
281         throws PortalException, SystemException {
282 
283         ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
284             columnId);
285 
286         validate(columnId, column.getTableId(), name, type);
287 
288         column.setName(name);
289         column.setType(type);
290 
291         expandoColumnPersistence.update(column, false);
292 
293         return column;
294     }
295 
296     protected void validate(long columnId, long tableId, String name, int type)
297         throws PortalException, SystemException {
298 
299         if (Validator.isNull(name)) {
300             throw new ColumnNameException();
301         }
302 
303         ExpandoColumn column = expandoColumnPersistence.fetchByT_N(
304             tableId, name);
305 
306         if ((column != null) && (column.getColumnId() != columnId)) {
307             throw new DuplicateColumnNameException();
308         }
309 
310         if ((type != ExpandoColumnConstants.BOOLEAN) &&
311             (type != ExpandoColumnConstants.BOOLEAN_ARRAY) &&
312             (type != ExpandoColumnConstants.DATE) &&
313             (type != ExpandoColumnConstants.DATE_ARRAY) &&
314             (type != ExpandoColumnConstants.DOUBLE) &&
315             (type != ExpandoColumnConstants.DOUBLE_ARRAY) &&
316             (type != ExpandoColumnConstants.FLOAT) &&
317             (type != ExpandoColumnConstants.FLOAT_ARRAY) &&
318             (type != ExpandoColumnConstants.INTEGER) &&
319             (type != ExpandoColumnConstants.INTEGER_ARRAY) &&
320             (type != ExpandoColumnConstants.LONG) &&
321             (type != ExpandoColumnConstants.LONG_ARRAY) &&
322             (type != ExpandoColumnConstants.SHORT) &&
323             (type != ExpandoColumnConstants.SHORT_ARRAY) &&
324             (type != ExpandoColumnConstants.STRING) &&
325             (type != ExpandoColumnConstants.STRING_ARRAY)) {
326 
327             throw new ColumnTypeException();
328         }
329     }
330 
331 }