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