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.dynamicdatamapping.util.test;
016    
017    import com.liferay.portal.kernel.locale.test.LocaleTestUtil;
018    import com.liferay.portal.model.Group;
019    import com.liferay.portal.util.PortalUtil;
020    import com.liferay.portal.util.test.ServiceContextTestUtil;
021    import com.liferay.portal.util.test.TestPropsValues;
022    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
023    import com.liferay.portlet.dynamicdatalists.util.test.DDLRecordTestUtil;
024    import com.liferay.portlet.dynamicdatamapping.io.DDMFormXSDDeserializerUtil;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
026    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
028    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
029    import com.liferay.portlet.dynamicdatamapping.storage.StorageType;
030    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
031    
032    /**
033     * @author Eduardo Garcia
034     * @author Andr?? de Oliveira
035     */
036    public class DDMStructureTestHelper {
037    
038            public DDMStructureTestHelper(Group group) throws Exception {
039                    _group = group;
040            }
041    
042            public DDMStructure addStructure(
043                            long parentStructureId, long classNameId, String structureKey,
044                            String name, String definition, String storageType, int type)
045                    throws Exception {
046    
047                    DDMXMLUtil.validateXML(definition);
048    
049                    DDMForm ddmForm = DDMFormXSDDeserializerUtil.deserialize(definition);
050    
051                    return DDMStructureLocalServiceUtil.addStructure(
052                            TestPropsValues.getUserId(), _group.getGroupId(), parentStructureId,
053                            classNameId, structureKey, LocaleTestUtil.getDefaultLocaleMap(name),
054                            null, ddmForm, storageType, type,
055                            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));
056            }
057    
058            public DDMStructure addStructure(
059                            long classNameId, String structureKey, String name,
060                            String definition, String storageType, int type)
061                    throws Exception {
062    
063                    return addStructure(
064                            DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, classNameId,
065                            structureKey, name, definition, storageType, type);
066            }
067    
068            public DDMStructure addStructure(String definition, String storageType)
069                    throws Exception {
070    
071                    return addStructure(
072                            PortalUtil.getClassNameId(DDLRecordSet.class), null,
073                            "Test Structure", definition, storageType,
074                            DDMStructureConstants.TYPE_DEFAULT);
075            }
076    
077            public DDMStructure addStructureXsd(Class<?> testClass) throws Exception {
078                    String definition = DDLRecordTestUtil.read(
079                            testClass, "test-structure.xsd");
080    
081                    return addStructure(definition, StorageType.XML.toString());
082            }
083    
084            private final Group _group;
085    
086    }