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.dynamicdatalists.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
019    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
020    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
021    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
023    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
024    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
025    
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class DDLRecordSetImpl extends DDLRecordSetBaseImpl {
032    
033            @Override
034            public DDMStructure getDDMStructure() throws PortalException {
035                    return DDMStructureLocalServiceUtil.getStructure(getDDMStructureId());
036            }
037    
038            @Override
039            public DDMStructure getDDMStructure(long formDDMTemplateId)
040                    throws PortalException {
041    
042                    DDMStructure ddmStructure = getDDMStructure();
043    
044                    if (formDDMTemplateId > 0) {
045                            try {
046                                    DDMTemplate ddmTemplate =
047                                            DDMTemplateLocalServiceUtil.getTemplate(formDDMTemplateId);
048    
049                                    // Clone ddmStructure to make sure changes are never persisted
050    
051                                    ddmStructure = (DDMStructure)ddmStructure.clone();
052    
053                                    ddmStructure.setDefinition(ddmTemplate.getScript());
054                            }
055                            catch (NoSuchTemplateException nste) {
056                            }
057                    }
058    
059                    return ddmStructure;
060            }
061    
062            @Override
063            public List<DDLRecord> getRecords() {
064                    return DDLRecordLocalServiceUtil.getRecords(getRecordSetId());
065            }
066    
067    }