1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.expando.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.security.auth.CompanyThreadLocal;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portlet.expando.DuplicateTableNameException;
23  import com.liferay.portlet.expando.TableNameException;
24  import com.liferay.portlet.expando.model.ExpandoTable;
25  import com.liferay.portlet.expando.model.ExpandoTableConstants;
26  import com.liferay.portlet.expando.service.base.ExpandoTableLocalServiceBaseImpl;
27  
28  import java.util.List;
29  
30  /**
31   * <a href="ExpandoTableLocalServiceImpl.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Raymond Augé
35   * @author Brian Wing Shun Chan
36   */
37  public class ExpandoTableLocalServiceImpl
38      extends ExpandoTableLocalServiceBaseImpl {
39  
40      public ExpandoTable addDefaultTable(long companyId, long classNameId)
41          throws PortalException, SystemException {
42  
43          return addTable(
44              companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
45      }
46  
47      public ExpandoTable addDefaultTable(long companyId, String className)
48          throws PortalException, SystemException {
49  
50          return addTable(
51              companyId, className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
52      }
53  
54      public ExpandoTable addTable(long companyId, long classNameId, String name)
55          throws PortalException, SystemException {
56  
57          validate(companyId, 0, classNameId, name);
58  
59          long tableId = counterLocalService.increment();
60  
61          ExpandoTable table = expandoTablePersistence.create(tableId);
62  
63          table.setCompanyId(companyId);
64          table.setClassNameId(classNameId);
65          table.setName(name);
66  
67          expandoTablePersistence.update(table, false);
68  
69          return table;
70      }
71  
72      /**
73       * @deprecated {@link #addTable(long, long, String)}
74       */
75      public ExpandoTable addTable(long classNameId, String name)
76          throws PortalException, SystemException {
77  
78          long companyId = CompanyThreadLocal.getCompanyId();
79  
80          return addTable(companyId, classNameId, name);
81      }
82  
83      public ExpandoTable addTable(long companyId, String className, String name)
84          throws PortalException, SystemException {
85  
86          long classNameId = PortalUtil.getClassNameId(className);
87  
88          return addTable(companyId, classNameId, name);
89      }
90  
91      /**
92       * @deprecated {@link #addTable(long, String, String)}
93       */
94      public ExpandoTable addTable(String className, String name)
95          throws PortalException, SystemException {
96  
97          long companyId = CompanyThreadLocal.getCompanyId();
98  
99          return addTable(companyId, className, name);
100     }
101 
102     public void deleteTable(ExpandoTable table) throws SystemException {
103 
104         // Table
105 
106         expandoTablePersistence.remove(table);
107 
108         // Columns
109 
110         runSQL(
111             "delete from ExpandoColumn where tableId = " + table.getTableId());
112 
113         expandoColumnPersistence.clearCache();
114 
115         // Rows
116 
117         runSQL("delete from ExpandoRow where tableId = " + table.getTableId());
118 
119         expandoRowPersistence.clearCache();
120 
121         // Values
122 
123         runSQL(
124             "delete from ExpandoValue where tableId = " + table.getTableId());
125 
126         expandoValuePersistence.clearCache();
127     }
128 
129     public void deleteTable(long tableId)
130         throws PortalException, SystemException {
131 
132         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
133 
134         deleteTable(table);
135     }
136 
137     public void deleteTable(long companyId, long classNameId, String name)
138         throws PortalException, SystemException {
139 
140         ExpandoTable table = expandoTablePersistence.findByC_C_N(
141             companyId, classNameId, name);
142 
143         deleteTable(table);
144     }
145 
146     public void deleteTable(long companyId, String className, String name)
147         throws PortalException, SystemException {
148 
149         long classNameId = PortalUtil.getClassNameId(className);
150 
151         deleteTable(companyId, classNameId, name);
152     }
153 
154     public void deleteTables(long companyId, long classNameId)
155         throws SystemException {
156 
157         List<ExpandoTable> tables = expandoTablePersistence.findByC_C(
158             companyId, classNameId);
159 
160         for (ExpandoTable table : tables) {
161             deleteTable(table);
162         }
163     }
164 
165     public void deleteTables(long companyId, String className)
166         throws SystemException {
167 
168         long classNameId = PortalUtil.getClassNameId(className);
169 
170         deleteTables(companyId, classNameId);
171     }
172 
173     public ExpandoTable getDefaultTable(long companyId, long classNameId)
174         throws PortalException, SystemException {
175 
176         return getTable(
177             companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
178     }
179 
180     public ExpandoTable getDefaultTable(long companyId, String className)
181         throws PortalException, SystemException {
182 
183         long classNameId = PortalUtil.getClassNameId(className);
184 
185         return getTable(
186             companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
187     }
188 
189     public ExpandoTable getTable(long tableId)
190         throws PortalException, SystemException {
191 
192         return expandoTablePersistence.findByPrimaryKey(tableId);
193     }
194 
195     public ExpandoTable getTable(long companyId, long classNameId, String name)
196         throws PortalException, SystemException {
197 
198         return expandoTablePersistence.findByC_C_N(
199             companyId, classNameId, name);
200     }
201 
202     /**
203      * @deprecated {@link #getTable(long, long, String)}
204      */
205     public ExpandoTable getTable(long classNameId, String name)
206         throws PortalException, SystemException {
207 
208         long companyId = CompanyThreadLocal.getCompanyId();
209 
210         return getTable(companyId, classNameId, name);
211     }
212 
213     public ExpandoTable getTable(long companyId, String className, String name)
214         throws PortalException, SystemException {
215 
216         long classNameId = PortalUtil.getClassNameId(className);
217 
218         return getTable(companyId, classNameId, name);
219     }
220 
221     /**
222      * @deprecated {@link #getTable(long, String, String)}
223      */
224     public ExpandoTable getTable(String className, String name)
225         throws PortalException, SystemException {
226 
227         long companyId = CompanyThreadLocal.getCompanyId();
228 
229         return getTable(companyId, className, name);
230     }
231 
232     public List<ExpandoTable> getTables(long companyId, long classNameId)
233         throws SystemException {
234 
235         return expandoTablePersistence.findByC_C(companyId, classNameId);
236     }
237 
238     public List<ExpandoTable> getTables(long companyId, String className)
239         throws SystemException {
240 
241         long classNameId = PortalUtil.getClassNameId(className);
242 
243         return getTables(companyId, classNameId);
244     }
245 
246     public ExpandoTable updateTable(long tableId, String name)
247         throws PortalException, SystemException {
248 
249         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
250 
251         if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
252             throw new TableNameException(
253                 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
254         }
255 
256         validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
257 
258         table.setName(name);
259 
260         return expandoTablePersistence.update(table, false);
261     }
262 
263     protected void validate(
264             long companyId, long tableId, long classNameId, String name)
265         throws PortalException, SystemException {
266 
267         if (Validator.isNull(name)) {
268             throw new TableNameException();
269         }
270 
271         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
272             companyId, classNameId, name);
273 
274         if ((table != null) && (table.getTableId() != tableId)) {
275             throw new DuplicateTableNameException();
276         }
277     }
278 
279 }