1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.util.PortalUtil;
21  import com.liferay.portlet.expando.DuplicateTableNameException;
22  import com.liferay.portlet.expando.TableNameException;
23  import com.liferay.portlet.expando.model.ExpandoTable;
24  import com.liferay.portlet.expando.model.ExpandoTableConstants;
25  import com.liferay.portlet.expando.service.base.ExpandoTableLocalServiceBaseImpl;
26  
27  import java.util.List;
28  
29  /**
30   * <a href="ExpandoTableLocalServiceImpl.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * @author Raymond Augé
34   * @author Brian Wing Shun Chan
35   */
36  public class ExpandoTableLocalServiceImpl
37      extends ExpandoTableLocalServiceBaseImpl {
38  
39      public ExpandoTable addDefaultTable(long classNameId)
40          throws PortalException, SystemException {
41  
42          return addTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
43      }
44  
45      public ExpandoTable addDefaultTable(String className)
46          throws PortalException, SystemException {
47  
48          return addTable(className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
49      }
50  
51      public ExpandoTable addTable(long classNameId, String name)
52          throws PortalException, SystemException {
53  
54          validate(0, classNameId, name);
55  
56          long tableId = counterLocalService.increment();
57  
58          ExpandoTable table = expandoTablePersistence.create(tableId);
59  
60          table.setClassNameId(classNameId);
61          table.setName(name);
62  
63          expandoTablePersistence.update(table, false);
64  
65          return table;
66      }
67  
68      public ExpandoTable addTable(String className, String name)
69          throws PortalException, SystemException {
70  
71          long classNameId = PortalUtil.getClassNameId(className);
72  
73          return addTable(classNameId, name);
74      }
75  
76      public void deleteTable(ExpandoTable table) throws SystemException {
77  
78          // Table
79  
80          expandoTablePersistence.remove(table);
81  
82          // Columns
83  
84          runSQL(
85              "delete from ExpandoColumn where tableId = " + table.getTableId());
86  
87          expandoColumnPersistence.clearCache();
88  
89          // Rows
90  
91          runSQL("delete from ExpandoRow where tableId = " + table.getTableId());
92  
93          expandoRowPersistence.clearCache();
94  
95          // Values
96  
97          runSQL(
98              "delete from ExpandoValue where tableId = " + table.getTableId());
99  
100         expandoValuePersistence.clearCache();
101     }
102 
103     public void deleteTable(long tableId)
104         throws PortalException, SystemException {
105 
106         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
107 
108         deleteTable(table);
109     }
110 
111     public void deleteTable(long classNameId, String name)
112         throws PortalException, SystemException {
113 
114         ExpandoTable table = expandoTablePersistence.findByC_N(
115             classNameId, name);
116 
117         deleteTable(table);
118     }
119 
120     public void deleteTable(String className, String name)
121         throws PortalException, SystemException {
122 
123         long classNameId = PortalUtil.getClassNameId(className);
124 
125         deleteTable(classNameId, name);
126     }
127 
128     public void deleteTables(long classNameId) throws SystemException {
129         List<ExpandoTable> tables = expandoTablePersistence.findByClassNameId(
130             classNameId);
131 
132         for (ExpandoTable table : tables) {
133             deleteTable(table);
134         }
135     }
136 
137     public void deleteTables(String className) throws SystemException {
138         long classNameId = PortalUtil.getClassNameId(className);
139 
140         deleteTables(classNameId);
141     }
142 
143     public ExpandoTable getDefaultTable(long classNameId)
144         throws PortalException, SystemException {
145 
146         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
147     }
148 
149     public ExpandoTable getDefaultTable(String className)
150         throws PortalException, SystemException {
151 
152         long classNameId = PortalUtil.getClassNameId(className);
153 
154         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
155     }
156 
157     public ExpandoTable getTable(long tableId)
158         throws PortalException, SystemException {
159 
160         return expandoTablePersistence.findByPrimaryKey(tableId);
161     }
162 
163     public ExpandoTable getTable(long classNameId, String name)
164         throws PortalException, SystemException {
165 
166         return expandoTablePersistence.findByC_N(classNameId, name);
167     }
168 
169     public ExpandoTable getTable(String className, String name)
170         throws PortalException, SystemException {
171 
172         long classNameId = PortalUtil.getClassNameId(className);
173 
174         return getTable(classNameId, name);
175     }
176 
177     public List<ExpandoTable> getTables(long classNameId)
178         throws SystemException {
179 
180         return expandoTablePersistence.findByClassNameId(classNameId);
181     }
182 
183     public List<ExpandoTable> getTables(String className)
184         throws SystemException {
185 
186         long classNameId = PortalUtil.getClassNameId(className);
187 
188         return getTables(classNameId);
189     }
190 
191     public ExpandoTable updateTable(long tableId, String name)
192         throws PortalException, SystemException {
193 
194         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
195 
196         if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
197             throw new TableNameException(
198                 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
199         }
200 
201         validate(tableId, table.getClassNameId(), name);
202 
203         table.setName(name);
204 
205         return expandoTablePersistence.update(table, false);
206     }
207 
208     protected void validate(long tableId, long classNameId, String name)
209         throws PortalException, SystemException {
210 
211         if (Validator.isNull(name)) {
212             throw new TableNameException();
213         }
214 
215         ExpandoTable table = expandoTablePersistence.fetchByC_N(
216             classNameId, name);
217 
218         if ((table != null) && (table.getTableId() != tableId)) {
219             throw new DuplicateTableNameException();
220         }
221     }
222 
223 }