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.Collection;
033 import java.util.Collections;
034 import java.util.Date;
035 import java.util.List;
036
037
041 public class ExpandoColumnLocalServiceImpl
042 extends ExpandoColumnLocalServiceBaseImpl {
043
044 @Override
045 public ExpandoColumn addColumn(long tableId, String name, int type)
046 throws PortalException, SystemException {
047
048 return addColumn(tableId, name, type, null);
049 }
050
051 @Override
052 public ExpandoColumn addColumn(
053 long tableId, String name, int type, Object defaultData)
054 throws PortalException, SystemException {
055
056
057
058 ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
059
060 ExpandoValue value = validate(0, tableId, name, type, defaultData);
061
062 long columnId = counterLocalService.increment();
063
064 ExpandoColumn column = expandoColumnPersistence.create(columnId);
065
066 column.setCompanyId(table.getCompanyId());
067 column.setTableId(tableId);
068 column.setName(name);
069 column.setType(type);
070 column.setDefaultData(value.getData());
071
072 expandoColumnPersistence.update(column, false);
073
074
075
076 resourceLocalService.addResources(
077 table.getCompanyId(), 0, 0, ExpandoColumn.class.getName(),
078 column.getColumnId(), false, false, false);
079
080 return column;
081 }
082
083 @Override
084 public void deleteColumn(ExpandoColumn column) throws SystemException {
085
086
087
088 expandoColumnPersistence.remove(column);
089
090
091
092 expandoValueLocalService.deleteColumnValues(column.getColumnId());
093 }
094
095 @Override
096 public void deleteColumn(long columnId)
097 throws PortalException, SystemException {
098
099 ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
100 columnId);
101
102 deleteColumn(column);
103 }
104
105 @Override
106 public void deleteColumn(
107 long companyId, long classNameId, String tableName, String name)
108 throws PortalException, SystemException {
109
110 ExpandoTable table = expandoTableLocalService.getTable(
111 companyId, classNameId, tableName);
112
113 deleteColumn(table.getTableId(), name);
114 }
115
116 @Override
117 public void deleteColumn(long tableId, String name) throws SystemException {
118 List<ExpandoColumn> columns = expandoColumnPersistence.findByT_N(
119 tableId, name);
120
121 if (!columns.isEmpty()) {
122 deleteColumn(columns.get(0));
123 }
124 }
125
126 @Override
127 public void deleteColumn(
128 long companyId, String className, String tableName, String name)
129 throws PortalException, SystemException {
130
131 long classNameId = PortalUtil.getClassNameId(className);
132
133 deleteColumn(companyId, classNameId, tableName, name);
134 }
135
136 @Override
137 public void deleteColumns(long tableId) throws SystemException {
138 List<ExpandoColumn> columns = expandoColumnPersistence.findByTableId(
139 tableId);
140
141 for (ExpandoColumn column : columns) {
142 deleteColumn(column);
143 }
144 }
145
146 @Override
147 public void deleteColumns(
148 long companyId, long classNameId, String tableName)
149 throws PortalException, SystemException {
150
151 ExpandoTable table = expandoTableLocalService.getTable(
152 companyId, classNameId, tableName);
153
154 deleteColumns(table.getTableId());
155 }
156
157 @Override
158 public void deleteColumns(
159 long companyId, String className, String tableName)
160 throws PortalException, SystemException {
161
162 long classNameId = PortalUtil.getClassNameId(className);
163
164 deleteColumns(companyId, classNameId, tableName);
165 }
166
167 @Override
168 public ExpandoColumn getColumn(long columnId)
169 throws PortalException, SystemException {
170
171 return expandoColumnPersistence.findByPrimaryKey(columnId);
172 }
173
174 @Override
175 public ExpandoColumn getColumn(
176 long companyId, long classNameId, String tableName, String name)
177 throws SystemException {
178
179 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
180 companyId, classNameId, tableName);
181
182 if (table == null) {
183 return null;
184 }
185
186 List<ExpandoColumn> columns = expandoColumnPersistence.findByT_N(
187 table.getTableId(), name);
188
189 if (!columns.isEmpty()) {
190 return columns.get(0);
191 }
192
193 return null;
194 }
195
196 @Override
197 public ExpandoColumn getColumn(long tableId, String name)
198 throws SystemException {
199
200 List<ExpandoColumn> columns = expandoColumnPersistence.findByT_N(
201 tableId, name);
202
203 if (!columns.isEmpty()) {
204 return columns.get(0);
205 }
206
207 return null;
208 }
209
210 @Override
211 public ExpandoColumn getColumn(
212 long companyId, String className, String tableName, String name)
213 throws SystemException {
214
215 long classNameId = PortalUtil.getClassNameId(className);
216
217 return getColumn(companyId, classNameId, tableName, name);
218 }
219
220 @Override
221 public List<ExpandoColumn> getColumns(long tableId) throws SystemException {
222 return expandoColumnPersistence.findByTableId(tableId);
223 }
224
225 @Override
226 public List<ExpandoColumn> getColumns(
227 long tableId, Collection<String> names)
228 throws SystemException {
229
230 return expandoColumnPersistence.findByT_N(
231 tableId, names.toArray(new String[names.size()]));
232 }
233
234 @Override
235 public List<ExpandoColumn> getColumns(
236 long companyId, long classNameId, String tableName)
237 throws SystemException {
238
239 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
240 companyId, classNameId, tableName);
241
242 if (table == null) {
243 return Collections.emptyList();
244 }
245
246 return expandoColumnPersistence.findByTableId(table.getTableId());
247 }
248
249 @Override
250 public List<ExpandoColumn> getColumns(
251 long companyId, long classNameId, String tableName,
252 Collection<String> names)
253 throws SystemException {
254
255 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
256 companyId, classNameId, tableName);
257
258 if (table == null) {
259 return Collections.emptyList();
260 }
261
262 return expandoColumnPersistence.findByT_N(
263 table.getTableId(), names.toArray(new String[names.size()]));
264 }
265
266 @Override
267 public List<ExpandoColumn> getColumns(
268 long companyId, String className, String tableName)
269 throws SystemException {
270
271 long classNameId = PortalUtil.getClassNameId(className);
272
273 return getColumns(companyId, classNameId, tableName);
274 }
275
276 @Override
277 public List<ExpandoColumn> getColumns(
278 long companyId, String className, String tableName,
279 Collection<String> columnNames)
280 throws SystemException {
281
282 long classNameId = PortalUtil.getClassNameId(className);
283
284 return getColumns(companyId, classNameId, tableName, columnNames);
285 }
286
287 @Override
288 public int getColumnsCount(long tableId) throws SystemException {
289 return expandoColumnPersistence.countByTableId(tableId);
290 }
291
292 @Override
293 public int getColumnsCount(
294 long companyId, long classNameId, String tableName)
295 throws SystemException {
296
297 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
298 companyId, classNameId, tableName);
299
300 if (table == null) {
301 return 0;
302 }
303
304 return expandoColumnPersistence.countByTableId(table.getTableId());
305 }
306
307 @Override
308 public int getColumnsCount(
309 long companyId, String className, String tableName)
310 throws SystemException {
311
312 long classNameId = PortalUtil.getClassNameId(className);
313
314 return getColumnsCount(companyId, classNameId, tableName);
315 }
316
317 @Override
318 public ExpandoColumn getDefaultTableColumn(
319 long companyId, long classNameId, String name)
320 throws SystemException {
321
322 return getColumn(
323 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME,
324 name);
325 }
326
327 @Override
328 public ExpandoColumn getDefaultTableColumn(
329 long companyId, String className, String name)
330 throws SystemException {
331
332 long classNameId = PortalUtil.getClassNameId(className);
333
334 return getColumn(
335 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME,
336 name);
337 }
338
339 @Override
340 public List<ExpandoColumn> getDefaultTableColumns(
341 long companyId, long classNameId)
342 throws SystemException {
343
344 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
345 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
346
347 if (table == null) {
348 return Collections.emptyList();
349 }
350
351 return expandoColumnPersistence.findByTableId(table.getTableId());
352 }
353
354 @Override
355 public List<ExpandoColumn> getDefaultTableColumns(
356 long companyId, String className)
357 throws SystemException {
358
359 long classNameId = PortalUtil.getClassNameId(className);
360
361 return getColumns(
362 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
363 }
364
365 @Override
366 public int getDefaultTableColumnsCount(long companyId, long classNameId)
367 throws SystemException {
368
369 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
370 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
371
372 if (table == null) {
373 return 0;
374 }
375
376 return expandoColumnPersistence.countByTableId(table.getTableId());
377 }
378
379 @Override
380 public int getDefaultTableColumnsCount(long companyId, String className)
381 throws SystemException {
382
383 long classNameId = PortalUtil.getClassNameId(className);
384
385 return getColumnsCount(
386 companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
387 }
388
389 @Override
390 public ExpandoColumn updateColumn(long columnId, String name, int type)
391 throws PortalException, SystemException {
392
393 return expandoColumnLocalService.updateColumn(
394 columnId, name, type, null);
395 }
396
397 @Override
398 public ExpandoColumn updateColumn(
399 long columnId, String name, int type, Object defaultData)
400 throws PortalException, SystemException {
401
402 ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
403 columnId);
404
405 ExpandoValue value = validate(
406 columnId, column.getTableId(), name, type, defaultData);
407
408 column.setName(name);
409 column.setType(type);
410 column.setDefaultData(value.getData());
411
412 expandoColumnPersistence.update(column, false);
413
414 return column;
415 }
416
417 @Override
418 public ExpandoColumn updateTypeSettings(long columnId, String typeSettings)
419 throws PortalException, SystemException {
420
421 ExpandoColumn column = expandoColumnPersistence.findByPrimaryKey(
422 columnId);
423
424 column.setTypeSettings(typeSettings);
425
426 expandoColumnPersistence.update(column, false);
427
428 return column;
429 }
430
431 protected ExpandoValue validate(
432 long columnId, long tableId, String name, int type,
433 Object defaultData)
434 throws PortalException, SystemException {
435
436 if (Validator.isNull(name)) {
437 throw new ColumnNameException();
438 }
439
440 List<ExpandoColumn> columns = expandoColumnPersistence.findByT_N(
441 tableId, name);
442
443 if (!columns.isEmpty()) {
444 ExpandoColumn column = columns.get(0);
445
446 if (column.getColumnId() != columnId) {
447 throw new DuplicateColumnNameException();
448 }
449 }
450
451 if ((type != ExpandoColumnConstants.BOOLEAN) &&
452 (type != ExpandoColumnConstants.BOOLEAN_ARRAY) &&
453 (type != ExpandoColumnConstants.DATE) &&
454 (type != ExpandoColumnConstants.DATE_ARRAY) &&
455 (type != ExpandoColumnConstants.DOUBLE) &&
456 (type != ExpandoColumnConstants.DOUBLE_ARRAY) &&
457 (type != ExpandoColumnConstants.FLOAT) &&
458 (type != ExpandoColumnConstants.FLOAT_ARRAY) &&
459 (type != ExpandoColumnConstants.INTEGER) &&
460 (type != ExpandoColumnConstants.INTEGER_ARRAY) &&
461 (type != ExpandoColumnConstants.LONG) &&
462 (type != ExpandoColumnConstants.LONG_ARRAY) &&
463 (type != ExpandoColumnConstants.NUMBER) &&
464 (type != ExpandoColumnConstants.NUMBER_ARRAY) &&
465 (type != ExpandoColumnConstants.SHORT) &&
466 (type != ExpandoColumnConstants.SHORT_ARRAY) &&
467 (type != ExpandoColumnConstants.STRING) &&
468 (type != ExpandoColumnConstants.STRING_ARRAY)) {
469
470 throw new ColumnTypeException();
471 }
472
473 ExpandoValue value = new ExpandoValueImpl();
474
475 if (defaultData != null) {
476 value.setColumnId(columnId);
477
478 if (type == ExpandoColumnConstants.BOOLEAN) {
479 value.setBoolean((Boolean)defaultData);
480 }
481 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
482 value.setBooleanArray((boolean[])defaultData);
483 }
484 else if (type == ExpandoColumnConstants.DATE) {
485 value.setDate((Date)defaultData);
486 }
487 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
488 value.setDateArray((Date[])defaultData);
489 }
490 else if (type == ExpandoColumnConstants.DOUBLE) {
491 value.setDouble((Double)defaultData);
492 }
493 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
494 value.setDoubleArray((double[])defaultData);
495 }
496 else if (type == ExpandoColumnConstants.FLOAT) {
497 value.setFloat((Float)defaultData);
498 }
499 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
500 value.setFloatArray((float[])defaultData);
501 }
502 else if (type == ExpandoColumnConstants.INTEGER) {
503 value.setInteger((Integer)defaultData);
504 }
505 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
506 value.setIntegerArray((int[])defaultData);
507 }
508 else if (type == ExpandoColumnConstants.LONG) {
509 value.setLong((Long)defaultData);
510 }
511 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
512 value.setLongArray((long[])defaultData);
513 }
514 else if (type == ExpandoColumnConstants.NUMBER) {
515 value.setNumber((Number)defaultData);
516 }
517 else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
518 value.setNumberArray((Number[])defaultData);
519 }
520 else if (type == ExpandoColumnConstants.SHORT) {
521 value.setShort((Short)defaultData);
522 }
523 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
524 value.setShortArray((short[])defaultData);
525 }
526 else if (type == ExpandoColumnConstants.STRING) {
527 value.setString((String)defaultData);
528 }
529 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
530 value.setStringArray((String[])defaultData);
531 }
532 }
533
534 return value;
535 }
536
537 }