KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > database > hibernate > service > ClipperManager


1 package org.jahia.clipbuilder.html.database.hibernate.service;
2
3 import org.jahia.clipbuilder.html.database.hibernate.dao.*;
4 import org.jahia.clipbuilder.html.database.hibernate.model.*;
5 import java.util.*;
6
7 import org.jdom.Document;
8 import org.jahia.clipbuilder.html.bean.ClipperBean;
9 import org.jahia.clipbuilder.html.bean.UrlBean;
10 import org.jahia.clipbuilder.html.bean.FormParamBean;
11 import org.jahia.clipbuilder.html.bean.FilterBean;
12 import org.jahia.clipbuilder.html.util.URLUtilities;
13 import org.jahia.clipbuilder.html.bean.QueryParamBean;
14 import org.jahia.clipbuilder.html.bean.ConfigureBean;
15
16 /**
17  * Service in order to manage clipper
18  *
19  *@author kTlili
20  */

21 public class ClipperManager {
22
23     private ClipperDAO clipperDAO;
24     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ClipperManager.class);
25
26
27     /**
28      * Sets the UserDAO attribute of the ClipperManager object
29      *
30      *@param clipperDAO The new ClipperDAO value
31      */

32     public void setClipperDAO(ClipperDAO clipperDAO) {
33         this.clipperDAO = clipperDAO;
34     }
35
36
37     /**
38      * Gets the Users attribute of the ClipperManager object
39      *
40      *@return The Users value
41      */

42     public List getClippers() {
43         return clipperDAO.getListClipper();
44     }
45
46
47     /**
48      * Gets the ClippersIdName attribute of the ClipperManager object
49      *
50      *@return The ClippersIdName value
51      */

52     public Map getClippersIdName() {
53         Map map = new HashMap();
54         List clipperList = this.getClippers();
55         for (int i = 0; i < clipperList.size(); i++) {
56             Clipper c = (Clipper) clipperList.get(i);
57             String JavaDoc name = c.getName();
58             Long JavaDoc id = c.getId();
59             map.put(name, id);
60         }
61         return map;
62     }
63
64
65
66     /**
67      * Gets the User attribute of the ClipperManager object
68      *
69      *@param clipperId Description of Parameter
70      *@return The User value
71      */

72     public Clipper getClipperById(String JavaDoc clipperId) {
73         Clipper clipper = clipperDAO.getClipper(Long.valueOf(clipperId));
74
75         if (clipper == null) {
76             //log.warn("UserId '" + userId + "' not found in database.");
77
}
78
79         return clipper;
80     }
81
82
83     /**
84      * Description of the Method
85      *
86      *@param id Description of Parameter
87      *@return Description of the Returned Value
88      */

89     public ClipperBean getClipperBean(String JavaDoc id) {
90         Clipper clipperModel = getClipperById(id);
91         ClipperBean clipperBean = new ClipperBean();
92         // load the xml document
93
clipperBean.setDescription(clipperModel.getDescription());
94         clipperBean.setName(clipperModel.getName());
95         //setTargetUrl(clipperModel.getTargetUrl());
96

97         // contain the sequence of all url
98
List urlModelList = clipperModel.getSequenceUrl();
99         int urlListSize = urlModelList.size();
100         for (int i = 0; i < urlListSize; i++) {
101             Url urlModel = (Url) urlModelList.get(i);
102             String JavaDoc baseURL = urlModel.getBase();
103             String JavaDoc relativeURL = urlModel.getValue();
104             String JavaDoc finalValue = urlModel.getFinalValue();
105             String JavaDoc from = urlModel.getFrom();
106             String JavaDoc hash = urlModel.getHash();
107
108             Map queryParamMap = new HashMap();
109
110             // build UrlBean
111
UrlBean uBean = new UrlBean(clipperBean, baseURL, relativeURL, queryParamMap, from, hash);
112             uBean.setRedirectUrl(URLUtilities.getURL(finalValue));
113
114             //add the form params
115
Set userParamSet = urlModel.getUserParams();
116             Iterator userParamIt = userParamSet.iterator();
117             while (userParamIt.hasNext()) {
118                 UserParam userParamModel = (UserParam) userParamIt.next();
119                 // add the parameter
120
String JavaDoc name = userParamModel.getName();
121                 String JavaDoc type = userParamModel.getType();
122                 String JavaDoc visibility = userParamModel.getVisibility();
123                 String JavaDoc update = userParamModel.getUpdate();
124                 int paramPosition = Integer.parseInt(userParamModel.getPosition());
125
126                 Set possibleValueSet = userParamModel.getPossibleValues();
127                 String JavaDoc formParentName = userParamModel.getFormParentName();
128                 String JavaDoc formParentId = userParamModel.getFormParentId();
129                 int formParentPosition = Integer.parseInt(userParamModel.getFormParentPosition());
130
131                 FormParamBean fBean = null;
132                 Iterator possibleValueIt = possibleValueSet.iterator();
133                 while (possibleValueIt.hasNext()) {
134                     PossibleValue possibleValueModel = (PossibleValue) possibleValueIt.next();
135                     String JavaDoc possibleValue = possibleValueModel.getValue();
136                     // add the parameter
137
fBean = uBean.addFormParameter(formParentName, formParentId, formParentPosition, name, possibleValue, type, visibility, paramPosition);
138                 }
139                 //set the mapping
140
String JavaDoc mapping = userParamModel.getMapping();
141                 fBean.setUpdate(update);
142                 uBean.setMappingFormParam(formParentName, formParentId, formParentPosition, name, mapping, paramPosition);
143
144             }
145
146             //add the queryParams
147
Set queryParamSet = urlModel.getQueryParams();
148             Iterator queryParamsIterator = queryParamSet.iterator();
149             while (queryParamsIterator.hasNext()) {
150                 // add the parameter
151
QueryParam queryParamModel = (QueryParam) queryParamsIterator.next();
152                 String JavaDoc name = queryParamModel.getName();
153                 String JavaDoc value = queryParamModel.getUsedValue();
154                 logger.debug("[ Query param whith name " + name + " and value " + value + " found ]");
155                 int qPosition = Integer.parseInt(queryParamModel.getPosition());
156                 QueryParamBean qBean = uBean.addQueryParameter(name, value, qPosition);
157                 if (value == null) {
158                     qBean.setUseAsDefaultValue("false");
159                 }
160                 else {
161                     qBean.setUseAsDefaultValue("true");
162                 }
163                 //add the qBean to the formBean
164
if (i > 0) {
165                     UrlBean previousUrlBean = clipperBean.getUrlBean(i - 1);
166                     FormParamBean fBean = previousUrlBean.getFormParamByNameAndFormParentHash(name, qPosition, hash);
167                     if (fBean != null) {
168                         fBean.setQueryParamBean(qBean);
169                     }
170                 }
171
172             }
173
174             // add the url
175
clipperBean.addUrlBean(uBean);
176         }
177
178         //set the filter
179
Filter filterModel = clipperModel.getFilter();
180         String JavaDoc typeFilter = filterModel.getType();
181         if (typeFilter == null) {
182             logger.error("[Filter type is null ]");
183         }
184         else {
185             logger.debug("[Filter type is " + typeFilter + " ]");
186             FilterBean filterBean = new FilterBean();
187             filterBean.setName(typeFilter);
188             if (filterBean == null) {
189                 logger.error("Unexpected type of filter");
190                 logger.warn("Filter not initialised");
191             }
192             else {
193                 //set the key of the filter
194
Iterator it = filterModel.getKeys().iterator();
195                 while (it.hasNext()) {
196                     FilterKey filterKeyModel = (FilterKey) it.next();
197                     String JavaDoc name = filterKeyModel.getName();
198                     String JavaDoc value = filterKeyModel.getValue();
199                     filterBean.addKeyPart(name, value);
200                 }
201
202                 // set the filter
203
clipperBean.setFilterBean(filterBean);
204             }
205         }
206
207         //set the configuration
208
ConfigureBean config = new ConfigureBean();
209         Configuration configurationModel = clipperModel.getConfiguration();
210         String JavaDoc proxy = configurationModel.getProxy();
211         String JavaDoc enableSSL = configurationModel.getEnableSsl();
212         String JavaDoc enableJavascript = configurationModel.getEnableJavascript();
213         String JavaDoc client = configurationModel.getClient();
214         String JavaDoc htmlDocument = configurationModel.getHtmldocument();
215         String JavaDoc enableCSS = configurationModel.getEnableCss();
216         String JavaDoc browserJavascriptEvent = configurationModel.getBrowserJavascriptEvent();
217         String JavaDoc browserJavascriptCode = configurationModel.getBrowserJavascriptCode();
218         String JavaDoc portletEnableSSL = configurationModel.getPortletSsl();
219         String JavaDoc portletContinualClipping = configurationModel.getPortletClipping();
220         String JavaDoc portetCacheExpiration = configurationModel.getPortletCacheExpiration();
221
222         config.setProxy(proxy);
223         config.setEnableCSS(enableCSS);
224         config.setEnableJavascript(enableJavascript);
225         config.setClient(client);
226         config.setHtmlDocument(htmlDocument);
227         config.setEnableSSL(enableSSL);
228         config.setBrowserJavascriptCode(browserJavascriptCode);
229         config.setBrowserJavascriptEvent(browserJavascriptEvent);
230         config.setPortletEnableSSL(portletEnableSSL);
231         config.setPortletContinualClipping(portletContinualClipping);
232         config.setPortletCacheExpiration(portetCacheExpiration);
233
234         clipperBean.setConfigurationBean(config);
235
236         return clipperBean;
237     }
238
239
240
241     /**
242      * Description of the Method
243      *
244      *@param clipper Description of Parameter
245      *@return Description of the Returned Value
246      */

247     public Clipper saveClipper(Clipper clipper) {
248         clipperDAO.saveClipper(clipper);
249         return clipper;
250     }
251
252
253     /**
254      * Description of the Method
255      *
256      *@param userId Description of Parameter
257      */

258     public void removeUser(String JavaDoc userId) {
259         clipperDAO.removeClipper(Long.valueOf(userId));
260     }
261
262 }
263
Popular Tags