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