1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.expando.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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 classNameId)
41          throws PortalException, SystemException {
42  
43          return addTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
44      }
45  
46      public ExpandoTable addDefaultTable(String className)
47          throws PortalException, SystemException {
48  
49          return addTable(className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
50      }
51  
52      public ExpandoTable addTable(long classNameId, String name)
53          throws PortalException, SystemException {
54  
55          long companyId = CompanyThreadLocal.getCompanyId();
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      public ExpandoTable addTable(String className, String name)
73          throws PortalException, SystemException {
74  
75          long classNameId = PortalUtil.getClassNameId(className);
76  
77          return addTable(classNameId, name);
78      }
79  
80      public void deleteTable(ExpandoTable table) throws SystemException {
81  
82          // Table
83  
84          expandoTablePersistence.remove(table);
85  
86          // Columns
87  
88          runSQL(
89              "delete from ExpandoColumn where tableId = " + table.getTableId());
90  
91          expandoColumnPersistence.clearCache();
92  
93          // Rows
94  
95          runSQL("delete from ExpandoRow where tableId = " + table.getTableId());
96  
97          expandoRowPersistence.clearCache();
98  
99          // Values
100 
101         runSQL(
102             "delete from ExpandoValue where tableId = " + table.getTableId());
103 
104         expandoValuePersistence.clearCache();
105     }
106 
107     public void deleteTable(long tableId)
108         throws PortalException, SystemException {
109 
110         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
111 
112         deleteTable(table);
113     }
114 
115     public void deleteTable(long classNameId, String name)
116         throws PortalException, SystemException {
117 
118         long companyId = CompanyThreadLocal.getCompanyId();
119 
120         ExpandoTable table = expandoTablePersistence.findByC_C_N(
121             companyId, classNameId, name);
122 
123         deleteTable(table);
124     }
125 
126     public void deleteTable(String className, String name)
127         throws PortalException, SystemException {
128 
129         long classNameId = PortalUtil.getClassNameId(className);
130 
131         deleteTable(classNameId, name);
132     }
133 
134     public void deleteTables(long classNameId) throws SystemException {
135         long companyId = CompanyThreadLocal.getCompanyId();
136 
137         List<ExpandoTable> tables = expandoTablePersistence.findByC_C(
138             companyId, classNameId);
139 
140         for (ExpandoTable table : tables) {
141             deleteTable(table);
142         }
143     }
144 
145     public void deleteTables(String className) throws SystemException {
146         long classNameId = PortalUtil.getClassNameId(className);
147 
148         deleteTables(classNameId);
149     }
150 
151     public ExpandoTable getDefaultTable(long classNameId)
152         throws PortalException, SystemException {
153 
154         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
155     }
156 
157     public ExpandoTable getDefaultTable(String className)
158         throws PortalException, SystemException {
159 
160         long classNameId = PortalUtil.getClassNameId(className);
161 
162         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
163     }
164 
165     public ExpandoTable getTable(long tableId)
166         throws PortalException, SystemException {
167 
168         return expandoTablePersistence.findByPrimaryKey(tableId);
169     }
170 
171     public ExpandoTable getTable(long classNameId, String name)
172         throws PortalException, SystemException {
173 
174         long companyId = CompanyThreadLocal.getCompanyId();
175 
176         return expandoTablePersistence.findByC_C_N(
177             companyId, classNameId, name);
178     }
179 
180     public ExpandoTable getTable(String className, String name)
181         throws PortalException, SystemException {
182 
183         long classNameId = PortalUtil.getClassNameId(className);
184 
185         return getTable(classNameId, name);
186     }
187 
188     public List<ExpandoTable> getTables(long classNameId)
189         throws SystemException {
190 
191         long companyId = CompanyThreadLocal.getCompanyId();
192 
193         return expandoTablePersistence.findByC_C(companyId, classNameId);
194     }
195 
196     public List<ExpandoTable> getTables(String className)
197         throws SystemException {
198 
199         long classNameId = PortalUtil.getClassNameId(className);
200 
201         return getTables(classNameId);
202     }
203 
204     public ExpandoTable updateTable(long tableId, String name)
205         throws PortalException, SystemException {
206 
207         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
208 
209         if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
210             throw new TableNameException(
211                 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
212         }
213 
214         validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
215 
216         table.setName(name);
217 
218         return expandoTablePersistence.update(table, false);
219     }
220 
221     protected void validate(
222             long companyId, long tableId, long classNameId, String name)
223         throws PortalException, SystemException {
224 
225         if (Validator.isNull(name)) {
226             throw new TableNameException();
227         }
228 
229         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
230             companyId, classNameId, name);
231 
232         if ((table != null) && (table.getTableId() != tableId)) {
233             throw new DuplicateTableNameException();
234         }
235     }
236 
237 }