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