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