KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > QualifyerController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.apache.xerces.parsers.DOMParser;
34 import org.exolab.castor.jdo.Database;
35 import org.infoglue.cms.entities.kernel.BaseEntityVO;
36 import org.infoglue.cms.entities.structure.Qualifyer;
37 import org.infoglue.cms.entities.structure.QualifyerVO;
38 import org.infoglue.cms.entities.structure.ServiceBinding;
39 import org.infoglue.cms.entities.structure.impl.simple.QualifyerImpl;
40 import org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl;
41 import org.infoglue.cms.exception.Bug;
42 import org.infoglue.cms.exception.ConstraintException;
43 import org.infoglue.cms.exception.SystemException;
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.Node JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47 import org.xml.sax.InputSource JavaDoc;
48
49 /**
50  * @author Mattias Bogeblad
51  */

52
53 public class QualifyerController extends BaseController
54 {
55     private final static Logger logger = Logger.getLogger(QualifyerController.class.getName());
56
57     public static Qualifyer getQualifyerWithId(Integer JavaDoc qualifyerId, Database db) throws SystemException, Bug
58     {
59         return (Qualifyer) getObjectWithId(QualifyerImpl.class, qualifyerId, db);
60     }
61
62
63     /**
64      * This method creates a new qualifyer for a serviceBinding. It is basically this qualifyer that
65      * specifies which stuff to fetch from the serviceDefinition.
66      */

67     /*
68     public static QualifyerVO create(String qualifyerXML, Integer serviceBindingId) throws ConstraintException, SystemException
69     {
70         Database db = CastorDatabaseService.getDatabase();
71         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
72
73         Qualifyer qualifyer = null;
74         
75         beginTransaction(db);
76         
77         ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingId, db);
78         
79         List qualifyerVOList = parseQualifyerList(qualifyerXML);
80         
81         Iterator i = qualifyerVOList.iterator();
82         while(i.hasNext())
83         {
84             QualifyerVO qualifyerVO = (QualifyerVO)i.next();
85             qualifyer = new QualifyerImpl();
86             qualifyer.setValueObject(qualifyerVO);
87             qualifyer.setServiceBinding((ServiceBindingImpl)serviceBinding);
88             
89             qualifyer = (Qualifyer) createEntity(qualifyer, db);
90         }
91                 
92         return qualifyer.getValueObject();
93     }
94 */

95
96
97     /**
98      * This method creates a new qualifyer for a serviceBinding. It is basically this qualifyer that
99      * specifies which stuff to fetch from the serviceDefinition.
100      */

101
102     public static Collection JavaDoc createQualifyers(String JavaDoc qualifyerXML, ServiceBinding serviceBinding) throws ConstraintException, SystemException
103     {
104         Collection JavaDoc qualifyers = new ArrayList JavaDoc();
105         
106         List JavaDoc qualifyerVOList = parseQualifyerList(qualifyerXML);
107         
108         Iterator JavaDoc i = qualifyerVOList.iterator();
109         while(i.hasNext())
110         {
111             QualifyerVO qualifyerVO = (QualifyerVO)i.next();
112             Qualifyer qualifyer = new QualifyerImpl();
113             qualifyer.setValueObject(qualifyerVO);
114             qualifyer.setServiceBinding((ServiceBindingImpl)serviceBinding);
115             qualifyers.add(qualifyer);
116             logger.info("ADDED:" + qualifyerVO.getValue());
117             //qualifyer = (Qualifyer) createEntity(qualifyer, db);
118
}
119                 
120         return qualifyers;
121     }
122
123
124     /**
125      * This method creates a new qualifyer for a serviceBinding. It is basically this qualifyer that
126      * specifies which stuff to fetch from the serviceDefinition.
127      */

128
129     public static QualifyerVO screate(QualifyerVO qualifyerVO, Integer JavaDoc serviceBindingId, Database db) throws ConstraintException, SystemException, Exception JavaDoc
130     {
131         Qualifyer qualifyer = null;
132         
133         ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingId, db);
134         
135         qualifyer = new QualifyerImpl();
136         qualifyer.setValueObject(qualifyerVO);
137         qualifyer.setServiceBinding((ServiceBindingImpl)serviceBinding);
138         db.create(qualifyer);
139                 
140         return qualifyer.getValueObject();
141     }
142
143
144       
145     /**
146      * This method returns a sorted list of qualifyers.
147      */

148     public static List JavaDoc getBindingQualifyers(Integer JavaDoc serviceBindingId) throws SystemException, Bug, Exception JavaDoc
149     {
150         List JavaDoc qualifyers = new ArrayList JavaDoc();
151         
152         Database db = CastorDatabaseService.getDatabase();
153         
154         beginTransaction(db);
155         
156         try
157         {
158             List JavaDoc unsortedQualifyers = ServiceBindingController.getQualifyerVOList(serviceBindingId);
159             
160             Iterator JavaDoc i = unsortedQualifyers.iterator();
161             while(i.hasNext())
162             {
163                 boolean isAdded = false;
164                 QualifyerVO qualifyerVO = (QualifyerVO)i.next();
165                 Iterator JavaDoc newListIterator = qualifyers.iterator();
166                 int index = 0;
167                 while(newListIterator.hasNext())
168                 {
169                     QualifyerVO sortedQualifyerVO = (QualifyerVO)newListIterator.next();
170                     if(sortedQualifyerVO.getSortOrder().intValue() < qualifyerVO.getSortOrder().intValue())
171                         logger.info("The old copy was before me... lets not do anything..");
172                     else
173                     {
174                         logger.info("The old copy was after me... lets insert the new one before it..");
175                         qualifyers.add(index, qualifyerVO);
176                         isAdded = true;
177                         break;
178                     }
179                 }
180                 
181                 if(!isAdded)
182                     qualifyers.add(qualifyerVO);
183             }
184             
185             commitTransaction(db);
186         }
187         catch(Exception JavaDoc e)
188         {
189             e.printStackTrace();
190             logger.error("An error occurred so we should not completes the transaction:" + e, e);
191             rollbackTransaction(db);
192             throw new SystemException(e.getMessage());
193         }
194         
195         return qualifyers;
196     }
197     
198     
199     private static List JavaDoc parseQualifyerList(String JavaDoc qualifyerXML)
200     {
201         List JavaDoc qualifyerVOList = new ArrayList JavaDoc();
202         
203         if(qualifyerXML != null)
204         {
205             try
206             {
207                 logger.info("qualifyerXML:" + qualifyerXML);
208                 InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(qualifyerXML));
209                 
210                 DOMParser parser = new DOMParser();
211                 parser.parse(inputSource);
212                 Document JavaDoc document = parser.getDocument();
213                 
214                 NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
215                 for(int i=0; i<nl.getLength(); i++)
216                 {
217                     Node JavaDoc n = nl.item(i);
218                     String JavaDoc name = n.getNodeName();
219                     String JavaDoc value = n.getFirstChild().getNodeValue();
220                     logger.info("name:" + name);
221                     logger.info("value:" + value);
222                     
223                     QualifyerVO qualifyerVO = new QualifyerVO();
224                     qualifyerVO.setName(name);
225                     qualifyerVO.setValue(value);
226                     qualifyerVO.setSortOrder(new Integer JavaDoc(i));
227                     
228                     qualifyerVOList.add(qualifyerVO);
229                 }
230             }
231             catch(Exception JavaDoc e)
232             {
233                 e.printStackTrace();
234             }
235         }
236         
237         return qualifyerVOList;
238     }
239
240     /**
241      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
242      * is handling.
243      */

244
245     public BaseEntityVO getNewVO()
246     {
247         return new QualifyerVO();
248     }
249
250 }
251
Popular Tags