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.security.auth.CompanyThreadLocal;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.expando.model.ExpandoRow;
30  import com.liferay.portlet.expando.model.ExpandoTable;
31  import com.liferay.portlet.expando.model.ExpandoTableConstants;
32  import com.liferay.portlet.expando.service.base.ExpandoRowLocalServiceBaseImpl;
33  
34  import java.util.Collections;
35  import java.util.List;
36  
37  /**
38   * <a href="ExpandoRowLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ExpandoRowLocalServiceImpl extends ExpandoRowLocalServiceBaseImpl {
44  
45      public ExpandoRow addRow(long tableId, long classPK)
46          throws PortalException, SystemException {
47  
48          ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
49  
50          long rowId = counterLocalService.increment();
51  
52          ExpandoRow row = expandoRowPersistence.create(rowId);
53  
54          row.setCompanyId(table.getCompanyId());
55          row.setTableId(tableId);
56          row.setClassPK(classPK);
57  
58          expandoRowPersistence.update(row, false);
59  
60          return row;
61      }
62  
63      public void deleteRow(long rowId)
64          throws PortalException, SystemException {
65  
66          // Values
67  
68          expandoValueLocalService.deleteRowValues(rowId);
69  
70          // Row
71  
72          expandoRowPersistence.remove(rowId);
73      }
74  
75      public void deleteRow(long tableId, long classPK)
76          throws PortalException, SystemException {
77  
78          ExpandoRow row = expandoRowPersistence.findByT_C(tableId, classPK);
79  
80          deleteRow(row.getRowId());
81      }
82  
83      public void deleteRow(long classNameId, String tableName, long classPK)
84          throws PortalException, SystemException {
85  
86          ExpandoTable table = expandoTableLocalService.getTable(
87              classNameId, tableName);
88  
89          deleteRow(table.getTableId(), classPK);
90      }
91  
92      public void deleteRow(String className, String tableName, long classPK)
93          throws PortalException, SystemException {
94  
95          long classNameId = PortalUtil.getClassNameId(className);
96  
97          deleteRow(classNameId, tableName, classPK);
98      }
99  
100     public List<ExpandoRow> getDefaultTableRows(
101             long classNameId, int start, int end)
102         throws SystemException {
103 
104         long companyId = CompanyThreadLocal.getCompanyId();
105 
106         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
107             companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
108 
109         if (table == null) {
110             return Collections.EMPTY_LIST;
111         }
112 
113         return expandoRowPersistence.findByTableId(
114             table.getTableId(), start, end);
115     }
116 
117     public List<ExpandoRow> getDefaultTableRows(
118             String className, int start, int end)
119         throws SystemException {
120 
121         long classNameId = PortalUtil.getClassNameId(className);
122 
123         return getDefaultTableRows(classNameId, start, end);
124     }
125 
126     public int getDefaultTableRowsCount(long classNameId)
127         throws SystemException {
128 
129         long companyId = CompanyThreadLocal.getCompanyId();
130 
131         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
132             companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
133 
134         if (table == null) {
135             return 0;
136         }
137 
138         return expandoRowPersistence.countByTableId(table.getTableId());
139     }
140 
141     public int getDefaultTableRowsCount(String className)
142         throws SystemException {
143 
144         long classNameId = PortalUtil.getClassNameId(className);
145 
146         return getDefaultTableRowsCount(classNameId);
147     }
148 
149     public ExpandoRow getRow(long rowId)
150         throws PortalException, SystemException {
151 
152         return expandoRowPersistence.findByPrimaryKey(rowId);
153     }
154 
155     public ExpandoRow getRow(long tableId, long classPK)
156         throws PortalException, SystemException {
157 
158         return expandoRowPersistence.findByT_C(tableId, classPK);
159     }
160 
161     public ExpandoRow getRow(long classNameId, String tableName, long classPK)
162         throws SystemException {
163 
164         long companyId = CompanyThreadLocal.getCompanyId();
165 
166         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
167             companyId, classNameId, tableName);
168 
169         if (table == null) {
170             return null;
171         }
172 
173         return expandoRowPersistence.fetchByT_C(table.getTableId(), classPK);
174     }
175 
176     public ExpandoRow getRow(String className, String tableName, long classPK)
177         throws SystemException {
178 
179         long classNameId = PortalUtil.getClassNameId(className);
180 
181         return getRow(classNameId, tableName, classPK);
182     }
183 
184     public List<ExpandoRow> getRows(long tableId, int start, int end)
185         throws SystemException {
186 
187         return expandoRowPersistence.findByTableId(tableId, start, end);
188     }
189 
190     public List<ExpandoRow> getRows(
191             long classNameId, String tableName, int start, int end)
192         throws SystemException {
193 
194         long companyId = CompanyThreadLocal.getCompanyId();
195 
196         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
197             companyId, classNameId, tableName);
198 
199         if (table == null) {
200             return Collections.EMPTY_LIST;
201         }
202 
203         return expandoRowPersistence.findByTableId(
204             table.getTableId(), start, end);
205     }
206 
207     public List<ExpandoRow> getRows(
208             String className, String tableName, int start, int end)
209         throws SystemException {
210 
211         long classNameId = PortalUtil.getClassNameId(className);
212 
213         return getRows(classNameId, tableName, start, end);
214     }
215 
216     public int getRowsCount(long tableId) throws SystemException {
217         return expandoRowPersistence.countByTableId(tableId);
218     }
219 
220     public int getRowsCount(long classNameId, String tableName)
221         throws SystemException {
222 
223         long companyId = CompanyThreadLocal.getCompanyId();
224 
225         ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
226             companyId, classNameId, tableName);
227 
228         if (table == null) {
229             return 0;
230         }
231 
232         return expandoRowPersistence.countByTableId(table.getTableId());
233     }
234 
235     public int getRowsCount(String className, String tableName)
236         throws SystemException {
237 
238         long classNameId = PortalUtil.getClassNameId(className);
239 
240         return getRowsCount(classNameId, tableName);
241     }
242 
243 }