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