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