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