001package de.deepamehta.plugins.mail;
002
003import static de.deepamehta.plugins.mail.MailPlugin.*;
004
005import java.util.Collection;
006import java.util.HashMap;
007import java.util.HashSet;
008import java.util.LinkedHashSet;
009import java.util.Map;
010import java.util.Set;
011import java.util.logging.Logger;
012
013import de.deepamehta.core.RelatedTopic;
014import de.deepamehta.core.Topic;
015import de.deepamehta.core.model.SimpleValue;
016import de.deepamehta.core.service.DeepaMehtaService;
017import de.deepamehta.core.service.ResultList;
018
019/**
020 * Reveals and caches mail configuration parts.
021 * 
022 * @TODO configure MTA user and password (requires a password data type)
023 */
024class MailConfigurationCache {
025
026    private static Logger log = Logger.getLogger(MailConfigurationCache.class.getName());
027
028    public static final String MAIL_CONFIG = "dm4.mail.config";
029
030    public static final String SMTP_HOST = "dm4.mail.config.host";
031
032    private Topic config = null;
033
034    private RecipientType defaultRecipientType = null;
035
036    private RelatedTopic defaultSender = null;
037
038    private boolean defaultSenderIsNull = false;
039
040    private final DeepaMehtaService dms;
041
042    private ResultList<RelatedTopic> recipientTypes;
043
044    private Set<String> recipientTypeUris;
045
046    private Map<String, Topic> searchParentTypes;
047
048    private ResultList<RelatedTopic> searchTypes;
049
050    private Set<String> searchTypeUris;
051
052    private String smtpHost = null;
053
054    public MailConfigurationCache(DeepaMehtaService dms) {
055        this.dms = dms;
056    }
057
058    /**
059     * Returns the corresponding enumeration value or the configured default
060     * recipient type.
061     * 
062     * @param type
063     *            Valid recipient type enumeration value.
064     * @return Recipient type.
065     */
066    public RecipientType checkRecipientType(String type) {
067        if (type == null || type.isEmpty()// type URI is unknown?
068                || getRecipientTypeUris().contains(type) == false) {
069            log.fine("use default recipient type");
070            return getDefaultRecipientType();
071        } else {
072            return RecipientType.fromUri(type);
073        }
074    }
075
076    private Topic getConfiguration() {
077        if (config == null) {
078            log.info("reveal mail plugin configuration");
079            config = dms.getTopic("uri", new SimpleValue(MAIL_CONFIG)).loadChildTopics();
080        }
081        return config;
082    }
083
084    public Topic getTopic() {
085        return getConfiguration();
086    }
087
088    public RecipientType getDefaultRecipientType() {
089        if (defaultRecipientType == null) {
090            log.info("reveal default recipient type");
091            Topic type = getConfiguration().getChildTopics().getTopic(RECIPIENT_TYPE);
092            defaultRecipientType = RecipientType.fromUri(type.getUri());
093        }
094        return defaultRecipientType;
095    }
096
097    public RelatedTopic getDefaultSender() {
098        if (defaultSenderIsNull == false && defaultSender == null) {
099            log.info("reveal default sender");
100            defaultSender = getConfiguration().getRelatedTopic(SENDER, PARENT, CHILD, null);
101            if (defaultSender == null) {
102                defaultSenderIsNull = true;
103            }
104        }
105        return defaultSender;
106    }
107
108    public ResultList<RelatedTopic> getRecipientTypes() {
109        if (recipientTypes == null) {
110            log.info("reveal recipient types");
111            recipientTypes = dms.getTopics(RECIPIENT_TYPE, 0);
112        }
113        return recipientTypes;
114    }
115
116    public Set<String> getRecipientTypeUris() {
117        if (recipientTypeUris == null) {
118            log.info("reveal recipient type URIs");
119            recipientTypeUris = new HashSet<String>();
120            for (Topic topic : getRecipientTypes()) {
121                recipientTypeUris.add(topic.getUri());
122            }
123        }
124        return recipientTypeUris;
125    }
126
127    public Collection<Topic> getSearchParentTypes() {
128        return revealSearchParentTypes().values();
129    }
130
131    public ResultList<RelatedTopic> getSearchTypes() {
132        if (searchTypes == null) {
133            log.info("reveal search types");
134            // get aggregated composite search types
135            // FIXME use a specific association type and field renderer
136            searchTypes = getConfiguration().getRelatedTopics(AGGREGATION, PARENT, CHILD, TOPIC_TYPE, 0);
137        }
138        return searchTypes;
139    }
140
141    public Set<String> getSearchTypeUris() {
142        if (searchTypeUris == null) {
143            log.info("reveal search type URIs");
144            searchTypeUris = new LinkedHashSet<String>();
145            for (Topic topic : getSearchTypes()) {
146                searchTypeUris.add(topic.getUri());
147            }
148        }
149        return searchTypeUris;
150    }
151
152    public String getSmtpHost() {
153        if (smtpHost == null) {
154            log.info("reveal smtp host");
155            smtpHost = getConfiguration().getChildTopics()//
156                    .getTopic(SMTP_HOST).getSimpleValue().toString();
157        }
158        return smtpHost;
159    }
160
161    private Map<String, Topic> revealSearchParentTypes() {
162        if (searchParentTypes == null) {
163            log.info("reveal search parent types");
164            searchParentTypes = new HashMap<String, Topic>();
165            for (Topic type : getSearchTypes()) {
166                searchParentTypes.put(type.getUri(), type.getRelatedTopic(null,//
167                        CHILD_TYPE, PARENT_TYPE, TOPIC_TYPE));
168            }
169        }
170        return searchParentTypes;
171    }
172
173}