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.portlet.expando.exception.DuplicateTableNameException;
020    import com.liferay.portlet.expando.exception.TableNameException;
021    import com.liferay.portlet.expando.model.ExpandoTable;
022    import com.liferay.portlet.expando.model.ExpandoTableConstants;
023    import com.liferay.portlet.expando.service.base.ExpandoTableLocalServiceBaseImpl;
024    
025    import java.util.List;
026    
027    /**
028     * @author Raymond Aug??
029     * @author Brian Wing Shun Chan
030     */
031    public class ExpandoTableLocalServiceImpl
032            extends ExpandoTableLocalServiceBaseImpl {
033    
034            @Override
035            public ExpandoTable addDefaultTable(long companyId, long classNameId)
036                    throws PortalException {
037    
038                    return addTable(
039                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
040            }
041    
042            @Override
043            public ExpandoTable addDefaultTable(long companyId, String className)
044                    throws PortalException {
045    
046                    return addTable(
047                            companyId, className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
048            }
049    
050            @Override
051            public ExpandoTable addTable(long companyId, long classNameId, String name)
052                    throws PortalException {
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            @Override
070            public ExpandoTable addTable(long companyId, String className, String name)
071                    throws PortalException {
072    
073                    long classNameId = classNameLocalService.getClassNameId(className);
074    
075                    return addTable(companyId, classNameId, name);
076            }
077    
078            @Override
079            public void deleteTable(ExpandoTable table) {
080    
081                    // Table
082    
083                    expandoTablePersistence.remove(table);
084    
085                    // Columns
086    
087                    runSQL(
088                            "delete from ExpandoColumn where tableId = " + table.getTableId());
089    
090                    expandoColumnPersistence.clearCache();
091    
092                    // Rows
093    
094                    runSQL("delete from ExpandoRow where tableId = " + table.getTableId());
095    
096                    expandoRowPersistence.clearCache();
097    
098                    // Values
099    
100                    runSQL(
101                            "delete from ExpandoValue where tableId = " + table.getTableId());
102    
103                    expandoValuePersistence.clearCache();
104            }
105    
106            @Override
107            public void deleteTable(long tableId) throws PortalException {
108                    ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
109    
110                    deleteTable(table);
111            }
112    
113            @Override
114            public void deleteTable(long companyId, long classNameId, String name)
115                    throws PortalException {
116    
117                    ExpandoTable table = expandoTablePersistence.findByC_C_N(
118                            companyId, classNameId, name);
119    
120                    deleteTable(table);
121            }
122    
123            @Override
124            public void deleteTable(long companyId, String className, String name)
125                    throws PortalException {
126    
127                    long classNameId = classNameLocalService.getClassNameId(className);
128    
129                    deleteTable(companyId, classNameId, name);
130            }
131    
132            @Override
133            public void deleteTables(long companyId, long classNameId) {
134                    List<ExpandoTable> tables = expandoTablePersistence.findByC_C(
135                            companyId, classNameId);
136    
137                    for (ExpandoTable table : tables) {
138                            deleteTable(table);
139                    }
140            }
141    
142            @Override
143            public void deleteTables(long companyId, String className) {
144                    long classNameId = classNameLocalService.getClassNameId(className);
145    
146                    deleteTables(companyId, classNameId);
147            }
148    
149            @Override
150            public ExpandoTable fetchDefaultTable(long companyId, long classNameId) {
151                    return fetchTable(
152                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
153            }
154    
155            @Override
156            public ExpandoTable fetchDefaultTable(long companyId, String className) {
157                    long classNameId = classNameLocalService.getClassNameId(className);
158    
159                    return fetchTable(
160                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
161            }
162    
163            @Override
164            public ExpandoTable fetchTable(
165                    long companyId, long classNameId, String name) {
166    
167                    return expandoTablePersistence.fetchByC_C_N(
168                            companyId, classNameId, name);
169            }
170    
171            @Override
172            public ExpandoTable getDefaultTable(long companyId, long classNameId)
173                    throws PortalException {
174    
175                    return getTable(
176                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
177            }
178    
179            @Override
180            public ExpandoTable getDefaultTable(long companyId, String className)
181                    throws PortalException {
182    
183                    long classNameId = classNameLocalService.getClassNameId(className);
184    
185                    return getTable(
186                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
187            }
188    
189            @Override
190            public ExpandoTable getTable(long tableId) throws PortalException {
191                    return expandoTablePersistence.findByPrimaryKey(tableId);
192            }
193    
194            @Override
195            public ExpandoTable getTable(long companyId, long classNameId, String name)
196                    throws PortalException {
197    
198                    return expandoTablePersistence.findByC_C_N(
199                            companyId, classNameId, name);
200            }
201    
202            @Override
203            public ExpandoTable getTable(long companyId, String className, String name)
204                    throws PortalException {
205    
206                    long classNameId = classNameLocalService.getClassNameId(className);
207    
208                    return getTable(companyId, classNameId, name);
209            }
210    
211            @Override
212            public List<ExpandoTable> getTables(long companyId, long classNameId) {
213                    return expandoTablePersistence.findByC_C(companyId, classNameId);
214            }
215    
216            @Override
217            public List<ExpandoTable> getTables(long companyId, String className) {
218                    long classNameId = classNameLocalService.getClassNameId(className);
219    
220                    return getTables(companyId, classNameId);
221            }
222    
223            @Override
224            public ExpandoTable updateTable(long tableId, String name)
225                    throws PortalException {
226    
227                    ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
228    
229                    if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
230                            throw new TableNameException(
231                                    "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
232                    }
233    
234                    validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
235    
236                    table.setName(name);
237    
238                    return expandoTablePersistence.update(table);
239            }
240    
241            protected void validate(
242                            long companyId, long tableId, long classNameId, String name)
243                    throws PortalException {
244    
245                    if (Validator.isNull(name)) {
246                            throw new TableNameException("Name is null");
247                    }
248    
249                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
250                            companyId, classNameId, name);
251    
252                    if ((table != null) && (table.getTableId() != tableId)) {
253                            throw new DuplicateTableNameException("{tableId=" + tableId + "}");
254                    }
255            }
256    
257    }