/*
 * Copyright 2012 s_wolff.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aegee.runanddine.advertisement;

import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.aegee.runanddine.util.model.Model;

/**
 * Model for persistent AdvertisementMailTemplates
 * @author s_wolff
 */
public class AdvertisementMailTemplate extends Model {

    /**
     * Instance of AdvertisementMailTemplate
     */
    private static AdvertisementMailTemplate TEMPLATE = null;
    
    /**
     * Mail subject
     */
    private String subject = "dummy subject";
    
    /**
     * Mail content
     */
    private String content = "dummy content";

    /**
     * Default constructor
     */
    public AdvertisementMailTemplate() {
    }

    /**
     * Constructor to set ID, subject and content
     * @param id Model ID
     * @param subject Mail subject
     * @param content Mail content
     */
    public AdvertisementMailTemplate(int id, String subject, String content) {
        this.id = id;
        this.subject = subject;
        this.content = content;
    }

    /**
     * Get mail content
     * @return Template content
     */
    public String getContent() {
        return content.replace("{{ ", "{{").replace(" }}", "}}");
    }

    /**
     * Get mail subject
     * @return Template subject
     */
    public String getSubject() {
        return subject.replace("{{ ", "{{").replace(" }}", "}}");
    }

    /**
     * Set mail content
     * @param content Template content
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * Set mail subject
     * @param subject Template subject
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * Get persistent AdvertisementMailTemplate if existing,
     * otherwise create new template
     * @return Existing or new AdvertisementMailTemplate
     */
    public static AdvertisementMailTemplate getTemplate() {
        if (TEMPLATE == null) {
            try {
                TEMPLATE = AdvertisementMailTemplateManager.getInstance().get();
            } catch (ModelNotExistingException e) {
                TEMPLATE = new AdvertisementMailTemplate();
            } finally {
                if (TEMPLATE == null) {
                    TEMPLATE = new AdvertisementMailTemplate();
                }
            }
        }
        return TEMPLATE;
    }

    /**
     * Store model persistently
     */
    public void saveTemplate() {
        AdvertisementMailTemplateManager.getInstance().save(TEMPLATE);
    }
}
