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.portal.template;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.template.TemplateException;
020    import com.liferay.portal.kernel.template.TemplateResource;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    
024    import java.io.Writer;
025    
026    import java.util.List;
027    import java.util.Map;
028    
029    /**
030     * @author Miroslav Ligas
031     */
032    public abstract class AbstractMultiResourceTemplate extends AbstractTemplate {
033    
034            public AbstractMultiResourceTemplate(
035                    List<TemplateResource> templateResources,
036                    TemplateResource errorTemplateResource, Map<String, Object> context,
037                    TemplateContextHelper templateContextHelper, String templateManagerName,
038                    long interval) {
039    
040                    super(
041                            errorTemplateResource, context, templateContextHelper,
042                            templateManagerName);
043    
044                    if (ListUtil.isEmpty(templateResources)) {
045                            throw new IllegalArgumentException("Template resource is null");
046                    }
047    
048                    this.templateResources = templateResources;
049            }
050    
051            @Override
052            public void doProcessTemplate(Writer writer) throws Exception {
053                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
054    
055                    put(TemplateConstants.WRITER, unsyncStringWriter);
056    
057                    processTemplates(templateResources, unsyncStringWriter);
058    
059                    StringBundler sb = unsyncStringWriter.getStringBundler();
060    
061                    sb.writeTo(writer);
062            }
063    
064            @Override
065            public void processTemplate(Writer writer) throws TemplateException {
066                    if (errorTemplateResource == null) {
067                            try {
068                                    processTemplates(templateResources, writer);
069    
070                                    return;
071                            }
072                            catch (Exception e) {
073                                    StringBuilder sb = new StringBuilder();
074    
075                                    for (TemplateResource templateResource : templateResources) {
076                                            sb.append(templateResource.getTemplateId());
077                                            sb.append(",");
078                                    }
079    
080                                    throw new TemplateException("Unable to process templates", e);
081                            }
082                    }
083    
084                    _write(writer);
085            }
086    
087            protected abstract void processTemplates(
088                            List<TemplateResource> templateResource, Writer writer)
089                    throws Exception;
090    
091            protected List<TemplateResource> templateResources;
092    
093    }