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