KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > service > core > impl > MailTemplateManagerImpl


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.service.core.impl;
17
18 import com.blandware.atleap.common.util.PartialCollection;
19 import com.blandware.atleap.common.util.QueryInfo;
20 import com.blandware.atleap.model.core.MailTemplate;
21 import com.blandware.atleap.persistence.core.MailTemplateDAO;
22 import com.blandware.atleap.service.core.MailTemplateManager;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28
29 /**
30  * <p>Implementation of MailTemplateManager</p>
31  * <p><a HREF="MailTemplateManagerImpl.java.htm"><i>View Source</i></a>
32  * </p>
33  * <p/>
34  *
35  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
36  * @version $Revision: 1.4 $ $Date: 2005/11/26 14:51:31 $
37  */

38 public class MailTemplateManagerImpl extends BaseManagerImpl implements MailTemplateManager {
39
40     /**
41      * Mail template DAO
42      */

43     protected MailTemplateDAO mailTemplateDAO;
44
45     /**
46      * List of mappings <code>identifier -&gt; list of available variables</code>
47      */

48     protected Map JavaDoc mailTemplates;
49
50     /**
51      * Creates new instance of MailTemplateManagerImpl
52      */

53     public MailTemplateManagerImpl() {
54     }
55
56     /**
57      * Sets DAO for operating with mail templates
58      *
59      * @param mailTemplateDAO the DAO to set
60      */

61     public void setMailTemplateDAO(MailTemplateDAO mailTemplateDAO) {
62         this.mailTemplateDAO = mailTemplateDAO;
63     }
64
65     /**
66      * Sets list of mappings from template identifiers to lists of available
67      * variables
68      *
69      * @param mailTemplates list to set
70      */

71     public void setMailTemplates(Map JavaDoc mailTemplates) {
72         this.mailTemplates = mailTemplates;
73     }
74
75     /**
76      * @see com.blandware.atleap.service.core.MailTemplateManager#initialize()
77      */

78     public void initialize() {
79         Set JavaDoc templateIdentifiers = mailTemplates.keySet();
80
81         // delete templates which identifiers are not presented in the list
82
List JavaDoc existentTemplates = mailTemplateDAO.listMailTemplates(null).asList();
83         for ( Iterator JavaDoc i = existentTemplates.iterator(); i.hasNext(); ) {
84             MailTemplate template = (MailTemplate) i.next();
85             if ( !templateIdentifiers.contains(template.getIdentifier()) ) {
86                 mailTemplateDAO.deleteMailTemplate(template);
87             }
88         }
89
90         // create unexistent templates
91
for ( Iterator JavaDoc i = templateIdentifiers.iterator(); i.hasNext(); ) {
92             String JavaDoc identifier = (String JavaDoc) i.next();
93             MailTemplate template = mailTemplateDAO.findMailTemplateByIdentifier(identifier);
94             if ( template == null ) {
95                 template = new MailTemplate();
96                 template.setIdentifier(identifier);
97                 mailTemplateDAO.createMailTemplate(template);
98             }
99         }
100
101         if (log.isInfoEnabled()) {
102             log.info("Mail templates initialized");
103         }
104     }
105
106     /**
107      * @see com.blandware.atleap.service.core.MailTemplateManager#retrieveMailTemplate(Long)
108      */

109     public MailTemplate retrieveMailTemplate(Long JavaDoc templateId) {
110         MailTemplate template = mailTemplateDAO.retrieveMailTemplate(templateId);
111         if ( template != null ) {
112             List JavaDoc availableVariables = (List JavaDoc) mailTemplates.get(template.getIdentifier());
113             if ( availableVariables != null ) {
114                 template.setAvailableVariables(availableVariables);
115             }
116         }
117         return template;
118     }
119
120     /**
121      * @see com.blandware.atleap.service.core.MailTemplateManager#updateMailTemplate(com.blandware.atleap.model.core.MailTemplate, java.util.Map)
122      */

123     public void updateMailTemplate(MailTemplate mailTemplate, Map JavaDoc linkedObjects) {
124         // remove mail template from cache in order to prevent Hibernate from assigning new version number
125
mailTemplateDAO.removeFromCache(mailTemplate);
126
127         if ( log.isDebugEnabled() ) {
128             log.debug("Updating mail template with ID '" + mailTemplate.getId() + "'...");
129         }
130
131         mailTemplateDAO.updateMailTemplate(mailTemplate, linkedObjects);
132
133         if ( log.isDebugEnabled() ) {
134             log.debug("Mail template has been updated successfully");
135         }
136     }
137
138     /**
139      * @see com.blandware.atleap.service.core.MailTemplateManager#listMailTemplates(com.blandware.atleap.common.util.QueryInfo)
140      */

141     public PartialCollection listMailTemplates(QueryInfo queryInfo) {
142         return mailTemplateDAO.listMailTemplates(queryInfo);
143     }
144
145     /**
146      * @see com.blandware.atleap.service.core.MailTemplateManager#findMailTemplateByIdentifier(String)
147      */

148     public MailTemplate findMailTemplateByIdentifier(String JavaDoc templateIdentifier) {
149         return mailTemplateDAO.findMailTemplateByIdentifier(templateIdentifier);
150     }
151 }
152
Popular Tags