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 List<ExpandoRow> getDefaultTableRows(
111                    long companyId, long classNameId, int start, int end) {
112    
113                    return expandoRowLocalService.getRows(
114                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME,
115                            start, end);
116            }
117    
118            @Override
119            public List<ExpandoRow> getDefaultTableRows(
120                    long companyId, String className, int start, int end) {
121    
122                    long classNameId = classNameLocalService.getClassNameId(className);
123    
124                    return expandoRowLocalService.getDefaultTableRows(
125                            companyId, classNameId, start, end);
126            }
127    
128            @Override
129            public int getDefaultTableRowsCount(long companyId, long classNameId) {
130                    return expandoRowLocalService.getRowsCount(
131                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
132            }
133    
134            @Override
135            public int getDefaultTableRowsCount(long companyId, String className) {
136                    long classNameId = classNameLocalService.getClassNameId(className);
137    
138                    return expandoRowLocalService.getDefaultTableRowsCount(
139                            companyId, classNameId);
140            }
141    
142            @Override
143            public ExpandoRow getRow(long rowId) throws PortalException {
144                    return expandoRowPersistence.findByPrimaryKey(rowId);
145            }
146    
147            @Override
148            public ExpandoRow getRow(long tableId, long classPK)
149                    throws PortalException {
150    
151                    return expandoRowPersistence.findByT_C(tableId, classPK);
152            }
153    
154            @Override
155            public ExpandoRow getRow(
156                    long companyId, long classNameId, String tableName, long classPK) {
157    
158                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
159                            companyId, classNameId, tableName);
160    
161                    if (table == null) {
162                            return null;
163                    }
164    
165                    return expandoRowPersistence.fetchByT_C(table.getTableId(), classPK);
166            }
167    
168            @Override
169            public ExpandoRow getRow(
170                    long companyId, String className, String tableName, long classPK) {
171    
172                    long classNameId = classNameLocalService.getClassNameId(className);
173    
174                    return expandoRowLocalService.getRow(
175                            companyId, classNameId, tableName, classPK);
176            }
177    
178            @Override
179            public List<ExpandoRow> getRows(long tableId, int start, int end) {
180                    return expandoRowPersistence.findByTableId(tableId, start, end);
181            }
182    
183            @Override
184            public List<ExpandoRow> getRows(
185                    long companyId, long classNameId, String tableName, int start,
186                    int end) {
187    
188                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
189                            companyId, classNameId, tableName);
190    
191                    if (table == null) {
192                            return Collections.emptyList();
193                    }
194    
195                    return expandoRowPersistence.findByTableId(
196                            table.getTableId(), start, end);
197            }
198    
199            @Override
200            public List<ExpandoRow> getRows(
201                    long companyId, String className, String tableName, int start,
202                    int end) {
203    
204                    long classNameId = classNameLocalService.getClassNameId(className);
205    
206                    return expandoRowLocalService.getRows(
207                            companyId, classNameId, tableName, start, end);
208            }
209    
210            /**
211             * @deprecated As of 6.1.0, replaced by {@link #getRows(long, String,
212             *             String, int, int)}
213             */
214            @Deprecated
215            @Override
216            public List<ExpandoRow> getRows(
217                    String className, String tableName, int start, int end) {
218    
219                    long companyId = CompanyThreadLocal.getCompanyId();
220    
221                    return expandoRowLocalService.getRows(
222                            companyId, className, tableName, start, end);
223            }
224    
225            @Override
226            public int getRowsCount(long tableId) {
227                    return expandoRowPersistence.countByTableId(tableId);
228            }
229    
230            @Override
231            public int getRowsCount(
232                    long companyId, long classNameId, String tableName) {
233    
234                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
235                            companyId, classNameId, tableName);
236    
237                    if (table == null) {
238                            return 0;
239                    }
240    
241                    return expandoRowPersistence.countByTableId(table.getTableId());
242            }
243    
244            @Override
245            public int getRowsCount(
246                    long companyId, String className, String tableName) {
247    
248                    long classNameId = classNameLocalService.getClassNameId(className);
249    
250                    return expandoRowLocalService.getRowsCount(
251                            companyId, classNameId, tableName);
252            }
253    
254            /**
255             * @deprecated As of 6.1.0, replaced by {@link #getRowsCount(long, String,
256             *             String)}
257             */
258            @Deprecated
259            @Override
260            public int getRowsCount(String className, String tableName) {
261                    long companyId = CompanyThreadLocal.getCompanyId();
262    
263                    return expandoRowLocalService.getRowsCount(
264                            companyId, className, tableName);
265            }
266    
267    }