1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.expando.model.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.search.Indexer;
29  import com.liferay.portal.kernel.search.IndexerRegistryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.UnicodeProperties;
32  import com.liferay.portal.service.ServiceContext;
33  import com.liferay.portlet.expando.NoSuchTableException;
34  import com.liferay.portlet.expando.model.ExpandoBridge;
35  import com.liferay.portlet.expando.model.ExpandoColumn;
36  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
37  import com.liferay.portlet.expando.model.ExpandoTable;
38  import com.liferay.portlet.expando.model.ExpandoTableConstants;
39  import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
40  import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
41  import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
42  import com.liferay.portlet.expando.service.ExpandoValueServiceUtil;
43  import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
44  
45  import java.io.Serializable;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.Enumeration;
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  /**
55   * <a href="ExpandoBridgeImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Raymond Augé
58   *
59   */
60  public class ExpandoBridgeImpl implements ExpandoBridge {
61  
62      public ExpandoBridgeImpl(String className) {
63          this(className, 0);
64      }
65  
66      public ExpandoBridgeImpl(String className, long classPK) {
67          _className = className;
68          _classPK = classPK;
69  
70          if (IndexerRegistryUtil.getIndexer(className) == null) {
71              setIndexEnabled(true);
72          }
73      }
74  
75      public void addAttribute(String name) throws PortalException {
76          addAttribute(name, ExpandoColumnConstants.STRING, null);
77      }
78  
79      public void addAttribute(String name, int type) throws PortalException {
80          addAttribute(name, type, null);
81      }
82  
83      public void addAttribute(String name, int type, Serializable defaultValue)
84          throws PortalException {
85  
86          try {
87              ExpandoTable table = null;
88  
89              try {
90                  table = ExpandoTableLocalServiceUtil.getDefaultTable(
91                      _className);
92              }
93              catch (NoSuchTableException nste) {
94                  table = ExpandoTableLocalServiceUtil.addDefaultTable(
95                      _className);
96              }
97  
98              ExpandoColumnServiceUtil.addColumn(
99                  table.getTableId(), name, type, defaultValue);
100         }
101         catch (Exception e) {
102             if (e instanceof PortalException) {
103                 throw (PortalException)e;
104             }
105             else {
106                 _log.error(e, e);
107             }
108         }
109     }
110 
111     public Serializable getAttribute(String name) {
112         Serializable data = null;
113 
114         try {
115             data = ExpandoValueServiceUtil.getData(
116                 _className, ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
117                 _classPK);
118         }
119         catch (Exception e) {
120             if (_log.isDebugEnabled()) {
121                 _log.debug(e, e);
122             }
123         }
124 
125         return data;
126     }
127 
128     public Serializable getAttributeDefault(String name) {
129         try {
130             ExpandoColumn column =
131                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
132                     _className, name);
133 
134             return column.getDefaultValue();
135         }
136         catch (Exception e) {
137             _log.error(e, e);
138 
139             return null;
140         }
141     }
142 
143     public Enumeration<String> getAttributeNames() {
144         List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
145 
146         try {
147             columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
148                 _className);
149         }
150         catch (Exception e) {
151             if (_log.isDebugEnabled()) {
152                 _log.debug(e, e);
153             }
154         }
155 
156         List<String> columnNames = new ArrayList<String>();
157 
158         for (ExpandoColumn column : columns) {
159             columnNames.add(column.getName());
160         }
161 
162         return Collections.enumeration(columnNames);
163     }
164 
165     public UnicodeProperties getAttributeProperties(String name) {
166         try {
167             ExpandoColumn column =
168                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
169                     _className, name);
170 
171             return column.getTypeSettingsProperties();
172         }
173         catch (Exception e) {
174             if (_log.isDebugEnabled()) {
175                 _log.debug("Properties for " + name, e);
176             }
177 
178             return new UnicodeProperties(true);
179         }
180     }
181 
182     public Map<String, Serializable> getAttributes() {
183         Map<String, Serializable> attributes =
184             new HashMap<String, Serializable>();
185 
186         List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
187 
188         try {
189             columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
190                 _className);
191         }
192         catch (Exception e) {
193             if (_log.isDebugEnabled()) {
194                 _log.debug(e, e);
195             }
196         }
197 
198         for (ExpandoColumn column : columns) {
199             attributes.put(column.getName(), getAttribute(column.getName()));
200         }
201 
202         return attributes;
203     }
204 
205     public int getAttributeType(String name) {
206         try {
207             ExpandoColumn column =
208                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
209                     _className, name);
210 
211             return column.getType();
212         }
213         catch (Exception e) {
214             _log.error(e, e);
215 
216             return 0;
217         }
218     }
219 
220     public String getClassName() {
221         return _className;
222     }
223 
224     public long getClassPK() {
225         return _classPK;
226     }
227 
228     public boolean hasAttribute(String name) {
229         ExpandoColumn column = null;
230 
231         try {
232             column = ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
233                 _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 setAttribute(String name, Serializable value) {
256         if (_classPK <= 0) {
257             throw new UnsupportedOperationException();
258         }
259 
260         try {
261             ExpandoValueServiceUtil.addValue(
262                 _className, ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
263                 _classPK, value);
264 
265             checkIndex(name);
266         }
267         catch (Exception e) {
268             _log.error(e, e);
269         }
270     }
271 
272     public void setAttributeDefault(String name, Serializable defaultValue) {
273         try {
274             ExpandoColumn column =
275                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
276                     _className, name);
277 
278             ExpandoColumnServiceUtil.updateColumn(
279                 column.getColumnId(), column.getName(), column.getType(),
280                 defaultValue);
281         }
282         catch (Exception e) {
283             _log.error(e, e);
284         }
285     }
286 
287     public void setAttributeProperties(
288         String name, UnicodeProperties properties) {
289 
290         try {
291             ExpandoColumn column =
292                 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
293                     _className, name);
294 
295             ExpandoColumnServiceUtil.updateTypeSettings(
296                 column.getColumnId(), properties.toString());
297         }
298         catch (Exception e) {
299             _log.error(e, e);
300         }
301     }
302 
303     public void setAttributes(Map<String, Serializable> attributes) {
304         if (attributes == null) {
305             return;
306         }
307 
308         boolean indexEnabled = isIndexEnabled();
309 
310         setIndexEnabled(false);
311 
312         for (Map.Entry<String, Serializable> entry : attributes.entrySet()) {
313             setAttribute(entry.getKey(), entry.getValue());
314         }
315 
316         setIndexEnabled(indexEnabled);
317 
318         reIndex();
319     }
320 
321     public void setAttributes(ServiceContext serviceContext) {
322         if (serviceContext == null) {
323             return;
324         }
325 
326         setAttributes(serviceContext.getExpandoBridgeAttributes());
327     }
328 
329     public void setClassName(String className) {
330         _className = className;
331     }
332 
333     public void setClassPK(long classPK) {
334         _classPK = classPK;
335     }
336 
337     public void setIndexEnabled(boolean indexEnabled) {
338         _indexEnabled = indexEnabled;
339     }
340 
341     protected void checkIndex(String name) {
342         if (!isIndexEnabled()) {
343             return;
344         }
345 
346         UnicodeProperties properties = getAttributeProperties(name);
347 
348         if ((getAttributeType(name) == ExpandoColumnConstants.STRING) &&
349             GetterUtil.getBoolean(
350                 properties.getProperty(ExpandoBridgeIndexer.INDEXABLE))) {
351 
352             reIndex();
353         }
354     }
355 
356     protected void reIndex() {
357         if (!isIndexEnabled()) {
358             return;
359         }
360 
361         Indexer indexer = IndexerRegistryUtil.getIndexer(_className);
362 
363         if (indexer != null) {
364             try {
365                 indexer.reIndex(_className, _classPK);
366             }
367             catch (Exception e) {
368                 _log.error(e, e);
369             }
370         }
371     }
372 
373     private static Log _log = LogFactoryUtil.getLog(ExpandoBridgeImpl.class);
374 
375     private String _className;
376     private long _classPK;
377     private boolean _indexEnabled;
378 
379 }