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
46 public class ExpandoTableLocalServiceImpl
47 extends ExpandoTableLocalServiceBaseImpl {
48
49 public ExpandoTable addDefaultTable(long classNameId)
50 throws PortalException, SystemException {
51
52 return addTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
53 }
54
55 public ExpandoTable addDefaultTable(String className)
56 throws PortalException, SystemException {
57
58 return addTable(className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
59 }
60
61 public ExpandoTable addTable(long classNameId, String name)
62 throws PortalException, SystemException {
63
64 long companyId = CompanyThreadLocal.getCompanyId();
65
66 validate(companyId, 0, classNameId, name);
67
68 long tableId = counterLocalService.increment();
69
70 ExpandoTable table = expandoTablePersistence.create(tableId);
71
72 table.setCompanyId(companyId);
73 table.setClassNameId(classNameId);
74 table.setName(name);
75
76 expandoTablePersistence.update(table, false);
77
78 return table;
79 }
80
81 public ExpandoTable addTable(String className, String name)
82 throws PortalException, SystemException {
83
84 long classNameId = PortalUtil.getClassNameId(className);
85
86 return addTable(classNameId, name);
87 }
88
89 public void deleteTable(long tableId)
90 throws PortalException, SystemException {
91
92
94 runSQL("DELETE FROM ExpandoValue WHERE tableId = " + tableId);
95
96
98 runSQL("DELETE FROM ExpandoColumn WHERE tableId = " + tableId);
99
100
102 runSQL("DELETE FROM ExpandoRow WHERE tableId = " + tableId);
103
104 expandoColumnPersistence.clearCache();
105 expandoRowPersistence.clearCache();
106 expandoValuePersistence.clearCache();
107
108
115
116
118 expandoTablePersistence.remove(tableId);
119 }
120
121 public void deleteTable(long classNameId, String name)
122 throws PortalException, SystemException {
123
124 long companyId = CompanyThreadLocal.getCompanyId();
125
126 ExpandoTable table = expandoTablePersistence.findByC_C_N(
127 companyId, classNameId, name);
128
129 deleteTable(table.getTableId());
130 }
131
132 public void deleteTable(String className, String name)
133 throws PortalException, SystemException {
134
135 long classNameId = PortalUtil.getClassNameId(className);
136
137 deleteTable(classNameId, name);
138 }
139
140 public void deleteTables(long classNameId)
141 throws PortalException, SystemException {
142
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.getTableId());
150 }
151 }
152
153 public void deleteTables(String className)
154 throws PortalException, SystemException {
155
156 long classNameId = PortalUtil.getClassNameId(className);
157
158 deleteTables(classNameId);
159 }
160
161 public ExpandoTable getDefaultTable(long classNameId)
162 throws PortalException, SystemException {
163
164 return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
165 }
166
167 public ExpandoTable getDefaultTable(String className)
168 throws PortalException, SystemException {
169
170 long classNameId = PortalUtil.getClassNameId(className);
171
172 return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
173 }
174
175 public ExpandoTable getTable(long tableId)
176 throws PortalException, SystemException {
177
178 return expandoTablePersistence.findByPrimaryKey(tableId);
179 }
180
181 public ExpandoTable getTable(long classNameId, String name)
182 throws PortalException, SystemException {
183
184 long companyId = CompanyThreadLocal.getCompanyId();
185
186 return expandoTablePersistence.findByC_C_N(
187 companyId, classNameId, name);
188 }
189
190 public ExpandoTable getTable(String className, String name)
191 throws PortalException, SystemException {
192
193 long classNameId = PortalUtil.getClassNameId(className);
194
195 return getTable(classNameId, name);
196 }
197
198 public List<ExpandoTable> getTables(long classNameId)
199 throws SystemException {
200
201 long companyId = CompanyThreadLocal.getCompanyId();
202
203 return expandoTablePersistence.findByC_C(companyId, classNameId);
204 }
205
206 public List<ExpandoTable> getTables(String className)
207 throws SystemException {
208
209 long classNameId = PortalUtil.getClassNameId(className);
210
211 return getTables(classNameId);
212 }
213
214 public ExpandoTable updateTable(long tableId, String name)
215 throws PortalException, SystemException {
216
217 ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
218
219 if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
220 throw new TableNameException(
221 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
222 }
223
224 validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
225
226 table.setName(name);
227
228 return expandoTablePersistence.update(table, false);
229 }
230
231 protected void validate(
232 long companyId, long tableId, long classNameId, String name)
233 throws PortalException, SystemException {
234
235 if (Validator.isNull(name)) {
236 throw new TableNameException();
237 }
238
239 ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
240 companyId, classNameId, name);
241
242 if ((table != null) && (table.getTableId() != tableId)) {
243 throw new DuplicateTableNameException();
244 }
245 }
246
247 }