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