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.portal.kernel.template;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
020    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
021    
022    import java.io.IOException;
023    import java.io.ObjectInput;
024    import java.io.ObjectOutput;
025    import java.io.Reader;
026    
027    import java.util.Date;
028    
029    /**
030     * @author Tina Tian
031     * @author Juan Fernández
032     */
033    public class DDMTemplateResource implements TemplateResource {
034    
035            /**
036             * The empty constructor is required by {@link java.io.Externalizable}. Do
037             * not use this for any other purpose.
038             */
039            public DDMTemplateResource() {
040            }
041    
042            public DDMTemplateResource(String ddmTemplateKey, DDMTemplate ddmTemplate) {
043                    if (Validator.isNull(ddmTemplateKey)) {
044                            throw new IllegalArgumentException("DDM Template Key is null");
045                    }
046    
047                    if (ddmTemplate == null) {
048                            throw new IllegalArgumentException("DDM template is null");
049                    }
050    
051                    _ddmTemplateKey = ddmTemplateKey;
052                    _ddmTemplate = ddmTemplate;
053            }
054    
055            @Override
056            public boolean equals(Object obj) {
057                    if (this == obj) {
058                            return true;
059                    }
060    
061                    if (!(obj instanceof DDMTemplateResource)) {
062                            return false;
063                    }
064    
065                    DDMTemplateResource ddmTemplateResource = (DDMTemplateResource)obj;
066    
067                    if (_ddmTemplateKey.equals(ddmTemplateResource._ddmTemplateKey) &&
068                            _ddmTemplate.equals(ddmTemplateResource._ddmTemplate)) {
069    
070                            return true;
071                    }
072    
073                    return false;
074            }
075    
076            public long getLastModified() {
077                    Date modifiedDate = _ddmTemplate.getModifiedDate();
078    
079                    return modifiedDate.getTime();
080            }
081    
082            public Reader getReader() {
083                    String script = _ddmTemplate.getScript();
084    
085                    return new UnsyncStringReader(script);
086            }
087    
088            public String getTemplateId() {
089                    return _ddmTemplateKey;
090            }
091    
092            @Override
093            public int hashCode() {
094                    return _ddmTemplateKey.hashCode() * 11 + _ddmTemplate.hashCode();
095            }
096    
097            public void readExternal(ObjectInput objectInput) throws IOException {
098                    long ddmTemplateId = objectInput.readLong();
099    
100                    try {
101                            _ddmTemplate = DDMTemplateLocalServiceUtil.getDDMTemplate(
102                                    ddmTemplateId);
103                    }
104                    catch (Exception e) {
105                            throw new IOException(
106                                    "Unable to retrieve ddm template with ID " + ddmTemplateId, e);
107                    }
108    
109                    _ddmTemplateKey = objectInput.readUTF();
110            }
111    
112            public void writeExternal(ObjectOutput objectOutput) throws IOException {
113                    objectOutput.writeLong(_ddmTemplate.getTemplateId());
114                    objectOutput.writeUTF(_ddmTemplateKey);
115            }
116    
117            private DDMTemplate _ddmTemplate;
118            private String _ddmTemplateKey;
119    
120    }