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