1   /**
2    * Copyright (c) 2000-2008 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.List;
39  
40  /**
41   * <a href="ExpandoColumnLocalServiceImpl.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Raymond Augé
45   * @author Brian Wing Shun Chan
46   *
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(String className, String tableName, String name)
91          throws PortalException, SystemException {
92  
93          long classNameId = PortalUtil.getClassNameId(className);
94  
95          deleteColumn(classNameId, tableName, name);
96      }
97  
98      public void deleteColumn(long classNameId, String tableName, String name)
99          throws PortalException, SystemException {
100 
101         ExpandoTable table = expandoTableLocalService.getTable(
102             classNameId, tableName);
103 
104         deleteColumn(table.getTableId(), 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(String className, String tableName)
119         throws PortalException, SystemException {
120 
121         long classNameId = PortalUtil.getClassNameId(className);
122 
123         deleteColumns(classNameId, tableName);
124     }
125 
126     public void deleteColumns(long classNameId, String tableName)
127         throws PortalException, SystemException {
128 
129         ExpandoTable table = expandoTablePersistence.findByC_N(
130             classNameId, tableName);
131 
132         deleteColumns(table.getTableId());
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             String className, String tableName, String name)
149         throws SystemException {
150 
151         long classNameId = PortalUtil.getClassNameId(className);
152 
153         return getColumn(classNameId, tableName, name);
154     }
155 
156     public ExpandoColumn getColumn(
157             long classNameId, String tableName, String name)
158         throws SystemException {
159 
160         return expandoColumnFinder.fetchByTC_TN_CN(
161             classNameId, tableName, name);
162     }
163 
164     public List<ExpandoColumn> getColumns(long tableId)
165         throws SystemException {
166 
167         return expandoColumnPersistence.findByTableId(tableId);
168     }
169 
170     public List<ExpandoColumn> getColumns(String className, String tableName)
171         throws SystemException {
172 
173         long classNameId = PortalUtil.getClassNameId(className);
174 
175         return getColumns(classNameId, tableName);
176     }
177 
178     public List<ExpandoColumn> getColumns(long classNameId, String tableName)
179         throws SystemException {
180 
181         return expandoColumnFinder.findByTC_TN(classNameId, tableName);
182     }
183 
184     public int getColumnsCount(long tableId) throws SystemException {
185         return expandoColumnPersistence.countByTableId(tableId);
186     }
187 
188     public int getColumnsCount(String className, String tableName)
189         throws SystemException {
190 
191         long classNameId = PortalUtil.getClassNameId(className);
192 
193         return getColumnsCount(classNameId, tableName);
194     }
195 
196     public int getColumnsCount(long classNameId, String tableName)
197         throws SystemException {
198 
199         return expandoColumnFinder.countByTC_TN(classNameId, tableName);
200     }
201 
202     public ExpandoColumn getDefaultTableColumn(String className, String name)
203         throws SystemException {
204 
205         long classNameId = PortalUtil.getClassNameId(className);
206 
207         return getColumn(
208             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
209     }
210 
211     public ExpandoColumn getDefaultTableColumn(long classNameId, String name)
212         throws SystemException {
213 
214         return getColumn(
215             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
216     }
217 
218     public List<ExpandoColumn> getDefaultTableColumns(String className)
219         throws SystemException {
220 
221         long classNameId = PortalUtil.getClassNameId(className);
222 
223         return getColumns(
224             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
225     }
226 
227     public List<ExpandoColumn> getDefaultTableColumns(long classNameId)
228         throws SystemException {
229 
230         return expandoColumnFinder.findByTC_TN(
231             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
232     }
233 
234     public int getDefaultTableColumnsCount(String className)
235         throws SystemException {
236 
237         long classNameId = PortalUtil.getClassNameId(className);
238 
239         return getColumnsCount(
240             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
241     }
242 
243     public int getDefaultTableColumnsCount(long classNameId)
244         throws SystemException {
245 
246         return expandoColumnFinder.countByTC_TN(
247             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
248     }
249 
250     public ExpandoColumn updateColumn(long columnId, String name, int type)
251         throws PortalException, SystemException {
252 
253         ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
254             columnId);
255 
256         validate(columnId, column.getTableId(), name, type);
257 
258         column.setName(name);
259         column.setType(type);
260 
261         expandoColumnPersistence.update(column, false);
262 
263         return column;
264     }
265 
266     protected void validate(long columnId, long tableId, String name, int type)
267         throws PortalException, SystemException {
268 
269         if (Validator.isNull(name)) {
270             throw new ColumnNameException();
271         }
272 
273         ExpandoColumn column = expandoColumnPersistence.fetchByT_N(
274             tableId, name);
275 
276         if ((column != null) && (column.getColumnId() != columnId)) {
277             throw new DuplicateColumnNameException();
278         }
279 
280         if ((type != ExpandoColumnConstants.BOOLEAN) &&
281             (type != ExpandoColumnConstants.BOOLEAN_ARRAY) &&
282             (type != ExpandoColumnConstants.DATE) &&
283             (type != ExpandoColumnConstants.DATE_ARRAY) &&
284             (type != ExpandoColumnConstants.DOUBLE) &&
285             (type != ExpandoColumnConstants.DOUBLE_ARRAY) &&
286             (type != ExpandoColumnConstants.FLOAT) &&
287             (type != ExpandoColumnConstants.FLOAT_ARRAY) &&
288             (type != ExpandoColumnConstants.INTEGER) &&
289             (type != ExpandoColumnConstants.INTEGER_ARRAY) &&
290             (type != ExpandoColumnConstants.LONG) &&
291             (type != ExpandoColumnConstants.LONG_ARRAY) &&
292             (type != ExpandoColumnConstants.SHORT) &&
293             (type != ExpandoColumnConstants.SHORT_ARRAY) &&
294             (type != ExpandoColumnConstants.STRING) &&
295             (type != ExpandoColumnConstants.STRING_ARRAY)) {
296 
297             throw new ColumnTypeException();
298         }
299     }
300 
301 }