1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.expando.model.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.search.Indexer;
21  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
22  import com.liferay.portal.kernel.util.UnicodeProperties;
23  import com.liferay.portal.security.auth.CompanyThreadLocal;
24  import com.liferay.portal.service.ServiceContext;
25  import com.liferay.portlet.expando.NoSuchTableException;
26  import com.liferay.portlet.expando.model.ExpandoBridge;
27  import com.liferay.portlet.expando.model.ExpandoColumn;
28  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
29  import com.liferay.portlet.expando.model.ExpandoTable;
30  import com.liferay.portlet.expando.model.ExpandoTableConstants;
31  import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
32  import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
33  import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
34  import com.liferay.portlet.expando.service.ExpandoValueServiceUtil;
35  
36  import java.io.Serializable;
37  
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.Enumeration;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * <a href="ExpandoBridgeImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Raymond Augé
49   */
50  public class ExpandoBridgeImpl implements ExpandoBridge {
51  
52      public ExpandoBridgeImpl(long companyId, String className) {
53          this(companyId, className, 0);
54      }
55  
56      public ExpandoBridgeImpl(long companyId, String className, long classPK) {
57          _companyId = companyId;
58  
59          if (_companyId == 0) {
60              _companyId = CompanyThreadLocal.getCompanyId();
61          }
62  
63          _className = className;
64          _classPK = classPK;
65  
66          if (IndexerRegistryUtil.getIndexer(className) == null) {
67              setIndexEnabled(true);
68          }
69      }
70  
71      public void addAttribute(String name) throws PortalException {
72          addAttribute(name, ExpandoColumnConstants.STRING, null);
73      }
74  
75      public void addAttribute(String name, int type) throws PortalException {
76          addAttribute(name, type, null);
77      }
78  
79      public void addAttribute(String name, int type, Serializable defaultValue)
80          throws PortalException {
81  
82          try {
83              ExpandoTable table = null;
84  
85              try {
86                  table = ExpandoTableLocalServiceUtil.getDefaultTable(
87                      _companyId, _className);
88              }
89              catch (NoSuchTableException nste) {
90                  table = ExpandoTableLocalServiceUtil.addDefaultTable(
91                      _companyId, _className);
92              }
93  
94              ExpandoColumnServiceUtil.addColumn(
95                  table.getTableId(), name, type, defaultValue);
96          }
97          catch (Exception e) {
98              if (e instanceof PortalException) {
99                  throw (PortalException)e;
100             }
101             else {
102                 _log.error(e, e);
103             }
104         }
105     }
106 
107     public Serializable getAttribute(String name) {
108         Serializable data = null;
109 
110         try {
111             data = ExpandoValueServiceUtil.getData(
112                 _companyId, _className,
113                 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK);
114         }
115         catch (Exception e) {
116             if (_log.isDebugEnabled()) {
117                 _log.debug(e, e);
118             }
119         }
120 
121         return data;
122     }
123 
124     public Serializable getAttributeDefault(String name) {
125         try {
126             ExpandoColumn column =
127                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
128                     _companyId, _className, name);
129 
130             return column.getDefaultValue();
131         }
132         catch (Exception e) {
133             _log.error(e, e);
134 
135             return null;
136         }
137     }
138 
139     public Enumeration<String> getAttributeNames() {
140         List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
141 
142         try {
143             columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
144                 _companyId, _className);
145         }
146         catch (Exception e) {
147             if (_log.isDebugEnabled()) {
148                 _log.debug(e, e);
149             }
150         }
151 
152         List<String> columnNames = new ArrayList<String>();
153 
154         for (ExpandoColumn column : columns) {
155             columnNames.add(column.getName());
156         }
157 
158         return Collections.enumeration(columnNames);
159     }
160 
161     public UnicodeProperties getAttributeProperties(String name) {
162         try {
163             ExpandoColumn column =
164                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
165                     _companyId, _className, name);
166 
167             return column.getTypeSettingsProperties();
168         }
169         catch (Exception e) {
170             if (_log.isDebugEnabled()) {
171                 _log.debug("Properties for " + name, e);
172             }
173 
174             return new UnicodeProperties(true);
175         }
176     }
177 
178     public Map<String, Serializable> getAttributes() {
179         Map<String, Serializable> attributes =
180             new HashMap<String, Serializable>();
181 
182         List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
183 
184         try {
185             columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
186                 _companyId, _className);
187         }
188         catch (Exception e) {
189             if (_log.isDebugEnabled()) {
190                 _log.debug(e, e);
191             }
192         }
193 
194         for (ExpandoColumn column : columns) {
195             attributes.put(column.getName(), getAttribute(column.getName()));
196         }
197 
198         return attributes;
199     }
200 
201     public int getAttributeType(String name) {
202         try {
203             ExpandoColumn column =
204                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
205                     _companyId, _className, name);
206 
207             return column.getType();
208         }
209         catch (Exception e) {
210             _log.error(e, e);
211 
212             return 0;
213         }
214     }
215 
216     public String getClassName() {
217         return _className;
218     }
219 
220     public long getClassPK() {
221         return _classPK;
222     }
223 
224     public long getCompanyId() {
225         return _companyId;
226     }
227 
228     public boolean hasAttribute(String name) {
229         ExpandoColumn column = null;
230 
231         try {
232             column = ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
233                 _companyId, _className, name);
234         }
235         catch (Exception e) {
236         }
237 
238         if (column != null) {
239             return true;
240         }
241         else {
242             return false;
243         }
244     }
245 
246     public boolean isIndexEnabled() {
247         if (_indexEnabled && (_classPK > 0)) {
248             return true;
249         }
250         else {
251             return false;
252         }
253     }
254 
255     public void reindex() {
256         if (!isIndexEnabled()) {
257             return;
258         }
259 
260         Indexer indexer = IndexerRegistryUtil.getIndexer(_className);
261 
262         if (indexer != null) {
263             try {
264                 indexer.reindex(_className, _classPK);
265             }
266             catch (Exception e) {
267                 _log.error(e, e);
268             }
269         }
270     }
271 
272     public void setAttribute(String name, Serializable value) {
273         if (_classPK <= 0) {
274             throw new UnsupportedOperationException();
275         }
276 
277         try {
278             ExpandoValueServiceUtil.addValue(
279                 _companyId, _className,
280                 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
281                 value);
282         }
283         catch (Exception e) {
284             _log.error(e, e);
285         }
286     }
287 
288     public void setAttributeDefault(String name, Serializable defaultValue) {
289         try {
290             ExpandoColumn column =
291                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
292                     _companyId, _className, name);
293 
294             ExpandoColumnServiceUtil.updateColumn(
295                 column.getColumnId(), column.getName(), column.getType(),
296                 defaultValue);
297         }
298         catch (Exception e) {
299             _log.error(e, e);
300         }
301     }
302 
303     public void setAttributeProperties(
304         String name, UnicodeProperties properties) {
305 
306         try {
307             ExpandoColumn column =
308                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
309                     _companyId, _className, name);
310 
311             ExpandoColumnServiceUtil.updateTypeSettings(
312                 column.getColumnId(), properties.toString());
313         }
314         catch (Exception e) {
315             _log.error(e, e);
316         }
317     }
318 
319     public void setAttributes(Map<String, Serializable> attributes) {
320         if (attributes == null) {
321             return;
322         }
323 
324         for (Map.Entry<String, Serializable> entry : attributes.entrySet()) {
325             setAttribute(entry.getKey(), entry.getValue());
326         }
327     }
328 
329     public void setAttributes(ServiceContext serviceContext) {
330         if (serviceContext == null) {
331             return;
332         }
333 
334         setAttributes(serviceContext.getExpandoBridgeAttributes());
335     }
336 
337     public void setClassName(String className) {
338         _className = className;
339     }
340 
341     public void setClassPK(long classPK) {
342         _classPK = classPK;
343     }
344 
345     public void setCompanyId(long companyId) {
346         _companyId = companyId;
347     }
348 
349     public void setIndexEnabled(boolean indexEnabled) {
350         _indexEnabled = indexEnabled;
351     }
352 
353     private static Log _log = LogFactoryUtil.getLog(ExpandoBridgeImpl.class);
354 
355     private String _className;
356     private long _classPK;
357     private long _companyId;
358     private boolean _indexEnabled;
359 
360 }