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 deleteColumns(long tableId)
82          throws PortalException, SystemException {
83  
84          List<ExpandoColumn> columns = expandoColumnPersistence.findByTableId(
85              tableId);
86  
87          for (ExpandoColumn column : columns) {
88              deleteColumn(column.getColumnId());
89          }
90      }
91  
92      public void deleteColumns(String className, String tableName)
93          throws PortalException, SystemException {
94  
95          long classNameId = PortalUtil.getClassNameId(className);
96  
97          deleteColumns(classNameId, tableName);
98      }
99  
100     public void deleteColumns(long classNameId, String tableName)
101         throws PortalException, SystemException {
102 
103         ExpandoTable table = expandoTablePersistence.findByC_N(
104             classNameId, tableName);
105 
106         deleteColumns(table.getTableId());
107     }
108 
109     public ExpandoColumn getColumn(long columnId)
110         throws PortalException, SystemException {
111 
112         return expandoColumnPersistence.findByPrimaryKey(columnId);
113     }
114 
115     public ExpandoColumn getColumn(long tableId, String name)
116         throws PortalException, SystemException {
117 
118         return expandoColumnPersistence.findByT_N(tableId, name);
119     }
120 
121     public ExpandoColumn getColumn(
122             String className, String tableName, String name)
123         throws PortalException, SystemException {
124 
125         long classNameId = PortalUtil.getClassNameId(className);
126 
127         return getColumn(classNameId, tableName, name);
128     }
129 
130     public ExpandoColumn getColumn(
131             long classNameId, String tableName, String name)
132         throws PortalException, SystemException {
133 
134         return expandoColumnFinder.findByTC_TN_CN(classNameId, tableName, name);
135     }
136 
137     public List<ExpandoColumn> getColumns(long tableId)
138         throws SystemException {
139 
140         return expandoColumnPersistence.findByTableId(tableId);
141     }
142 
143     public List<ExpandoColumn> getColumns(String className, String tableName)
144         throws SystemException {
145 
146         long classNameId = PortalUtil.getClassNameId(className);
147 
148         return getColumns(classNameId, tableName);
149     }
150 
151     public List<ExpandoColumn> getColumns(long classNameId, String tableName)
152         throws SystemException {
153 
154         return expandoColumnFinder.findByTC_TN(classNameId, tableName);
155     }
156 
157     public int getColumnsCount(long tableId) throws SystemException {
158         return expandoColumnPersistence.countByTableId(tableId);
159     }
160 
161     public int getColumnsCount(String className, String tableName)
162         throws SystemException {
163 
164         long classNameId = PortalUtil.getClassNameId(className);
165 
166         return getColumnsCount(classNameId, tableName);
167     }
168 
169     public int getColumnsCount(long classNameId, String tableName)
170         throws SystemException {
171 
172         return expandoColumnFinder.countByTC_TN(classNameId, tableName);
173     }
174 
175     public ExpandoColumn getDefaultTableColumn(String className, String name)
176         throws PortalException, SystemException {
177 
178         long classNameId = PortalUtil.getClassNameId(className);
179 
180         return getColumn(
181             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
182     }
183 
184     public ExpandoColumn getDefaultTableColumn(long classNameId, String name)
185         throws PortalException, SystemException {
186 
187         return getColumn(
188             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME, name);
189     }
190 
191     public List<ExpandoColumn> getDefaultTableColumns(String className)
192         throws SystemException {
193 
194         long classNameId = PortalUtil.getClassNameId(className);
195 
196         return getColumns(
197             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
198     }
199 
200     public List<ExpandoColumn> getDefaultTableColumns(long classNameId)
201         throws SystemException {
202 
203         return expandoColumnFinder.findByTC_TN(
204             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
205     }
206 
207     public int getDefaultTableColumnsCount(String className)
208         throws SystemException {
209 
210         long classNameId = PortalUtil.getClassNameId(className);
211 
212         return getColumnsCount(
213             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
214     }
215 
216     public int getDefaultTableColumnsCount(long classNameId)
217         throws SystemException {
218 
219         return expandoColumnFinder.countByTC_TN(
220             classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
221     }
222 
223     public ExpandoColumn updateColumn(long columnId, String name, int type)
224         throws PortalException, SystemException {
225 
226         ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
227             columnId);
228 
229         validate(columnId, column.getTableId(), name, type);
230 
231         column.setName(name);
232         column.setType(type);
233 
234         expandoColumnPersistence.update(column, false);
235 
236         return column;
237     }
238 
239     protected void validate(long columnId, long tableId, String name, int type)
240         throws PortalException, SystemException {
241 
242         if (Validator.isNull(name)) {
243             throw new ColumnNameException();
244         }
245 
246         ExpandoColumn column = expandoColumnPersistence.fetchByT_N(
247             tableId, name);
248 
249         if ((column != null) && (column.getColumnId() != columnId)) {
250             throw new DuplicateColumnNameException();
251         }
252 
253         if ((type != ExpandoColumnConstants.BOOLEAN) &&
254             (type != ExpandoColumnConstants.BOOLEAN_ARRAY) &&
255             (type != ExpandoColumnConstants.DATE) &&
256             (type != ExpandoColumnConstants.DATE_ARRAY) &&
257             (type != ExpandoColumnConstants.DOUBLE) &&
258             (type != ExpandoColumnConstants.DOUBLE_ARRAY) &&
259             (type != ExpandoColumnConstants.FLOAT) &&
260             (type != ExpandoColumnConstants.FLOAT_ARRAY) &&
261             (type != ExpandoColumnConstants.INTEGER) &&
262             (type != ExpandoColumnConstants.INTEGER_ARRAY) &&
263             (type != ExpandoColumnConstants.LONG) &&
264             (type != ExpandoColumnConstants.LONG_ARRAY) &&
265             (type != ExpandoColumnConstants.SHORT) &&
266             (type != ExpandoColumnConstants.SHORT_ARRAY) &&
267             (type != ExpandoColumnConstants.STRING) &&
268             (type != ExpandoColumnConstants.STRING_ARRAY)) {
269 
270             throw new ColumnTypeException();
271         }
272     }
273 
274 }