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