001
014
015 package com.liferay.portlet.expando.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.Validator;
020 import com.liferay.portal.util.PortalUtil;
021 import com.liferay.portlet.expando.ColumnNameException;
022 import com.liferay.portlet.expando.ColumnTypeException;
023 import com.liferay.portlet.expando.DuplicateColumnNameException;
024 import com.liferay.portlet.expando.model.ExpandoColumn;
025 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
026 import com.liferay.portlet.expando.model.ExpandoTable;
027 import com.liferay.portlet.expando.model.ExpandoTableConstants;
028 import com.liferay.portlet.expando.model.ExpandoValue;
029 import com.liferay.portlet.expando.model.impl.ExpandoValueImpl;
030 import com.liferay.portlet.expando.service.base.ExpandoColumnLocalServiceBaseImpl;
031
032 import java.util.Collections;
033 import java.util.Date;
034 import java.util.List;
035
036
040 public class ExpandoColumnLocalServiceImpl
041 extends ExpandoColumnLocalServiceBaseImpl {
042
043 public ExpandoColumn addColumn(long tableId, String name, int type)
044 throws PortalException, SystemException {
045
046 return addColumn(tableId, name, type, null);
047 }
048
049 public ExpandoColumn addColumn(
050 long tableId, String name, int type, Object defaultData)
051 throws PortalException, SystemException {
052
053
054
055 ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
056
057 ExpandoValue value = validate(0, tableId, name, type, defaultData);
058
059 long columnId = counterLocalService.increment();
060
061 ExpandoColumn column = expandoColumnPersistence.create(columnId);
062
063 column.setCompanyId(table.getCompanyId());
064 column.setTableId(tableId);
065 column.setName(name);
066 column.setType(type);
067 column.setDefaultData(value.getData());
068
069 expandoColumnPersistence.update(column, false);
070
071
072
073 resourceLocalService.addResources(
074 table.getCompanyId(), 0, 0, ExpandoColumn.class.getName(),
075 column.getColumnId(), false, false, false);
076
077 return column;
078 }
079
080 public void deleteColumn(ExpandoColumn column) throws SystemException {
081
082
083
084 expandoColumnPersistence.remove(column);
085
086
087
088 expandoValueLocalService.deleteColumnValues(column.getColumnId());
089 }
090
091 public void deleteColumn(long columnId)
092 throws PortalException, SystemException {
093
094 ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
095 columnId);
096
097 deleteColumn(column);
098 }
099
100 public void deleteColumn(
101 long companyId, long classNameId, String tableName, String name)
102 throws PortalException, SystemException {
103
104 ExpandoTable table = expandoTableLocalService.getTable(
105 companyId, classNameId, tableName);
106
107 deleteColumn(table.getTableId(), name);
108 }
109
110 public void deleteColumn(long tableId, String name)
111 throws PortalException, SystemException {
112
113 ExpandoColumn column = expandoColumnPersistence.findByT_N(
114 tableId, name);
115
116 deleteColumn(column);
117 }
118
119 public void deleteColumn(
120 long companyId, String className, String tableName, String name)
121 throws PortalException, SystemException {
122
123 long classNameId = PortalUtil.getClassNameId(className);
124
125 deleteColumn(companyId, classNameId, tableName, name);
126 }
127
128 public void deleteColumns(long tableId) throws SystemException {
129 List<ExpandoColumn> columns = expandoColumnPersistence.findByTableId(
130 tableId);
131
132 for (ExpandoColumn column : columns) {
133 deleteColumn(column);
134 }
135 }
136
137 public void deleteColumns(
138 long companyId, long classNameId, String tableName)
139 throws PortalException, SystemException {
140
141 ExpandoTable table = expandoTableLocalService.getTable(
142 companyId, classNameId, tableName);
143
144 deleteColumns(table.getTableId());
145 }
146
147 public void deleteColumns(
148 long companyId, String className, String tableName)
149 throws PortalException, SystemException {
150
151 long classNameId = PortalUtil.getClassNameId(className);
152
153 deleteColumns(companyId, classNameId, tableName);
154 }
155
156 public ExpandoColumn getColumn(long columnId)
157 throws PortalException, SystemException {
158
159 return expandoColumnPersistence.findByPrimaryKey(columnId);
160 }
161
162 public ExpandoColumn getColumn(
163 long companyId, long classNameId, String tableName, String name)
164 throws SystemException {
165
166 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
167 companyId, classNameId, tableName);
168
169 if (table == null) {
170 return null;
171 }
172
173 return expandoColumnPersistence.fetchByT_N(table.getTableId(), name);
174 }
175
176 public ExpandoColumn getColumn(long tableId, String name)
177 throws PortalException, SystemException {
178
179 return expandoColumnPersistence.findByT_N(tableId, name);
180 }
181
182 public ExpandoColumn getColumn(
183 long companyId, String className, String tableName, String name)
184 throws SystemException {
185
186 long classNameId = PortalUtil.getClassNameId(className);
187
188 return getColumn(companyId, classNameId, tableName, name);
189 }
190
191 public List<ExpandoColumn> getColumns(long tableId)
192 throws SystemException {
193
194 return expandoColumnPersistence.findByTableId(tableId);
195 }
196
197 public List<ExpandoColumn> getColumns(
198 long companyId, long classNameId, String tableName)
199 throws SystemException {
200
201 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
202 companyId, classNameId, tableName);
203
204 if (table == null) {
205 return Collections.EMPTY_LIST;
206 }
207
208 return expandoColumnPersistence.findByTableId(table.getTableId());
209 }
210
211 public List<ExpandoColumn> getColumns(
212 long companyId, String className, String tableName)
213 throws SystemException {
214
215 long classNameId = PortalUtil.getClassNameId(className);
216
217 return getColumns(companyId, classNameId, tableName);
218 }
219
220 public int getColumnsCount(long tableId) throws SystemException {
221 return expandoColumnPersistence.countByTableId(tableId);
222 }
223
224 public int getColumnsCount(
225 long companyId, long classNameId, String tableName)
226 throws SystemException {
227
228 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
229 companyId, classNameId, tableName);
230
231 if (table == null) {
232 return 0;
233 }
234
235 return expandoColumnPersistence.countByTableId(table.getTableId());
236 }
237
238 public int getColumnsCount(
239 long companyId, String className, String tableName)
240 throws SystemException {
241
242 long classNameId = PortalUtil.getClassNameId(className);
243
244 return getColumnsCount(companyId, classNameId, tableName);
245 }
246
247 public ExpandoColumn getDefaultTableColumn(
248 long companyId, long classNameId, String name)
249 throws SystemException {
250
251 return getColumn(
252 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME,
253 name);
254 }
255
256 public ExpandoColumn getDefaultTableColumn(
257 long companyId, String className, String name)
258 throws SystemException {
259
260 long classNameId = PortalUtil.getClassNameId(className);
261
262 return getColumn(
263 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME,
264 name);
265 }
266
267 public List<ExpandoColumn> getDefaultTableColumns(
268 long companyId, long classNameId)
269 throws SystemException {
270
271 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
272 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
273
274 if (table == null) {
275 return Collections.EMPTY_LIST;
276 }
277
278 return expandoColumnPersistence.findByTableId(table.getTableId());
279 }
280
281 public List<ExpandoColumn> getDefaultTableColumns(
282 long companyId, String className)
283 throws SystemException {
284
285 long classNameId = PortalUtil.getClassNameId(className);
286
287 return getColumns(
288 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
289 }
290
291 public int getDefaultTableColumnsCount(long companyId, long classNameId)
292 throws SystemException {
293
294 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
295 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
296
297 if (table == null) {
298 return 0;
299 }
300
301 return expandoColumnPersistence.countByTableId(table.getTableId());
302 }
303
304 public int getDefaultTableColumnsCount(long companyId, String className)
305 throws SystemException {
306
307 long classNameId = PortalUtil.getClassNameId(className);
308
309 return getColumnsCount(
310 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
311 }
312
313 public ExpandoColumn updateColumn(long columnId, String name, int type)
314 throws PortalException, SystemException {
315
316 return updateColumn(columnId, name, type, null);
317 }
318
319 public ExpandoColumn updateColumn(
320 long columnId, String name, int type, Object defaultData)
321 throws PortalException, SystemException {
322
323 ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
324 columnId);
325
326 ExpandoValue value = validate(
327 columnId, column.getTableId(), name, type, defaultData);
328
329 column.setName(name);
330 column.setType(type);
331 column.setDefaultData(value.getData());
332
333 expandoColumnPersistence.update(column, false);
334
335 return column;
336 }
337
338 public ExpandoColumn updateTypeSettings(long columnId, String typeSettings)
339 throws PortalException, SystemException {
340
341 ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
342 columnId);
343
344 column.setTypeSettings(typeSettings);
345
346 expandoColumnPersistence.update(column, false);
347
348 return column;
349 }
350
351 protected ExpandoValue validate(
352 long columnId, long tableId, String name, int type,
353 Object defaultData)
354 throws PortalException, SystemException {
355
356 if (Validator.isNull(name)) {
357 throw new ColumnNameException();
358 }
359
360 ExpandoColumn column = expandoColumnPersistence.fetchByT_N(
361 tableId, name);
362
363 if ((column != null) && (column.getColumnId() != columnId)) {
364 throw new DuplicateColumnNameException();
365 }
366
367 if ((type != ExpandoColumnConstants.BOOLEAN) &&
368 (type != ExpandoColumnConstants.BOOLEAN_ARRAY) &&
369 (type != ExpandoColumnConstants.DATE) &&
370 (type != ExpandoColumnConstants.DATE_ARRAY) &&
371 (type != ExpandoColumnConstants.DOUBLE) &&
372 (type != ExpandoColumnConstants.DOUBLE_ARRAY) &&
373 (type != ExpandoColumnConstants.FLOAT) &&
374 (type != ExpandoColumnConstants.FLOAT_ARRAY) &&
375 (type != ExpandoColumnConstants.INTEGER) &&
376 (type != ExpandoColumnConstants.INTEGER_ARRAY) &&
377 (type != ExpandoColumnConstants.LONG) &&
378 (type != ExpandoColumnConstants.LONG_ARRAY) &&
379 (type != ExpandoColumnConstants.SHORT) &&
380 (type != ExpandoColumnConstants.SHORT_ARRAY) &&
381 (type != ExpandoColumnConstants.STRING) &&
382 (type != ExpandoColumnConstants.STRING_ARRAY)) {
383
384 throw new ColumnTypeException();
385 }
386
387 ExpandoValue value = new ExpandoValueImpl();
388
389 if (defaultData != null) {
390 value.setColumnId(columnId);
391
392 if (type == ExpandoColumnConstants.BOOLEAN) {
393 value.setBoolean((Boolean)defaultData);
394 }
395 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
396 value.setBooleanArray((boolean[])defaultData);
397 }
398 else if (type == ExpandoColumnConstants.DATE) {
399 value.setDate((Date)defaultData);
400 }
401 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
402 value.setDateArray((Date[])defaultData);
403 }
404 else if (type == ExpandoColumnConstants.DOUBLE) {
405 value.setDouble((Double)defaultData);
406 }
407 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
408 value.setDoubleArray((double[])defaultData);
409 }
410 else if (type == ExpandoColumnConstants.FLOAT) {
411 value.setFloat((Float)defaultData);
412 }
413 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
414 value.setFloatArray((float[])defaultData);
415 }
416 else if (type == ExpandoColumnConstants.INTEGER) {
417 value.setInteger((Integer)defaultData);
418 }
419 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
420 value.setIntegerArray((int[])defaultData);
421 }
422 else if (type == ExpandoColumnConstants.LONG) {
423 value.setLong((Long)defaultData);
424 }
425 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
426 value.setLongArray((long[])defaultData);
427 }
428 else if (type == ExpandoColumnConstants.SHORT) {
429 value.setShort((Short)defaultData);
430 }
431 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
432 value.setShortArray((short[])defaultData);
433 }
434 else if (type == ExpandoColumnConstants.STRING) {
435 value.setString((String)defaultData);
436 }
437 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
438 value.setStringArray((String[])defaultData);
439 }
440 }
441
442 return value;
443 }
444
445 }