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