001
014
015 package com.liferay.portlet.expando.model.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.lar.ImportExportThreadLocal;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.search.Indexer;
022 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
023 import com.liferay.portal.kernel.util.UnicodeProperties;
024 import com.liferay.portal.security.auth.CompanyThreadLocal;
025 import com.liferay.portal.service.ServiceContext;
026 import com.liferay.portal.util.PropsValues;
027 import com.liferay.portlet.expando.model.ExpandoBridge;
028 import com.liferay.portlet.expando.model.ExpandoColumn;
029 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
030 import com.liferay.portlet.expando.model.ExpandoTable;
031 import com.liferay.portlet.expando.model.ExpandoTableConstants;
032 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
033 import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
034 import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
035 import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
036 import com.liferay.portlet.expando.service.ExpandoValueServiceUtil;
037
038 import java.io.Serializable;
039
040 import java.util.ArrayList;
041 import java.util.Collection;
042 import java.util.Collections;
043 import java.util.Enumeration;
044 import java.util.HashMap;
045 import java.util.List;
046 import java.util.Map;
047
048
051 public class ExpandoBridgeImpl implements ExpandoBridge {
052
053 public ExpandoBridgeImpl(long companyId, String className) {
054 this(companyId, className, 0);
055 }
056
057 public ExpandoBridgeImpl(long companyId, String className, long classPK) {
058 _companyId = companyId;
059
060 if (_companyId == 0) {
061 _companyId = CompanyThreadLocal.getCompanyId();
062 }
063
064 _className = className;
065 _classPK = classPK;
066
067 if (IndexerRegistryUtil.getIndexer(className) == null) {
068 setIndexEnabled(true);
069 }
070 }
071
072 public void addAttribute(String name) throws PortalException {
073 boolean secure =
074 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
075
076 if (ImportExportThreadLocal.isImportInProcess()) {
077 secure = false;
078 }
079
080 addAttribute(name, ExpandoColumnConstants.STRING, null, secure);
081 }
082
083 public void addAttribute(String name, boolean secure)
084 throws PortalException {
085
086 addAttribute(name, ExpandoColumnConstants.STRING, null, secure);
087 }
088
089 public void addAttribute(String name, int type) throws PortalException {
090 boolean secure =
091 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
092
093 if (ImportExportThreadLocal.isImportInProcess()) {
094 secure = false;
095 }
096
097 addAttribute(name, type, null, secure);
098 }
099
100 public void addAttribute(String name, int type, boolean secure)
101 throws PortalException {
102
103 addAttribute(name, type, null, secure);
104 }
105
106 public void addAttribute(
107 String name, int type, Serializable defaultValue)
108 throws PortalException {
109
110 boolean secure =
111 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
112
113 if (ImportExportThreadLocal.isImportInProcess()) {
114 secure = false;
115 }
116
117 addAttribute(name, type, defaultValue, secure);
118 }
119
120 public void addAttribute(
121 String name, int type, Serializable defaultValue, boolean secure)
122 throws PortalException {
123
124 try {
125 ExpandoTable table = ExpandoTableLocalServiceUtil.fetchDefaultTable(
126 _companyId, _className);
127
128 if (table == null) {
129 table = ExpandoTableLocalServiceUtil.addDefaultTable(
130 _companyId, _className);
131 }
132
133 if (secure) {
134 ExpandoColumnServiceUtil.addColumn(
135 table.getTableId(), name, type, defaultValue);
136 }
137 else {
138 ExpandoColumnLocalServiceUtil.addColumn(
139 table.getTableId(), name, type, defaultValue);
140 }
141 }
142 catch (Exception e) {
143 if (e instanceof PortalException) {
144 throw (PortalException)e;
145 }
146 else {
147 _log.error(e, e);
148 }
149 }
150 }
151
152 public Serializable getAttribute(String name) {
153 boolean secure =
154 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_READ_CHECK_BY_DEFAULT;
155
156 if (ImportExportThreadLocal.isExportInProcess()) {
157 secure = false;
158 }
159
160 return getAttribute(name, secure);
161 }
162
163 public Serializable getAttribute(String name, boolean secure) {
164 Serializable data = null;
165
166 try {
167 if (secure) {
168 data = ExpandoValueServiceUtil.getData(
169 _companyId, _className,
170 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK);
171 }
172 else {
173 data = ExpandoValueLocalServiceUtil.getData(
174 _companyId, _className,
175 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK);
176 }
177 }
178 catch (Exception e) {
179 if (_log.isDebugEnabled()) {
180 _log.debug(e, e);
181 }
182 }
183
184 return data;
185 }
186
187 public Serializable getAttributeDefault(String name) {
188 try {
189 ExpandoColumn column =
190 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
191 _companyId, _className, name);
192
193 return column.getDefaultValue();
194 }
195 catch (Exception e) {
196 _log.error(e, e);
197
198 return null;
199 }
200 }
201
202 public Enumeration<String> getAttributeNames() {
203 List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
204
205 try {
206 columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
207 _companyId, _className);
208 }
209 catch (Exception e) {
210 if (_log.isDebugEnabled()) {
211 _log.debug(e, e);
212 }
213 }
214
215 List<String> columnNames = new ArrayList<String>();
216
217 for (ExpandoColumn column : columns) {
218 columnNames.add(column.getName());
219 }
220
221 return Collections.enumeration(columnNames);
222 }
223
224 public UnicodeProperties getAttributeProperties(String name) {
225 try {
226 ExpandoColumn column =
227 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
228 _companyId, _className, name);
229
230 return column.getTypeSettingsProperties();
231 }
232 catch (Exception e) {
233 if (_log.isDebugEnabled()) {
234 _log.debug("Properties for " + name, e);
235 }
236
237 return new UnicodeProperties(true);
238 }
239 }
240
241 public Map<String, Serializable> getAttributes() {
242 boolean secure =
243 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_READ_CHECK_BY_DEFAULT;
244
245 if (ImportExportThreadLocal.isExportInProcess()) {
246 secure = false;
247 }
248
249 return getAttributes(secure);
250 }
251
252 public Map<String, Serializable> getAttributes(boolean secure) {
253 Map<String, Serializable> attributes =
254 new HashMap<String, Serializable>();
255
256 List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
257
258 try {
259 columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
260 _companyId, _className);
261 }
262 catch (Exception e) {
263 if (_log.isDebugEnabled()) {
264 _log.debug(e, e);
265 }
266 }
267
268 for (ExpandoColumn column : columns) {
269 attributes.put(
270 column.getName(), getAttribute(column.getName(), secure));
271 }
272
273 return attributes;
274 }
275
276 public Map<String, Serializable> getAttributes(Collection<String> names) {
277 boolean secure =
278 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_READ_CHECK_BY_DEFAULT;
279
280 if (ImportExportThreadLocal.isExportInProcess()) {
281 secure = false;
282 }
283
284 return getAttributes(names, secure);
285 }
286
287 public Map<String, Serializable> getAttributes(
288 Collection<String> names, boolean secure) {
289
290 Map<String, Serializable> attributeValues = null;
291
292 try {
293 if (secure) {
294 attributeValues = ExpandoValueServiceUtil.getData(
295 _companyId, _className,
296 ExpandoTableConstants.DEFAULT_TABLE_NAME, names, _classPK);
297 }
298 else {
299 attributeValues = ExpandoValueLocalServiceUtil.getData(
300 _companyId, _className,
301 ExpandoTableConstants.DEFAULT_TABLE_NAME, names, _classPK);
302 }
303 }
304 catch (Exception e) {
305 if (_log.isDebugEnabled()) {
306 _log.debug(e, e);
307 }
308 }
309
310 return attributeValues;
311 }
312
313 public int getAttributeType(String name) {
314 try {
315 ExpandoColumn column =
316 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
317 _companyId, _className, name);
318
319 return column.getType();
320 }
321 catch (Exception e) {
322 _log.error(e, e);
323
324 return 0;
325 }
326 }
327
328 public String getClassName() {
329 return _className;
330 }
331
332 public long getClassPK() {
333 return _classPK;
334 }
335
336 public long getCompanyId() {
337 return _companyId;
338 }
339
340 public boolean hasAttribute(String name) {
341 ExpandoColumn column = null;
342
343 try {
344 column = ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
345 _companyId, _className, name);
346 }
347 catch (Exception e) {
348 }
349
350 if (column != null) {
351 return true;
352 }
353 else {
354 return false;
355 }
356 }
357
358 public boolean isIndexEnabled() {
359 if (_indexEnabled && (_classPK > 0)) {
360 return true;
361 }
362 else {
363 return false;
364 }
365 }
366
367 public void reindex() {
368 if (!isIndexEnabled()) {
369 return;
370 }
371
372 Indexer indexer = IndexerRegistryUtil.getIndexer(_className);
373
374 if (indexer != null) {
375 try {
376 indexer.reindex(_className, _classPK);
377 }
378 catch (Exception e) {
379 _log.error(e, e);
380 }
381 }
382 }
383
384 public void setAttribute(String name, Serializable value) {
385 boolean secure =
386 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
387
388 if (ImportExportThreadLocal.isImportInProcess()) {
389 secure = false;
390 }
391
392 setAttribute(name, value, secure);
393 }
394
395 public void setAttribute(String name, Serializable value, boolean secure) {
396 if (_classPK <= 0) {
397 throw new UnsupportedOperationException(
398 "Class primary key is less than 0");
399 }
400
401 try {
402 if (secure) {
403 ExpandoValueServiceUtil.addValue(
404 _companyId, _className,
405 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
406 value);
407 }
408 else {
409 ExpandoValueLocalServiceUtil.addValue(
410 _companyId, _className,
411 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
412 value);
413 }
414 }
415 catch (Exception e) {
416 _log.error(e, e);
417 }
418 }
419
420 public void setAttributeDefault(String name, Serializable defaultValue) {
421 try {
422 ExpandoColumn column =
423 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
424 _companyId, _className, name);
425
426 ExpandoColumnServiceUtil.updateColumn(
427 column.getColumnId(), column.getName(), column.getType(),
428 defaultValue);
429 }
430 catch (Exception e) {
431 _log.error(e, e);
432 }
433 }
434
435 public void setAttributeProperties(
436 String name, UnicodeProperties properties) {
437
438 boolean secure =
439 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
440
441 if (ImportExportThreadLocal.isImportInProcess()) {
442 secure = false;
443 }
444
445 setAttributeProperties(name, properties, secure);
446 }
447
448 public void setAttributeProperties(
449 String name, UnicodeProperties properties, boolean secure) {
450
451 try {
452 ExpandoColumn column =
453 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
454 _companyId, _className, name);
455
456 if (secure) {
457 ExpandoColumnServiceUtil.updateTypeSettings(
458 column.getColumnId(), properties.toString());
459 }
460 else {
461 ExpandoColumnLocalServiceUtil.updateTypeSettings(
462 column.getColumnId(), properties.toString());
463 }
464 }
465 catch (Exception e) {
466 _log.error(e, e);
467 }
468 }
469
470 public void setAttributes(Map<String, Serializable> attributes) {
471 boolean secure =
472 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
473
474 if (ImportExportThreadLocal.isImportInProcess()) {
475 secure = false;
476 }
477
478 setAttributes(attributes, secure);
479 }
480
481 public void setAttributes(
482 Map<String, Serializable> attributes, boolean secure) {
483
484 if (_classPK <= 0) {
485 throw new UnsupportedOperationException(
486 "Class primary key is less than 0");
487 }
488
489 if ((attributes == null) || attributes.isEmpty()) {
490 return;
491 }
492
493 try {
494 if (secure) {
495 ExpandoValueServiceUtil.addValues(
496 _companyId, _className,
497 ExpandoTableConstants.DEFAULT_TABLE_NAME,
498 _classPK, attributes);
499 }
500 else {
501 ExpandoValueLocalServiceUtil.addValues(
502 _companyId, _className,
503 ExpandoTableConstants.DEFAULT_TABLE_NAME,
504 _classPK, attributes);
505 }
506 }
507 catch (Exception e) {
508 _log.error(e, e);
509 }
510 }
511
512 public void setAttributes(ServiceContext serviceContext) {
513 boolean secure =
514 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
515
516 if (ImportExportThreadLocal.isImportInProcess()) {
517 secure = false;
518 }
519
520 setAttributes(serviceContext, secure);
521 }
522
523 public void setAttributes(ServiceContext serviceContext, boolean secure) {
524 if (serviceContext == null) {
525 return;
526 }
527
528 setAttributes(serviceContext.getExpandoBridgeAttributes(), secure);
529 }
530
531 public void setClassName(String className) {
532 _className = className;
533 }
534
535 public void setClassPK(long classPK) {
536 _classPK = classPK;
537 }
538
539 public void setCompanyId(long companyId) {
540 _companyId = companyId;
541 }
542
543 public void setIndexEnabled(boolean indexEnabled) {
544 _indexEnabled = indexEnabled;
545 }
546
547 private static Log _log = LogFactoryUtil.getLog(ExpandoBridgeImpl.class);
548
549 private String _className;
550 private long _classPK;
551 private long _companyId;
552 private boolean _indexEnabled;
553
554 }