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.dynamicdatalists.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
020    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
021    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
024    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
025    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
026    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class DDLRecordSetImpl extends DDLRecordSetBaseImpl {
035    
036            public DDLRecordSetImpl() {
037            }
038    
039            @Override
040            public DDMStructure getDDMStructure()
041                    throws PortalException, SystemException {
042    
043                    return DDMStructureLocalServiceUtil.getStructure(getDDMStructureId());
044            }
045    
046            @Override
047            public DDMStructure getDDMStructure(long formDDMTemplateId)
048                    throws PortalException, SystemException {
049    
050                    DDMStructure ddmStructure = getDDMStructure();
051    
052                    if (formDDMTemplateId > 0) {
053                            try {
054                                    DDMTemplate ddmTemplate =
055                                            DDMTemplateLocalServiceUtil.getTemplate(formDDMTemplateId);
056    
057                                    // Clone ddmStructure to make sure changes are never persisted
058    
059                                    ddmStructure = (DDMStructure)ddmStructure.clone();
060    
061                                    ddmStructure.setXsd(ddmTemplate.getScript());
062                            }
063                            catch (NoSuchTemplateException nste) {
064                            }
065                    }
066    
067                    return ddmStructure;
068            }
069    
070            @Override
071            public List<DDLRecord> getRecords() throws SystemException {
072                    return DDLRecordLocalServiceUtil.getRecords(getRecordSetId());
073            }
074    
075            @Override
076            public List<Fields> getRecordsFieldsList()
077                    throws PortalException, SystemException {
078    
079                    List<Fields> fieldsList = new ArrayList<Fields>();
080    
081                    for (DDLRecord record : getRecords()) {
082                            fieldsList.add(record.getFields());
083                    }
084    
085                    return fieldsList;
086            }
087    
088    }