001    /**
002     * Copyright (c) 2000-2012 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.journal.model.JournalTemplate;
020    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
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     */
032    public class JournalTemplateResource implements TemplateResource {
033    
034            /**
035             * The empty constructor is required by {@link java.io.Externalizable}. Do
036             * not use this for any other purpose.
037             */
038            public JournalTemplateResource() {
039            }
040    
041            public JournalTemplateResource(
042                    String templateId, JournalTemplate journalTemplate) {
043    
044                    if (Validator.isNull(templateId)) {
045                            throw new IllegalArgumentException("Template ID is null");
046                    }
047    
048                    if (journalTemplate == null) {
049                            throw new IllegalArgumentException("Journal template is null");
050                    }
051    
052                    _templateId = templateId;
053                    _journalTemplate = journalTemplate;
054            }
055    
056            @Override
057            public boolean equals(Object obj) {
058                    if (this == obj) {
059                            return true;
060                    }
061    
062                    if (!(obj instanceof JournalTemplateResource)) {
063                            return false;
064                    }
065    
066                    JournalTemplateResource journalTemplateResource =
067                            (JournalTemplateResource)obj;
068    
069                    if (_templateId.equals(journalTemplateResource._templateId) &&
070                            _journalTemplate.equals(journalTemplateResource._journalTemplate)) {
071    
072                            return true;
073                    }
074    
075                    return false;
076            }
077    
078            public long getLastModified() {
079                    Date modifiedDate = _journalTemplate.getModifiedDate();
080    
081                    return modifiedDate.getTime();
082            }
083    
084            public Reader getReader() {
085                    String xsl = _journalTemplate.getXsl();
086    
087                    return new UnsyncStringReader(xsl);
088            }
089    
090            public String getTemplateId() {
091                    return _templateId;
092            }
093    
094            @Override
095            public int hashCode() {
096                    return _templateId.hashCode() * 11 + _journalTemplate.hashCode();
097            }
098    
099            public void readExternal(ObjectInput objectInput) throws IOException {
100                    long journalTemplateId = objectInput.readLong();
101    
102                    try {
103                            _journalTemplate =
104                                    JournalTemplateLocalServiceUtil.getJournalTemplate(
105                                            journalTemplateId);
106                    }
107                    catch (Exception e) {
108                            throw new IOException(
109                                    "Unable to retrieve journal template with ID " +
110                                            journalTemplateId,
111                                    e);
112                    }
113    
114                    _templateId = objectInput.readUTF();
115            }
116    
117            public void writeExternal(ObjectOutput objectOutput) throws IOException {
118                    objectOutput.writeLong(_journalTemplate.getId());
119                    objectOutput.writeUTF(_templateId);
120            }
121    
122            private JournalTemplate _journalTemplate;
123            private String _templateId;
124    
125    }