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.util.test;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.test.util.RandomTestUtil;
019    import com.liferay.portal.kernel.test.util.TestPropsValues;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.expando.model.ExpandoColumn;
022    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
023    import com.liferay.portlet.expando.model.ExpandoTable;
024    import com.liferay.portlet.expando.model.ExpandoValue;
025    import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
026    import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
027    import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
028    
029    import java.util.Locale;
030    import java.util.Map;
031    
032    /**
033     * @author Manuel de la Pe??a
034     */
035    public class ExpandoTestUtil {
036    
037            public static ExpandoColumn addColumn(
038                            ExpandoTable table, String columnName, int type)
039                    throws Exception {
040    
041                    return ExpandoColumnLocalServiceUtil.addColumn(
042                            table.getTableId(), columnName, type);
043            }
044    
045            public static ExpandoColumn addColumn(
046                            ExpandoTable table, String columnName, int type,
047                            Map<Locale, String> defaultData)
048                    throws Exception {
049    
050                    return ExpandoColumnLocalServiceUtil.addColumn(
051                            table.getTableId(), columnName, type, defaultData);
052            }
053    
054            public static ExpandoTable addTable(long classNameId, String tableName)
055                    throws Exception {
056    
057                    return ExpandoTableLocalServiceUtil.addTable(
058                            TestPropsValues.getCompanyId(), classNameId, tableName);
059            }
060    
061            public static ExpandoValue addValue(
062                            ExpandoTable table, ExpandoColumn column, long classPK, Object data)
063                    throws Exception {
064    
065                    return ExpandoValueLocalServiceUtil.addValue(
066                            TestPropsValues.getCompanyId(),
067                            PortalUtil.getClassName(table.getClassNameId()), table.getName(),
068                            column.getName(), classPK, data);
069            }
070    
071            public static ExpandoValue addValue(
072                            ExpandoTable table, ExpandoColumn column, Map<Locale, String> data,
073                            Locale defaultLocale)
074                    throws Exception {
075    
076                    return ExpandoValueLocalServiceUtil.addValue(
077                            TestPropsValues.getCompanyId(),
078                            PortalUtil.getClassName(table.getClassNameId()), table.getName(),
079                            column.getName(), CounterLocalServiceUtil.increment(), data,
080                            defaultLocale);
081            }
082    
083            public static ExpandoValue addValue(
084                            ExpandoTable table, ExpandoColumn column, Object data)
085                    throws Exception {
086    
087                    return addValue(
088                            table, column, CounterLocalServiceUtil.increment(), data);
089            }
090    
091            public static ExpandoValue addValue(
092                            long classNameId, long classPK, Object data)
093                    throws Exception {
094    
095                    ExpandoTable table = addTable(
096                            classNameId, RandomTestUtil.randomString());
097                    ExpandoColumn column = addColumn(
098                            table, RandomTestUtil.randomString(),
099                            ExpandoColumnConstants.STRING);
100    
101                    return addValue(table, column, classPK, data);
102            }
103    
104    }