1
22
23 package com.liferay.portlet.expando.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.security.auth.CompanyThreadLocal;
29 import com.liferay.portal.util.PortalUtil;
30 import com.liferay.portlet.expando.DuplicateTableNameException;
31 import com.liferay.portlet.expando.TableNameException;
32 import com.liferay.portlet.expando.model.ExpandoTable;
33 import com.liferay.portlet.expando.model.ExpandoTableConstants;
34 import com.liferay.portlet.expando.service.base.ExpandoTableLocalServiceBaseImpl;
35
36 import java.util.List;
37
38
45 public class ExpandoTableLocalServiceImpl
46 extends ExpandoTableLocalServiceBaseImpl {
47
48 public ExpandoTable addDefaultTable(long classNameId)
49 throws PortalException, SystemException {
50
51 return addTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
52 }
53
54 public ExpandoTable addDefaultTable(String className)
55 throws PortalException, SystemException {
56
57 return addTable(className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
58 }
59
60 public ExpandoTable addTable(long classNameId, String name)
61 throws PortalException, SystemException {
62
63 long companyId = CompanyThreadLocal.getCompanyId();
64
65 validate(companyId, 0, classNameId, name);
66
67 long tableId = counterLocalService.increment();
68
69 ExpandoTable table = expandoTablePersistence.create(tableId);
70
71 table.setCompanyId(companyId);
72 table.setClassNameId(classNameId);
73 table.setName(name);
74
75 expandoTablePersistence.update(table, false);
76
77 return table;
78 }
79
80 public ExpandoTable addTable(String className, String name)
81 throws PortalException, SystemException {
82
83 long classNameId = PortalUtil.getClassNameId(className);
84
85 return addTable(classNameId, name);
86 }
87
88 public void deleteTable(ExpandoTable table) throws SystemException {
89
90
92 expandoTablePersistence.remove(table);
93
94
96 runSQL(
97 "delete from ExpandoColumn where tableId = " + table.getTableId());
98
99 expandoColumnPersistence.clearCache();
100
101
103 runSQL("delete from ExpandoRow where tableId = " + table.getTableId());
104
105 expandoRowPersistence.clearCache();
106
107
109 runSQL(
110 "delete from ExpandoValue where tableId = " + table.getTableId());
111
112 expandoValuePersistence.clearCache();
113 }
114
115 public void deleteTable(long tableId)
116 throws PortalException, SystemException {
117
118 ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
119
120 deleteTable(table);
121 }
122
123 public void deleteTable(long classNameId, String name)
124 throws PortalException, SystemException {
125
126 long companyId = CompanyThreadLocal.getCompanyId();
127
128 ExpandoTable table = expandoTablePersistence.findByC_C_N(
129 companyId, classNameId, name);
130
131 deleteTable(table);
132 }
133
134 public void deleteTable(String className, String name)
135 throws PortalException, SystemException {
136
137 long classNameId = PortalUtil.getClassNameId(className);
138
139 deleteTable(classNameId, name);
140 }
141
142 public void deleteTables(long classNameId) throws SystemException {
143 long companyId = CompanyThreadLocal.getCompanyId();
144
145 List<ExpandoTable> tables = expandoTablePersistence.findByC_C(
146 companyId, classNameId);
147
148 for (ExpandoTable table : tables) {
149 deleteTable(table);
150 }
151 }
152
153 public void deleteTables(String className) throws SystemException {
154 long classNameId = PortalUtil.getClassNameId(className);
155
156 deleteTables(classNameId);
157 }
158
159 public ExpandoTable getDefaultTable(long classNameId)
160 throws PortalException, SystemException {
161
162 return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
163 }
164
165 public ExpandoTable getDefaultTable(String className)
166 throws PortalException, SystemException {
167
168 long classNameId = PortalUtil.getClassNameId(className);
169
170 return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
171 }
172
173 public ExpandoTable getTable(long tableId)
174 throws PortalException, SystemException {
175
176 return expandoTablePersistence.findByPrimaryKey(tableId);
177 }
178
179 public ExpandoTable getTable(long classNameId, String name)
180 throws PortalException, SystemException {
181
182 long companyId = CompanyThreadLocal.getCompanyId();
183
184 return expandoTablePersistence.findByC_C_N(
185 companyId, classNameId, name);
186 }
187
188 public ExpandoTable getTable(String className, String name)
189 throws PortalException, SystemException {
190
191 long classNameId = PortalUtil.getClassNameId(className);
192
193 return getTable(classNameId, name);
194 }
195
196 public List<ExpandoTable> getTables(long classNameId)
197 throws SystemException {
198
199 long companyId = CompanyThreadLocal.getCompanyId();
200
201 return expandoTablePersistence.findByC_C(companyId, classNameId);
202 }
203
204 public List<ExpandoTable> getTables(String className)
205 throws SystemException {
206
207 long classNameId = PortalUtil.getClassNameId(className);
208
209 return getTables(classNameId);
210 }
211
212 public ExpandoTable updateTable(long tableId, String name)
213 throws PortalException, SystemException {
214
215 ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
216
217 if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
218 throw new TableNameException(
219 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
220 }
221
222 validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
223
224 table.setName(name);
225
226 return expandoTablePersistence.update(table, false);
227 }
228
229 protected void validate(
230 long companyId, long tableId, long classNameId, String name)
231 throws PortalException, SystemException {
232
233 if (Validator.isNull(name)) {
234 throw new TableNameException();
235 }
236
237 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
238 companyId, classNameId, name);
239
240 if ((table != null) && (table.getTableId() != tableId)) {
241 throw new DuplicateTableNameException();
242 }
243 }
244
245 }