001
014
015 package com.liferay.portlet.expando.model.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.search.Indexer;
021 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
022 import com.liferay.portal.kernel.util.UnicodeProperties;
023 import com.liferay.portal.security.auth.CompanyThreadLocal;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portlet.expando.NoSuchTableException;
026 import com.liferay.portlet.expando.model.ExpandoBridge;
027 import com.liferay.portlet.expando.model.ExpandoColumn;
028 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
029 import com.liferay.portlet.expando.model.ExpandoTable;
030 import com.liferay.portlet.expando.model.ExpandoTableConstants;
031 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
032 import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
033 import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
034 import com.liferay.portlet.expando.service.ExpandoValueServiceUtil;
035
036 import java.io.Serializable;
037
038 import java.util.ArrayList;
039 import java.util.Collections;
040 import java.util.Enumeration;
041 import java.util.HashMap;
042 import java.util.List;
043 import java.util.Map;
044
045
048 public class ExpandoBridgeImpl implements ExpandoBridge {
049
050 public ExpandoBridgeImpl(long companyId, String className) {
051 this(companyId, className, 0);
052 }
053
054 public ExpandoBridgeImpl(long companyId, String className, long classPK) {
055 _companyId = companyId;
056
057 if (_companyId == 0) {
058 _companyId = CompanyThreadLocal.getCompanyId();
059 }
060
061 _className = className;
062 _classPK = classPK;
063
064 if (IndexerRegistryUtil.getIndexer(className) == null) {
065 setIndexEnabled(true);
066 }
067 }
068
069 public void addAttribute(String name) throws PortalException {
070 addAttribute(name, ExpandoColumnConstants.STRING, null);
071 }
072
073 public void addAttribute(String name, int type) throws PortalException {
074 addAttribute(name, type, null);
075 }
076
077 public void addAttribute(String name, int type, Serializable defaultValue)
078 throws PortalException {
079
080 try {
081 ExpandoTable table = null;
082
083 try {
084 table = ExpandoTableLocalServiceUtil.getDefaultTable(
085 _companyId, _className);
086 }
087 catch (NoSuchTableException nste) {
088 table = ExpandoTableLocalServiceUtil.addDefaultTable(
089 _companyId, _className);
090 }
091
092 ExpandoColumnServiceUtil.addColumn(
093 table.getTableId(), name, type, defaultValue);
094 }
095 catch (Exception e) {
096 if (e instanceof PortalException) {
097 throw (PortalException)e;
098 }
099 else {
100 _log.error(e, e);
101 }
102 }
103 }
104
105 public Serializable getAttribute(String name) {
106 Serializable data = null;
107
108 try {
109 data = ExpandoValueServiceUtil.getData(
110 _companyId, _className,
111 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK);
112 }
113 catch (Exception e) {
114 if (_log.isDebugEnabled()) {
115 _log.debug(e, e);
116 }
117 }
118
119 return data;
120 }
121
122 public Serializable getAttributeDefault(String name) {
123 try {
124 ExpandoColumn column =
125 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
126 _companyId, _className, name);
127
128 return column.getDefaultValue();
129 }
130 catch (Exception e) {
131 _log.error(e, e);
132
133 return null;
134 }
135 }
136
137 public Enumeration<String> getAttributeNames() {
138 List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
139
140 try {
141 columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
142 _companyId, _className);
143 }
144 catch (Exception e) {
145 if (_log.isDebugEnabled()) {
146 _log.debug(e, e);
147 }
148 }
149
150 List<String> columnNames = new ArrayList<String>();
151
152 for (ExpandoColumn column : columns) {
153 columnNames.add(column.getName());
154 }
155
156 return Collections.enumeration(columnNames);
157 }
158
159 public UnicodeProperties getAttributeProperties(String name) {
160 try {
161 ExpandoColumn column =
162 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
163 _companyId, _className, name);
164
165 return column.getTypeSettingsProperties();
166 }
167 catch (Exception e) {
168 if (_log.isDebugEnabled()) {
169 _log.debug("Properties for " + name, e);
170 }
171
172 return new UnicodeProperties(true);
173 }
174 }
175
176 public Map<String, Serializable> getAttributes() {
177 Map<String, Serializable> attributes =
178 new HashMap<String, Serializable>();
179
180 List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
181
182 try {
183 columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
184 _companyId, _className);
185 }
186 catch (Exception e) {
187 if (_log.isDebugEnabled()) {
188 _log.debug(e, e);
189 }
190 }
191
192 for (ExpandoColumn column : columns) {
193 attributes.put(column.getName(), getAttribute(column.getName()));
194 }
195
196 return attributes;
197 }
198
199 public int getAttributeType(String name) {
200 try {
201 ExpandoColumn column =
202 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
203 _companyId, _className, name);
204
205 return column.getType();
206 }
207 catch (Exception e) {
208 _log.error(e, e);
209
210 return 0;
211 }
212 }
213
214 public String getClassName() {
215 return _className;
216 }
217
218 public long getClassPK() {
219 return _classPK;
220 }
221
222 public long getCompanyId() {
223 return _companyId;
224 }
225
226 public boolean hasAttribute(String name) {
227 ExpandoColumn column = null;
228
229 try {
230 column = ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
231 _companyId, _className, name);
232 }
233 catch (Exception e) {
234 }
235
236 if (column != null) {
237 return true;
238 }
239 else {
240 return false;
241 }
242 }
243
244 public boolean isIndexEnabled() {
245 if (_indexEnabled && (_classPK > 0)) {
246 return true;
247 }
248 else {
249 return false;
250 }
251 }
252
253 public void reindex() {
254 if (!isIndexEnabled()) {
255 return;
256 }
257
258 Indexer indexer = IndexerRegistryUtil.getIndexer(_className);
259
260 if (indexer != null) {
261 try {
262 indexer.reindex(_className, _classPK);
263 }
264 catch (Exception e) {
265 _log.error(e, e);
266 }
267 }
268 }
269
270 public void setAttribute(String name, Serializable value) {
271 if (_classPK <= 0) {
272 throw new UnsupportedOperationException();
273 }
274
275 try {
276 ExpandoValueServiceUtil.addValue(
277 _companyId, _className,
278 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
279 value);
280 }
281 catch (Exception e) {
282 _log.error(e, e);
283 }
284 }
285
286 public void setAttributeDefault(String name, Serializable defaultValue) {
287 try {
288 ExpandoColumn column =
289 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
290 _companyId, _className, name);
291
292 ExpandoColumnServiceUtil.updateColumn(
293 column.getColumnId(), column.getName(), column.getType(),
294 defaultValue);
295 }
296 catch (Exception e) {
297 _log.error(e, e);
298 }
299 }
300
301 public void setAttributeProperties(
302 String name, UnicodeProperties properties) {
303
304 try {
305 ExpandoColumn column =
306 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
307 _companyId, _className, name);
308
309 ExpandoColumnServiceUtil.updateTypeSettings(
310 column.getColumnId(), properties.toString());
311 }
312 catch (Exception e) {
313 _log.error(e, e);
314 }
315 }
316
317 public void setAttributes(Map<String, Serializable> attributes) {
318 if (attributes == null) {
319 return;
320 }
321
322 for (Map.Entry<String, Serializable> entry : attributes.entrySet()) {
323 setAttribute(entry.getKey(), entry.getValue());
324 }
325 }
326
327 public void setAttributes(ServiceContext serviceContext) {
328 if (serviceContext == null) {
329 return;
330 }
331
332 setAttributes(serviceContext.getExpandoBridgeAttributes());
333 }
334
335 public void setClassName(String className) {
336 _className = className;
337 }
338
339 public void setClassPK(long classPK) {
340 _classPK = classPK;
341 }
342
343 public void setCompanyId(long companyId) {
344 _companyId = companyId;
345 }
346
347 public void setIndexEnabled(boolean indexEnabled) {
348 _indexEnabled = indexEnabled;
349 }
350
351 private static Log _log = LogFactoryUtil.getLog(ExpandoBridgeImpl.class);
352
353 private String _className;
354 private long _classPK;
355 private long _companyId;
356 private boolean _indexEnabled;
357
358 }