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