1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
39   * <a href="ExpandoTableLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Raymond Augé
43   * @author Brian Wing Shun Chan
44   */
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          // Table
91  
92          expandoTablePersistence.remove(table);
93  
94          // Columns
95  
96          runSQL(
97              "delete from ExpandoColumn where tableId = " + table.getTableId());
98  
99          expandoColumnPersistence.clearCache();
100 
101         // Rows
102 
103         runSQL("delete from ExpandoRow where tableId = " + table.getTableId());
104 
105         expandoRowPersistence.clearCache();
106 
107         // Values
108 
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 }