1
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
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
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 _className);
88 }
89 catch (NoSuchTableException nste) {
90 table = ExpandoTableLocalServiceUtil.addDefaultTable(
91 _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 _className, ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
113 _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 _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 _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 _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 _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 _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 boolean hasAttribute(String name) {
225 if (getAttribute(name) != null) {
226 return true;
227 }
228 else {
229 return false;
230 }
231 }
232
233 public boolean isIndexEnabled() {
234 if (_indexEnabled && (_classPK > 0)) {
235 return true;
236 }
237 else {
238 return false;
239 }
240 }
241
242 public void setAttribute(String name, Serializable value) {
243 if (_classPK <= 0) {
244 throw new UnsupportedOperationException();
245 }
246
247 try {
248 ExpandoValueServiceUtil.addValue(
249 _className, ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
250 _classPK, value);
251
252 checkIndex(name);
253 }
254 catch (Exception e) {
255 _log.error(e, e);
256 }
257 }
258
259 public void setAttributeDefault(String name, Serializable defaultValue) {
260 try {
261 ExpandoColumn column =
262 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
263 _className, name);
264
265 ExpandoColumnServiceUtil.updateColumn(
266 column.getColumnId(), column.getName(), column.getType(),
267 defaultValue);
268 }
269 catch (Exception e) {
270 _log.error(e, e);
271 }
272 }
273
274 public void setAttributeProperties(
275 String name, UnicodeProperties properties) {
276
277 try {
278 ExpandoColumn column =
279 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
280 _className, name);
281
282 ExpandoColumnServiceUtil.updateTypeSettings(
283 column.getColumnId(), properties.toString());
284 }
285 catch (Exception e) {
286 _log.error(e, e);
287 }
288 }
289
290 public void setAttributes(Map<String, Serializable> attributes) {
291 if (attributes == null) {
292 return;
293 }
294
295 boolean indexEnabled = isIndexEnabled();
296
297 setIndexEnabled(false);
298
299 for (Map.Entry<String, Serializable> entry : attributes.entrySet()) {
300 setAttribute(entry.getKey(), entry.getValue());
301 }
302
303 setIndexEnabled(indexEnabled);
304
305 reIndex();
306 }
307
308 public void setAttributes(ServiceContext serviceContext) {
309 if (serviceContext == null) {
310 return;
311 }
312
313 setAttributes(serviceContext.getExpandoBridgeAttributes());
314 }
315
316 public void setClassName(String className) {
317 _className = className;
318 }
319
320 public void setClassPK(long classPK) {
321 _classPK = classPK;
322 }
323
324 public void setIndexEnabled(boolean indexEnabled) {
325 _indexEnabled = indexEnabled;
326 }
327
328 protected void checkIndex(String name) {
329 if (!isIndexEnabled()) {
330 return;
331 }
332
333 UnicodeProperties properties = getAttributeProperties(name);
334
335 if ((getAttributeType(name) == ExpandoColumnConstants.STRING) &&
336 GetterUtil.getBoolean(
337 properties.getProperty(ExpandoBridgeIndexer.INDEXABLE))) {
338
339 reIndex();
340 }
341 }
342
343 protected void reIndex() {
344 if (!isIndexEnabled()) {
345 return;
346 }
347
348 Indexer indexer = IndexerRegistryUtil.getIndexer(_className);
349
350 if (indexer != null) {
351 try {
352 indexer.reIndex(_className, _classPK);
353 }
354 catch (Exception e) {
355 _log.error(e, e);
356 }
357 }
358 }
359
360 private static Log _log = LogFactoryUtil.getLog(ExpandoBridgeImpl.class);
361
362 private String _className;
363 private long _classPK;
364 private boolean _indexEnabled = true;
365
366 }