1
14
15 package com.liferay.portlet.expando.model.impl;
16
17 import com.liferay.portal.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.service.ServiceContext;
24 import com.liferay.portlet.expando.NoSuchTableException;
25 import com.liferay.portlet.expando.model.ExpandoBridge;
26 import com.liferay.portlet.expando.model.ExpandoColumn;
27 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
28 import com.liferay.portlet.expando.model.ExpandoTable;
29 import com.liferay.portlet.expando.model.ExpandoTableConstants;
30 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
31 import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
32 import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
33 import com.liferay.portlet.expando.service.ExpandoValueServiceUtil;
34
35 import java.io.Serializable;
36
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44
49 public class ExpandoBridgeImpl implements ExpandoBridge {
50
51 public ExpandoBridgeImpl(String className) {
52 this(className, 0);
53 }
54
55 public ExpandoBridgeImpl(String className, long classPK) {
56 _className = className;
57 _classPK = classPK;
58
59 if (IndexerRegistryUtil.getIndexer(className) == null) {
60 setIndexEnabled(true);
61 }
62 }
63
64 public void addAttribute(String name) throws PortalException {
65 addAttribute(name, ExpandoColumnConstants.STRING, null);
66 }
67
68 public void addAttribute(String name, int type) throws PortalException {
69 addAttribute(name, type, null);
70 }
71
72 public void addAttribute(String name, int type, Serializable defaultValue)
73 throws PortalException {
74
75 try {
76 ExpandoTable table = null;
77
78 try {
79 table = ExpandoTableLocalServiceUtil.getDefaultTable(
80 _className);
81 }
82 catch (NoSuchTableException nste) {
83 table = ExpandoTableLocalServiceUtil.addDefaultTable(
84 _className);
85 }
86
87 ExpandoColumnServiceUtil.addColumn(
88 table.getTableId(), name, type, defaultValue);
89 }
90 catch (Exception e) {
91 if (e instanceof PortalException) {
92 throw (PortalException)e;
93 }
94 else {
95 _log.error(e, e);
96 }
97 }
98 }
99
100 public Serializable getAttribute(String name) {
101 Serializable data = null;
102
103 try {
104 data = ExpandoValueServiceUtil.getData(
105 _className, ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
106 _classPK);
107 }
108 catch (Exception e) {
109 if (_log.isDebugEnabled()) {
110 _log.debug(e, e);
111 }
112 }
113
114 return data;
115 }
116
117 public Serializable getAttributeDefault(String name) {
118 try {
119 ExpandoColumn column =
120 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
121 _className, name);
122
123 return column.getDefaultValue();
124 }
125 catch (Exception e) {
126 _log.error(e, e);
127
128 return null;
129 }
130 }
131
132 public Enumeration<String> getAttributeNames() {
133 List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
134
135 try {
136 columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
137 _className);
138 }
139 catch (Exception e) {
140 if (_log.isDebugEnabled()) {
141 _log.debug(e, e);
142 }
143 }
144
145 List<String> columnNames = new ArrayList<String>();
146
147 for (ExpandoColumn column : columns) {
148 columnNames.add(column.getName());
149 }
150
151 return Collections.enumeration(columnNames);
152 }
153
154 public UnicodeProperties getAttributeProperties(String name) {
155 try {
156 ExpandoColumn column =
157 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
158 _className, name);
159
160 return column.getTypeSettingsProperties();
161 }
162 catch (Exception e) {
163 if (_log.isDebugEnabled()) {
164 _log.debug("Properties for " + name, e);
165 }
166
167 return new UnicodeProperties(true);
168 }
169 }
170
171 public Map<String, Serializable> getAttributes() {
172 Map<String, Serializable> attributes =
173 new HashMap<String, Serializable>();
174
175 List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
176
177 try {
178 columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
179 _className);
180 }
181 catch (Exception e) {
182 if (_log.isDebugEnabled()) {
183 _log.debug(e, e);
184 }
185 }
186
187 for (ExpandoColumn column : columns) {
188 attributes.put(column.getName(), getAttribute(column.getName()));
189 }
190
191 return attributes;
192 }
193
194 public int getAttributeType(String name) {
195 try {
196 ExpandoColumn column =
197 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
198 _className, name);
199
200 return column.getType();
201 }
202 catch (Exception e) {
203 _log.error(e, e);
204
205 return 0;
206 }
207 }
208
209 public String getClassName() {
210 return _className;
211 }
212
213 public long getClassPK() {
214 return _classPK;
215 }
216
217 public boolean hasAttribute(String name) {
218 ExpandoColumn column = null;
219
220 try {
221 column = ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
222 _className, name);
223 }
224 catch (Exception e) {
225 }
226
227 if (column != null) {
228 return true;
229 }
230 else {
231 return false;
232 }
233 }
234
235 public boolean isIndexEnabled() {
236 if (_indexEnabled && (_classPK > 0)) {
237 return true;
238 }
239 else {
240 return false;
241 }
242 }
243
244 public void reIndex() {
245 if (!isIndexEnabled()) {
246 return;
247 }
248
249 Indexer indexer = IndexerRegistryUtil.getIndexer(_className);
250
251 if (indexer != null) {
252 try {
253 indexer.reIndex(_className, _classPK);
254 }
255 catch (Exception e) {
256 _log.error(e, e);
257 }
258 }
259 }
260
261 public void setAttribute(String name, Serializable value) {
262 if (_classPK <= 0) {
263 throw new UnsupportedOperationException();
264 }
265
266 try {
267 ExpandoValueServiceUtil.addValue(
268 _className, ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
269 _classPK, value);
270 }
271 catch (Exception e) {
272 _log.error(e, e);
273 }
274 }
275
276 public void setAttributeDefault(String name, Serializable defaultValue) {
277 try {
278 ExpandoColumn column =
279 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
280 _className, name);
281
282 ExpandoColumnServiceUtil.updateColumn(
283 column.getColumnId(), column.getName(), column.getType(),
284 defaultValue);
285 }
286 catch (Exception e) {
287 _log.error(e, e);
288 }
289 }
290
291 public void setAttributeProperties(
292 String name, UnicodeProperties properties) {
293
294 try {
295 ExpandoColumn column =
296 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
297 _className, name);
298
299 ExpandoColumnServiceUtil.updateTypeSettings(
300 column.getColumnId(), properties.toString());
301 }
302 catch (Exception e) {
303 _log.error(e, e);
304 }
305 }
306
307 public void setAttributes(Map<String, Serializable> attributes) {
308 if (attributes == null) {
309 return;
310 }
311
312 for (Map.Entry<String, Serializable> entry : attributes.entrySet()) {
313 setAttribute(entry.getKey(), entry.getValue());
314 }
315 }
316
317 public void setAttributes(ServiceContext serviceContext) {
318 if (serviceContext == null) {
319 return;
320 }
321
322 setAttributes(serviceContext.getExpandoBridgeAttributes());
323 }
324
325 public void setClassName(String className) {
326 _className = className;
327 }
328
329 public void setClassPK(long classPK) {
330 _classPK = classPK;
331 }
332
333 public void setIndexEnabled(boolean indexEnabled) {
334 _indexEnabled = indexEnabled;
335 }
336
337 private static Log _log = LogFactoryUtil.getLog(ExpandoBridgeImpl.class);
338
339 private String _className;
340 private long _classPK;
341 private boolean _indexEnabled;
342
343 }