001    /**
002     * Copyright (c) 2000-2010 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 getDefaultTable(long companyId, long classNameId)
171                    throws PortalException, SystemException {
172    
173                    return getTable(
174                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
175            }
176    
177            public ExpandoTable getDefaultTable(long companyId, String className)
178                    throws PortalException, SystemException {
179    
180                    long classNameId = PortalUtil.getClassNameId(className);
181    
182                    return getTable(
183                            companyId, classNameId, ExpandoTableConstants.DEFAULT_TABLE_NAME);
184            }
185    
186            public ExpandoTable getTable(long tableId)
187                    throws PortalException, SystemException {
188    
189                    return expandoTablePersistence.findByPrimaryKey(tableId);
190            }
191    
192            public ExpandoTable getTable(long companyId, long classNameId, String name)
193                    throws PortalException, SystemException {
194    
195                    return expandoTablePersistence.findByC_C_N(
196                            companyId, classNameId, name);
197            }
198    
199            /**
200             * @deprecated {@link #getTable(long, long, String)}
201             */
202            public ExpandoTable getTable(long classNameId, String name)
203                    throws PortalException, SystemException {
204    
205                    long companyId = CompanyThreadLocal.getCompanyId();
206    
207                    return getTable(companyId, classNameId, name);
208            }
209    
210            public ExpandoTable getTable(long companyId, String className, String name)
211                    throws PortalException, SystemException {
212    
213                    long classNameId = PortalUtil.getClassNameId(className);
214    
215                    return getTable(companyId, classNameId, name);
216            }
217    
218            /**
219             * @deprecated {@link #getTable(long, String, String)}
220             */
221            public ExpandoTable getTable(String className, String name)
222                    throws PortalException, SystemException {
223    
224                    long companyId = CompanyThreadLocal.getCompanyId();
225    
226                    return getTable(companyId, className, name);
227            }
228    
229            public List<ExpandoTable> getTables(long companyId, long classNameId)
230                    throws SystemException {
231    
232                    return expandoTablePersistence.findByC_C(companyId, classNameId);
233            }
234    
235            public List<ExpandoTable> getTables(long companyId, String className)
236                    throws SystemException {
237    
238                    long classNameId = PortalUtil.getClassNameId(className);
239    
240                    return getTables(companyId, classNameId);
241            }
242    
243            public ExpandoTable updateTable(long tableId, String name)
244                    throws PortalException, SystemException {
245    
246                    ExpandoTable table = expandoTablePersistence.findByPrimaryKey(tableId);
247    
248                    if (table.getName().equals(ExpandoTableConstants.DEFAULT_TABLE_NAME)) {
249                            throw new TableNameException(
250                                    "Cannot rename " + ExpandoTableConstants.DEFAULT_TABLE_NAME);
251                    }
252    
253                    validate(table.getCompanyId(), tableId, table.getClassNameId(), name);
254    
255                    table.setName(name);
256    
257                    return expandoTablePersistence.update(table, false);
258            }
259    
260            protected void validate(
261                            long companyId, long tableId, long classNameId, String name)
262                    throws PortalException, SystemException {
263    
264                    if (Validator.isNull(name)) {
265                            throw new TableNameException();
266                    }
267    
268                    ExpandoTable table = expandoTablePersistence.fetchByC_C_N(
269                            companyId, classNameId, name);
270    
271                    if ((table != null) && (table.getTableId() != tableId)) {
272                            throw new DuplicateTableNameException();
273                    }
274            }
275    
276    }