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