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