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