001    /**
002     * Copyright (c) 2000-present 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.portlet.expando.model.ExpandoRow;
019    import com.liferay.portlet.expando.model.ExpandoTable;
020    import com.liferay.portlet.expando.model.ExpandoTableConstants;
021    import com.liferay.portlet.expando.service.base.ExpandoRowLocalServiceBaseImpl;
022    
023    import java.util.Collections;
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Wesley Gong
029     */
030    public class ExpandoRowLocalServiceImpl extends ExpandoRowLocalServiceBaseImpl {
031    
032            @Override
033            public ExpandoRow addRow(long tableId, long classPK)
034                    throws PortalException {
035    
036                    ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
037    
038                    long rowId = counterLocalService.increment();
039    
040                    ExpandoRow row = expandoRowPersistence.create(rowId);
041    
042                    row.setCompanyId(table.getCompanyId());
043                    row.setTableId(tableId);
044                    row.setClassPK(classPK);
045    
046                    expandoRowPersistence.update(row);
047    
048                    return row;
049            }
050    
051            @Override
052            public void deleteRow(ExpandoRow row) {
053    
054                    // Row
055    
056                    expandoRowPersistence.remove(row);
057    
058                    // Values
059    
060                    expandoValueLocalService.deleteRowValues(row.getRowId());
061            }
062    
063            @Override
064            public void deleteRow(long rowId) throws PortalException {
065                    ExpandoRow row = expandoRowPersistence.findByPrimaryKey(rowId);
066    
067                    deleteRow(row);
068            }
069    
070            @Override
071            public void deleteRow(long tableId, long classPK) throws PortalException {
072                    ExpandoRow row = expandoRowPersistence.findByT_C(tableId, classPK);
073    
074                    deleteRow(row);
075            }
076    
077            @Override
078            public void deleteRow(
079                            long companyId, long classNameId, String tableName, long classPK)
080                    throws PortalException {
081    
082                    ExpandoTable table = expandoTableLocalService.getTable(
083                            companyId, classNameId, tableName);
084    
085                    expandoRowLocalService.deleteRow(table.getTableId(), classPK);
086            }
087    
088            @Override
089            public void deleteRow(
090                            long companyId, String className, String tableName, long classPK)
091                    throws PortalException {
092    
093                    long classNameId = classNameLocalService.getClassNameId(className);
094    
095                    expandoRowLocalService.deleteRow(
096                            companyId, classNameId, tableName, classPK);
097            }
098    
099            @Override
100            public void deleteRows(long classPK) {
101                    List<ExpandoRow> rows = expandoRowPersistence.findByClassPK(classPK);
102    
103                    for (ExpandoRow row : rows) {
104                            deleteRow(row);
105                    }
106            }
107    
108            @Override
109            public ExpandoRow fetchRow(long tableId, long classPK) {
110                    return expandoRowPersistence.fetchByT_C(tableId, classPK);
111            }
112    
113            @Override
114            public List<ExpandoRow> getDefaultTableRows(
115                    long companyId, long classNameId, int start, int end) {
116    
117                    return expandoRowLocalService.getRows(
118                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME,
119                            start, end);
120            }
121    
122            @Override
123            public List<ExpandoRow> getDefaultTableRows(
124                    long companyId, String className, int start, int end) {
125    
126                    long classNameId = classNameLocalService.getClassNameId(className);
127    
128                    return expandoRowLocalService.getDefaultTableRows(
129                            companyId, classNameId, start, end);
130            }
131    
132            @Override
133            public int getDefaultTableRowsCount(long companyId, long classNameId) {
134                    return expandoRowLocalService.getRowsCount(
135                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
136            }
137    
138            @Override
139            public int getDefaultTableRowsCount(long companyId, String className) {
140                    long classNameId = classNameLocalService.getClassNameId(className);
141    
142                    return expandoRowLocalService.getDefaultTableRowsCount(
143                            companyId, classNameId);
144            }
145    
146            @Override
147            public ExpandoRow getRow(long rowId) throws PortalException {
148                    return expandoRowPersistence.findByPrimaryKey(rowId);
149            }
150    
151            @Override
152            public ExpandoRow getRow(long tableId, long classPK)
153                    throws PortalException {
154    
155                    return expandoRowPersistence.findByT_C(tableId, classPK);
156            }
157    
158            @Override
159            public ExpandoRow getRow(
160                    long companyId, long classNameId, String tableName, long classPK) {
161    
162                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
163                            companyId, classNameId, tableName);
164    
165                    if (table == null) {
166                            return null;
167                    }
168    
169                    return expandoRowPersistence.fetchByT_C(table.getTableId(), classPK);
170            }
171    
172            @Override
173            public ExpandoRow getRow(
174                    long companyId, String className, String tableName, long classPK) {
175    
176                    long classNameId = classNameLocalService.getClassNameId(className);
177    
178                    return expandoRowLocalService.getRow(
179                            companyId, classNameId, tableName, classPK);
180            }
181    
182            @Override
183            public List<ExpandoRow> getRows(long tableId, int start, int end) {
184                    return expandoRowPersistence.findByTableId(tableId, start, end);
185            }
186    
187            @Override
188            public List<ExpandoRow> getRows(
189                    long companyId, long classNameId, String tableName, int start,
190                    int end) {
191    
192                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
193                            companyId, classNameId, tableName);
194    
195                    if (table == null) {
196                            return Collections.emptyList();
197                    }
198    
199                    return expandoRowPersistence.findByTableId(
200                            table.getTableId(), start, end);
201            }
202    
203            @Override
204            public List<ExpandoRow> getRows(
205                    long companyId, String className, String tableName, int start,
206                    int end) {
207    
208                    long classNameId = classNameLocalService.getClassNameId(className);
209    
210                    return expandoRowLocalService.getRows(
211                            companyId, classNameId, tableName, start, end);
212            }
213    
214            @Override
215            public int getRowsCount(long tableId) {
216                    return expandoRowPersistence.countByTableId(tableId);
217            }
218    
219            @Override
220            public int getRowsCount(
221                    long companyId, long classNameId, String tableName) {
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            @Override
234            public int getRowsCount(
235                    long companyId, String className, String tableName) {
236    
237                    long classNameId = classNameLocalService.getClassNameId(className);
238    
239                    return expandoRowLocalService.getRowsCount(
240                            companyId, classNameId, tableName);
241            }
242    
243    }