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.util.PortalUtil;
29 import com.liferay.portlet.expando.DuplicateTableNameException;
30 import com.liferay.portlet.expando.TableNameException;
31 import com.liferay.portlet.expando.model.ExpandoTable;
32 import com.liferay.portlet.expando.model.ExpandoTableConstants;
33 import com.liferay.portlet.expando.service.base.ExpandoTableLocalServiceBaseImpl;
34
35 import java.util.List;
36
37
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 validate(0, classNameId, name);
64
65 long tableId = counterLocalService.increment();
66
67 ExpandoTable table = expandoTablePersistence.create(tableId);
68
69 table.setClassNameId(classNameId);
70 table.setName(name);
71
72 expandoTablePersistence.update(table, false);
73
74 return table;
75 }
76
77 public ExpandoTable addTable(String className, String name)
78 throws PortalException, SystemException {
79
80 long classNameId = PortalUtil.getClassNameId(className);
81
82 return addTable(classNameId, name);
83 }
84
85 public void deleteTable(long tableId)
86 throws PortalException, SystemException {
87
88
90 runSQL("DELETE FROM ExpandoValue WHERE tableId = " + tableId);
91
92
94 runSQL("DELETE FROM ExpandoColumn WHERE tableId = " + tableId);
95
96
98 runSQL("DELETE FROM ExpandoRow WHERE tableId = " + tableId);
99
100 expandoColumnPersistence.clearCache();
101 expandoRowPersistence.clearCache();
102 expandoValuePersistence.clearCache();
103
104
111
112
114 expandoTablePersistence.remove(tableId);
115 }
116
117 public void deleteTable(long classNameId, String name)
118 throws PortalException, SystemException {
119
120 ExpandoTable table = expandoTablePersistence.findByC_N(
121 classNameId, name);
122
123 deleteTable(table.getTableId());
124 }
125
126 public void deleteTable(String className, String name)
127 throws PortalException, SystemException {
128
129 long classNameId = PortalUtil.getClassNameId(className);
130
131 deleteTable(classNameId, name);
132 }
133
134 public void deleteTables(long classNameId)
135 throws PortalException, SystemException {
136
137 List<ExpandoTable> tables = expandoTablePersistence.findByClassNameId(
138 classNameId);
139
140 for (ExpandoTable table : tables) {
141 deleteTable(table.getTableId());
142 }
143 }
144
145 public void deleteTables(String className)
146 throws PortalException, SystemException {
147
148 long classNameId = PortalUtil.getClassNameId(className);
149
150 deleteTables(classNameId);
151 }
152
153 public ExpandoTable getDefaultTable(long classNameId)
154 throws PortalException, SystemException {
155
156 return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
157 }
158
159 public ExpandoTable getDefaultTable(String className)
160 throws PortalException, SystemException {
161
162 long classNameId = PortalUtil.getClassNameId(className);
163
164 return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
165 }
166
167 public ExpandoTable getTable(long tableId)
168 throws PortalException, SystemException {
169
170 return expandoTablePersistence.findByPrimaryKey(tableId);
171 }
172
173 public ExpandoTable getTable(long classNameId, String name)
174 throws PortalException, SystemException {
175
176 return expandoTablePersistence.findByC_N(classNameId, name);
177 }
178
179 public ExpandoTable getTable(String className, String name)
180 throws PortalException, SystemException {
181
182 long classNameId = PortalUtil.getClassNameId(className);
183
184 return getTable(classNameId, name);
185 }
186
187 public List<ExpandoTable> getTables(long classNameId)
188 throws SystemException {
189
190 return expandoTablePersistence.findByClassNameId(classNameId);
191 }
192
193 public List<ExpandoTable> getTables(String className)
194 throws SystemException {
195
196 long classNameId = PortalUtil.getClassNameId(className);
197
198 return getTables(classNameId);
199 }
200
201 public ExpandoTable updateTable(long tableId, String name)
202 throws PortalException, SystemException {
203
204 ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
205
206 if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
207 throw new TableNameException(
208 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
209 }
210
211 validate(tableId, table.getClassNameId(), name);
212
213 table.setName(name);
214
215 return expandoTablePersistence.update(table, false);
216 }
217
218 protected void validate(long tableId, long classNameId, String name)
219 throws PortalException, SystemException {
220
221 if (Validator.isNull(name)) {
222 throw new TableNameException();
223 }
224
225 ExpandoTable table = expandoTablePersistence.fetchByC_N(
226 classNameId, name);
227
228 if ((table != null) && (table.getTableId() != tableId)) {
229 throw new DuplicateTableNameException();
230 }
231 }
232
233 }