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(long tableId)
89          throws PortalException, SystemException {
90  
91          // Values
92  
93          runSQL("DELETE FROM ExpandoValue WHERE tableId = " + tableId);
94  
95          // Columns
96  
97          runSQL("DELETE FROM ExpandoColumn WHERE tableId = " + tableId);
98  
99          // Rows
100 
101         runSQL("DELETE FROM ExpandoRow WHERE tableId = " + tableId);
102 
103         expandoColumnPersistence.clearCache();
104         expandoRowPersistence.clearCache();
105         expandoValuePersistence.clearCache();
106 
107         /*// Columns
108 
109         expandoColumnLocalService.deleteColumns(tableId);
110 
111         // Rows
112 
113         expandoRowPersistence.removeByTableId(tableId);*/
114 
115         // Table
116 
117         expandoTablePersistence.remove(tableId);
118     }
119 
120     public void deleteTable(long classNameId, String name)
121         throws PortalException, SystemException {
122 
123         long companyId = CompanyThreadLocal.getCompanyId();
124 
125         ExpandoTable table = expandoTablePersistence.findByC_C_N(
126             companyId, classNameId, name);
127 
128         deleteTable(table.getTableId());
129     }
130 
131     public void deleteTable(String className, String name)
132         throws PortalException, SystemException {
133 
134         long classNameId = PortalUtil.getClassNameId(className);
135 
136         deleteTable(classNameId, name);
137     }
138 
139     public void deleteTables(long classNameId)
140         throws PortalException, SystemException {
141 
142         long companyId = CompanyThreadLocal.getCompanyId();
143 
144         List<ExpandoTable> tables = expandoTablePersistence.findByC_C(
145             companyId, classNameId);
146 
147         for (ExpandoTable table : tables) {
148             deleteTable(table.getTableId());
149         }
150     }
151 
152     public void deleteTables(String className)
153         throws PortalException, SystemException {
154 
155         long classNameId = PortalUtil.getClassNameId(className);
156 
157         deleteTables(classNameId);
158     }
159 
160     public ExpandoTable getDefaultTable(long classNameId)
161         throws PortalException, SystemException {
162 
163         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
164     }
165 
166     public ExpandoTable getDefaultTable(String className)
167         throws PortalException, SystemException {
168 
169         long classNameId = PortalUtil.getClassNameId(className);
170 
171         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
172     }
173 
174     public ExpandoTable getTable(long tableId)
175         throws PortalException, SystemException {
176 
177         return expandoTablePersistence.findByPrimaryKey(tableId);
178     }
179 
180     public ExpandoTable getTable(long classNameId, String name)
181         throws PortalException, SystemException {
182 
183         long companyId = CompanyThreadLocal.getCompanyId();
184 
185         return expandoTablePersistence.findByC_C_N(
186             companyId, classNameId, name);
187     }
188 
189     public ExpandoTable getTable(String className, String name)
190         throws PortalException, SystemException {
191 
192         long classNameId = PortalUtil.getClassNameId(className);
193 
194         return getTable(classNameId, name);
195     }
196 
197     public List<ExpandoTable> getTables(long classNameId)
198         throws SystemException {
199 
200         long companyId = CompanyThreadLocal.getCompanyId();
201 
202         return expandoTablePersistence.findByC_C(companyId, classNameId);
203     }
204 
205     public List<ExpandoTable> getTables(String className)
206         throws SystemException {
207 
208         long classNameId = PortalUtil.getClassNameId(className);
209 
210         return getTables(classNameId);
211     }
212 
213     public ExpandoTable updateTable(long tableId, String name)
214         throws PortalException, SystemException {
215 
216         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
217 
218         if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
219             throw new TableNameException(
220                 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
221         }
222 
223         validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
224 
225         table.setName(name);
226 
227         return expandoTablePersistence.update(table, false);
228     }
229 
230     protected void validate(
231             long companyId, long tableId, long classNameId, String name)
232         throws PortalException, SystemException {
233 
234         if (Validator.isNull(name)) {
235             throw new TableNameException();
236         }
237 
238         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
239             companyId, classNameId, name);
240 
241         if ((table != null) && (table.getTableId() != tableId)) {
242             throw new DuplicateTableNameException();
243         }
244     }
245 
246 }