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.util.PortalUtil;
29  import com.liferay.portlet.expando.DuplicateTableNameException;
30  import com.liferay.portlet.expando.TableNameException;
31  import com.liferay.portlet.expando.model.ExpandoTable;
32  import com.liferay.portlet.expando.model.ExpandoTableConstants;
33  import com.liferay.portlet.expando.service.base.ExpandoTableLocalServiceBaseImpl;
34  
35  import java.util.List;
36  
37  /**
38   * <a href="ExpandoTableLocalServiceImpl.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Raymond Augé
42   * @author Brian Wing Shun Chan
43   */
44  public class ExpandoTableLocalServiceImpl
45      extends ExpandoTableLocalServiceBaseImpl {
46  
47      public ExpandoTable addDefaultTable(long classNameId)
48          throws PortalException, SystemException {
49  
50          return addTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
51      }
52  
53      public ExpandoTable addDefaultTable(String className)
54          throws PortalException, SystemException {
55  
56          return addTable(className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
57      }
58  
59      public ExpandoTable addTable(long classNameId, String name)
60          throws PortalException, SystemException {
61  
62          validate(0, classNameId, name);
63  
64          long tableId = counterLocalService.increment();
65  
66          ExpandoTable table = expandoTablePersistence.create(tableId);
67  
68          table.setClassNameId(classNameId);
69          table.setName(name);
70  
71          expandoTablePersistence.update(table, false);
72  
73          return table;
74      }
75  
76      public ExpandoTable addTable(String className, String name)
77          throws PortalException, SystemException {
78  
79          long classNameId = PortalUtil.getClassNameId(className);
80  
81          return addTable(classNameId, name);
82      }
83  
84      public void deleteTable(long tableId)
85          throws PortalException, SystemException {
86  
87          // Values
88  
89          runSQL("DELETE FROM ExpandoValue WHERE tableId = " + tableId);
90  
91          // Columns
92  
93          runSQL("DELETE FROM ExpandoColumn WHERE tableId = " + tableId);
94  
95          // Rows
96  
97          runSQL("DELETE FROM ExpandoRow WHERE tableId = " + tableId);
98  
99          expandoColumnPersistence.clearCache();
100         expandoRowPersistence.clearCache();
101         expandoValuePersistence.clearCache();
102 
103         /*// Columns
104 
105         expandoColumnLocalService.deleteColumns(tableId);
106 
107         // Rows
108 
109         expandoRowPersistence.removeByTableId(tableId);*/
110 
111         // Table
112 
113         expandoTablePersistence.remove(tableId);
114     }
115 
116     public void deleteTable(long classNameId, String name)
117         throws PortalException, SystemException {
118 
119         ExpandoTable table = expandoTablePersistence.findByC_N(
120             classNameId, name);
121 
122         deleteTable(table.getTableId());
123     }
124 
125     public void deleteTable(String className, String name)
126         throws PortalException, SystemException {
127 
128         long classNameId = PortalUtil.getClassNameId(className);
129 
130         deleteTable(classNameId, name);
131     }
132 
133     public void deleteTables(long classNameId)
134         throws PortalException, SystemException {
135 
136         List<ExpandoTable> tables = expandoTablePersistence.findByClassNameId(
137             classNameId);
138 
139         for (ExpandoTable table : tables) {
140             deleteTable(table.getTableId());
141         }
142     }
143 
144     public void deleteTables(String className)
145         throws PortalException, SystemException {
146 
147         long classNameId = PortalUtil.getClassNameId(className);
148 
149         deleteTables(classNameId);
150     }
151 
152     public ExpandoTable getDefaultTable(long classNameId)
153         throws PortalException, SystemException {
154 
155         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
156     }
157 
158     public ExpandoTable getDefaultTable(String className)
159         throws PortalException, SystemException {
160 
161         long classNameId = PortalUtil.getClassNameId(className);
162 
163         return getTable(classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
164     }
165 
166     public ExpandoTable getTable(long tableId)
167         throws PortalException, SystemException {
168 
169         return expandoTablePersistence.findByPrimaryKey(tableId);
170     }
171 
172     public ExpandoTable getTable(long classNameId, String name)
173         throws PortalException, SystemException {
174 
175         return expandoTablePersistence.findByC_N(classNameId, name);
176     }
177 
178     public ExpandoTable getTable(String className, String name)
179         throws PortalException, SystemException {
180 
181         long classNameId = PortalUtil.getClassNameId(className);
182 
183         return getTable(classNameId, name);
184     }
185 
186     public List<ExpandoTable> getTables(long classNameId)
187         throws SystemException {
188 
189         return expandoTablePersistence.findByClassNameId(classNameId);
190     }
191 
192     public List<ExpandoTable> getTables(String className)
193         throws SystemException {
194 
195         long classNameId = PortalUtil.getClassNameId(className);
196 
197         return getTables(classNameId);
198     }
199 
200     public ExpandoTable updateTable(long tableId, String name)
201         throws PortalException, SystemException {
202 
203         ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
204 
205         if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
206             throw new TableNameException(
207                 "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
208         }
209 
210         validate(tableId, table.getClassNameId(), name);
211 
212         table.setName(name);
213 
214         return expandoTablePersistence.update(table, false);
215     }
216 
217     protected void validate(long tableId, long classNameId, String name)
218         throws PortalException, SystemException {
219 
220         if (Validator.isNull(name)) {
221             throw new TableNameException();
222         }
223 
224         ExpandoTable table = expandoTablePersistence.fetchByC_N(
225             classNameId, name);
226 
227         if ((table != null) && (table.getTableId() != tableId)) {
228             throw new DuplicateTableNameException();
229         }
230     }
231 
232 }