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            @Override
077            public long getLastModified() {
078                    Date modifiedDate = _ddmTemplate.getModifiedDate();
079    
080                    return modifiedDate.getTime();
081            }
082    
083            @Override
084            public Reader getReader() {
085                    String script = _ddmTemplate.getScript();
086    
087                    return new UnsyncStringReader(script);
088            }
089    
090            @Override
091            public String getTemplateId() {
092                    return _ddmTemplateKey;
093            }
094    
095            @Override
096            public int hashCode() {
097                    return _ddmTemplateKey.hashCode() * 11 + _ddmTemplate.hashCode();
098            }
099    
100            @Override
101            public void readExternal(ObjectInput objectInput) throws IOException {
102                    long ddmTemplateId = objectInput.readLong();
103    
104                    try {
105                            _ddmTemplate = DDMTemplateLocalServiceUtil.getDDMTemplate(
106                                    ddmTemplateId);
107                    }
108                    catch (Exception e) {
109                            throw new IOException(
110                                    "Unable to retrieve ddm template with ID " + ddmTemplateId, e);
111                    }
112    
113                    _ddmTemplateKey = objectInput.readUTF();
114            }
115    
116            @Override
117            public void writeExternal(ObjectOutput objectOutput) throws IOException {
118                    objectOutput.writeLong(_ddmTemplate.getTemplateId());
119                    objectOutput.writeUTF(_ddmTemplateKey);
120            }
121    
122            private DDMTemplate _ddmTemplate;
123            private String _ddmTemplateKey;
124    
125    }