001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.expando.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.auth.CompanyThreadLocal;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.expando.model.ExpandoRow;
022    import com.liferay.portlet.expando.model.ExpandoTable;
023    import com.liferay.portlet.expando.model.ExpandoTableConstants;
024    import com.liferay.portlet.expando.service.base.ExpandoRowLocalServiceBaseImpl;
025    
026    import java.util.Collections;
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Wesley Gong
032     */
033    public class ExpandoRowLocalServiceImpl extends ExpandoRowLocalServiceBaseImpl {
034    
035            public ExpandoRow addRow(long tableId, long classPK)
036                    throws PortalException, SystemException {
037    
038                    ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
039    
040                    long rowId = counterLocalService.increment();
041    
042                    ExpandoRow row = expandoRowPersistence.create(rowId);
043    
044                    row.setCompanyId(table.getCompanyId());
045                    row.setTableId(tableId);
046                    row.setClassPK(classPK);
047    
048                    expandoRowPersistence.update(row, false);
049    
050                    return row;
051            }
052    
053            public void deleteRow(long rowId)
054                    throws PortalException, SystemException {
055    
056                    // Row
057    
058                    expandoRowPersistence.remove(rowId);
059    
060                    // Values
061    
062                    expandoValueLocalService.deleteRowValues(rowId);
063            }
064    
065            public void deleteRow(long tableId, long classPK)
066                    throws PortalException, SystemException {
067    
068                    ExpandoRow row = expandoRowPersistence.findByT_C(tableId, classPK);
069    
070                    deleteRow(row.getRowId());
071            }
072    
073            public void deleteRow(
074                            long companyId, long classNameId, String tableName, long classPK)
075                    throws PortalException, SystemException {
076    
077                    ExpandoTable table = expandoTableLocalService.getTable(
078                            companyId, classNameId, tableName);
079    
080                    deleteRow(table.getTableId(), classPK);
081            }
082    
083            public void deleteRow(
084                            long companyId, String className, String tableName, long classPK)
085                    throws PortalException, SystemException {
086    
087                    long classNameId = PortalUtil.getClassNameId(className);
088    
089                    deleteRow(companyId, classNameId, tableName, classPK);
090            }
091    
092            public List<ExpandoRow> getDefaultTableRows(
093                            long companyId, long classNameId, int start, int end)
094                    throws SystemException {
095    
096                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
097                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
098    
099                    if (table == null) {
100                            return Collections.EMPTY_LIST;
101                    }
102    
103                    return expandoRowPersistence.findByTableId(
104                            table.getTableId(), start, end);
105            }
106    
107            public List<ExpandoRow> getDefaultTableRows(
108                            long companyId, String className, int start, int end)
109                    throws SystemException {
110    
111                    long classNameId = PortalUtil.getClassNameId(className);
112    
113                    return getDefaultTableRows(companyId, classNameId, start, end);
114            }
115    
116            public int getDefaultTableRowsCount(long companyId, long classNameId)
117                    throws SystemException {
118    
119                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
120                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
121    
122                    if (table == null) {
123                            return 0;
124                    }
125    
126                    return expandoRowPersistence.countByTableId(table.getTableId());
127            }
128    
129            public int getDefaultTableRowsCount(long companyId, String className)
130                    throws SystemException {
131    
132                    long classNameId = PortalUtil.getClassNameId(className);
133    
134                    return getDefaultTableRowsCount(companyId, classNameId);
135            }
136    
137            public ExpandoRow getRow(long rowId)
138                    throws PortalException, SystemException {
139    
140                    return expandoRowPersistence.findByPrimaryKey(rowId);
141            }
142    
143            public ExpandoRow getRow(long tableId, long classPK)
144                    throws PortalException, SystemException {
145    
146                    return expandoRowPersistence.findByT_C(tableId, classPK);
147            }
148    
149            public ExpandoRow getRow(
150                            long companyId, long classNameId, String tableName, long classPK)
151                    throws SystemException {
152    
153                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
154                            companyId, classNameId, tableName);
155    
156                    if (table == null) {
157                            return null;
158                    }
159    
160                    return expandoRowPersistence.fetchByT_C(table.getTableId(), classPK);
161            }
162    
163            public ExpandoRow getRow(
164                            long companyId, String className, String tableName, long classPK)
165                    throws SystemException {
166    
167                    long classNameId = PortalUtil.getClassNameId(className);
168    
169                    return getRow(companyId, classNameId, tableName, classPK);
170            }
171    
172            public List<ExpandoRow> getRows(long tableId, int start, int end)
173                    throws SystemException {
174    
175                    return expandoRowPersistence.findByTableId(tableId, start, end);
176            }
177    
178            public List<ExpandoRow> getRows(
179                            long companyId, long classNameId, String tableName, int start,
180                            int end)
181                    throws SystemException {
182    
183                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
184                            companyId, classNameId, tableName);
185    
186                    if (table == null) {
187                            return Collections.EMPTY_LIST;
188                    }
189    
190                    return expandoRowPersistence.findByTableId(
191                            table.getTableId(), start, end);
192            }
193    
194            public List<ExpandoRow> getRows(
195                            long companyId, String className, String tableName, int start,
196                            int end)
197                    throws SystemException {
198    
199                    long classNameId = PortalUtil.getClassNameId(className);
200    
201                    return getRows(companyId, classNameId, tableName, start, end);
202            }
203    
204            /**
205             * @deprecated {@link #getRows(long, String, String, int, int)}
206             */
207            public List<ExpandoRow> getRows(
208                            String className, String tableName, int start, int end)
209                    throws SystemException {
210    
211                    long companyId = CompanyThreadLocal.getCompanyId();
212    
213                    return getRows(companyId, className, 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 companyId, long classNameId, String tableName)
221                    throws SystemException {
222    
223                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
224                            companyId, classNameId, tableName);
225    
226                    if (table == null) {
227                            return 0;
228                    }
229    
230                    return expandoRowPersistence.countByTableId(table.getTableId());
231            }
232    
233            public int getRowsCount(long companyId, String className, String tableName)
234                    throws SystemException {
235    
236                    long classNameId = PortalUtil.getClassNameId(className);
237    
238                    return getRowsCount(companyId, classNameId, tableName);
239            }
240    
241            /**
242             * @deprecated {@link #getRowsCount(long, String, String)}
243             */
244            public int getRowsCount(String className, String tableName)
245                    throws SystemException {
246    
247                    long companyId = CompanyThreadLocal.getCompanyId();
248    
249                    return getRowsCount(companyId, className, tableName);
250            }
251    
252    }