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.kernel.util.Validator;
019    import com.liferay.portal.security.auth.CompanyThreadLocal;
020    import com.liferay.portlet.expando.DuplicateTableNameException;
021    import com.liferay.portlet.expando.TableNameException;
022    import com.liferay.portlet.expando.model.ExpandoTable;
023    import com.liferay.portlet.expando.model.ExpandoTableConstants;
024    import com.liferay.portlet.expando.service.base.ExpandoTableLocalServiceBaseImpl;
025    
026    import java.util.List;
027    
028    /**
029     * @author Raymond Aug??
030     * @author Brian Wing Shun Chan
031     */
032    public class ExpandoTableLocalServiceImpl
033            extends ExpandoTableLocalServiceBaseImpl {
034    
035            @Override
036            public ExpandoTable addDefaultTable(long companyId, long classNameId)
037                    throws PortalException {
038    
039                    return addTable(
040                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
041            }
042    
043            @Override
044            public ExpandoTable addDefaultTable(long companyId, String className)
045                    throws PortalException {
046    
047                    return addTable(
048                            companyId, className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
049            }
050    
051            @Override
052            public ExpandoTable addTable(long companyId, long classNameId, String name)
053                    throws PortalException {
054    
055                    validate(companyId, 0, classNameId, name);
056    
057                    long tableId = counterLocalService.increment();
058    
059                    ExpandoTable table = expandoTablePersistence.create(tableId);
060    
061                    table.setCompanyId(companyId);
062                    table.setClassNameId(classNameId);
063                    table.setName(name);
064    
065                    expandoTablePersistence.update(table);
066    
067                    return table;
068            }
069    
070            /**
071             * @deprecated As of 6.1.0, replaced by {@link #addTable(long, long,
072             *             String)}
073             */
074            @Deprecated
075            @Override
076            public ExpandoTable addTable(long classNameId, String name)
077                    throws PortalException {
078    
079                    long companyId = CompanyThreadLocal.getCompanyId();
080    
081                    return addTable(companyId, classNameId, name);
082            }
083    
084            @Override
085            public ExpandoTable addTable(long companyId, String className, String name)
086                    throws PortalException {
087    
088                    long classNameId = classNameLocalService.getClassNameId(className);
089    
090                    return addTable(companyId, classNameId, name);
091            }
092    
093            /**
094             * @deprecated As of 6.1.0, replaced by {@link #addTable(long, String,
095             *             String)}
096             */
097            @Deprecated
098            @Override
099            public ExpandoTable addTable(String className, String name)
100                    throws PortalException {
101    
102                    long companyId = CompanyThreadLocal.getCompanyId();
103    
104                    return addTable(companyId, className, name);
105            }
106    
107            @Override
108            public void deleteTable(ExpandoTable table) {
109    
110                    // Table
111    
112                    expandoTablePersistence.remove(table);
113    
114                    // Columns
115    
116                    runSQL(
117                            "delete from ExpandoColumn where tableId = " + table.getTableId());
118    
119                    expandoColumnPersistence.clearCache();
120    
121                    // Rows
122    
123                    runSQL("delete from ExpandoRow where tableId = " + table.getTableId());
124    
125                    expandoRowPersistence.clearCache();
126    
127                    // Values
128    
129                    runSQL(
130                            "delete from ExpandoValue where tableId = " + table.getTableId());
131    
132                    expandoValuePersistence.clearCache();
133            }
134    
135            @Override
136            public void deleteTable(long tableId) throws PortalException {
137                    ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
138    
139                    deleteTable(table);
140            }
141    
142            @Override
143            public void deleteTable(long companyId, long classNameId, String name)
144                    throws PortalException {
145    
146                    ExpandoTable table = expandoTablePersistence.findByC_C_N(
147                            companyId, classNameId, name);
148    
149                    deleteTable(table);
150            }
151    
152            @Override
153            public void deleteTable(long companyId, String className, String name)
154                    throws PortalException {
155    
156                    long classNameId = classNameLocalService.getClassNameId(className);
157    
158                    deleteTable(companyId, classNameId, name);
159            }
160    
161            @Override
162            public void deleteTables(long companyId, long classNameId) {
163                    List<ExpandoTable> tables = expandoTablePersistence.findByC_C(
164                            companyId, classNameId);
165    
166                    for (ExpandoTable table : tables) {
167                            deleteTable(table);
168                    }
169            }
170    
171            @Override
172            public void deleteTables(long companyId, String className) {
173                    long classNameId = classNameLocalService.getClassNameId(className);
174    
175                    deleteTables(companyId, classNameId);
176            }
177    
178            @Override
179            public ExpandoTable fetchDefaultTable(long companyId, long classNameId) {
180                    return fetchTable(
181                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
182            }
183    
184            @Override
185            public ExpandoTable fetchDefaultTable(long companyId, String className) {
186                    long classNameId = classNameLocalService.getClassNameId(className);
187    
188                    return fetchTable(
189                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
190            }
191    
192            @Override
193            public ExpandoTable fetchTable(
194                    long companyId, long classNameId, String name) {
195    
196                    return expandoTablePersistence.fetchByC_C_N(
197                            companyId, classNameId, name);
198            }
199    
200            @Override
201            public ExpandoTable getDefaultTable(long companyId, long classNameId)
202                    throws PortalException {
203    
204                    return getTable(
205                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
206            }
207    
208            @Override
209            public ExpandoTable getDefaultTable(long companyId, String className)
210                    throws PortalException {
211    
212                    long classNameId = classNameLocalService.getClassNameId(className);
213    
214                    return getTable(
215                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
216            }
217    
218            @Override
219            public ExpandoTable getTable(long tableId) throws PortalException {
220                    return expandoTablePersistence.findByPrimaryKey(tableId);
221            }
222    
223            @Override
224            public ExpandoTable getTable(long companyId, long classNameId, String name)
225                    throws PortalException {
226    
227                    return expandoTablePersistence.findByC_C_N(
228                            companyId, classNameId, name);
229            }
230    
231            /**
232             * @deprecated As of 6.1.0, replaced by {@link #getTable(long, long,
233             *             String)}
234             */
235            @Deprecated
236            @Override
237            public ExpandoTable getTable(long classNameId, String name)
238                    throws PortalException {
239    
240                    long companyId = CompanyThreadLocal.getCompanyId();
241    
242                    return getTable(companyId, classNameId, name);
243            }
244    
245            @Override
246            public ExpandoTable getTable(long companyId, String className, String name)
247                    throws PortalException {
248    
249                    long classNameId = classNameLocalService.getClassNameId(className);
250    
251                    return getTable(companyId, classNameId, name);
252            }
253    
254            /**
255             * @deprecated As of 6.1.0, replaced by {@link #getTable(long, String,
256             *             String)}
257             */
258            @Deprecated
259            @Override
260            public ExpandoTable getTable(String className, String name)
261                    throws PortalException {
262    
263                    long companyId = CompanyThreadLocal.getCompanyId();
264    
265                    return getTable(companyId, className, name);
266            }
267    
268            @Override
269            public List<ExpandoTable> getTables(long companyId, long classNameId) {
270                    return expandoTablePersistence.findByC_C(companyId, classNameId);
271            }
272    
273            @Override
274            public List<ExpandoTable> getTables(long companyId, String className) {
275                    long classNameId = classNameLocalService.getClassNameId(className);
276    
277                    return getTables(companyId, classNameId);
278            }
279    
280            @Override
281            public ExpandoTable updateTable(long tableId, String name)
282                    throws PortalException {
283    
284                    ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
285    
286                    if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
287                            throw new TableNameException(
288                                    "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
289                    }
290    
291                    validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
292    
293                    table.setName(name);
294    
295                    return expandoTablePersistence.update(table);
296            }
297    
298            protected void validate(
299                            long companyId, long tableId, long classNameId, String name)
300                    throws PortalException {
301    
302                    if (Validator.isNull(name)) {
303                            throw new TableNameException("Name is null");
304                    }
305    
306                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
307                            companyId, classNameId, name);
308    
309                    if ((table != null) && (table.getTableId() != tableId)) {
310                            throw new DuplicateTableNameException("{tableId=" + tableId + "}");
311                    }
312            }
313    
314    }