1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 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 ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 import org.xml.sax.InputSource ; 48 49 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 qualifyerId, Database db) throws SystemException, Bug 58 { 59 return (Qualifyer) getObjectWithId(QualifyerImpl.class, qualifyerId, db); 60 } 61 62 63 67 95 96 97 101 102 public static Collection createQualifyers(String qualifyerXML, ServiceBinding serviceBinding) throws ConstraintException, SystemException 103 { 104 Collection qualifyers = new ArrayList (); 105 106 List qualifyerVOList = parseQualifyerList(qualifyerXML); 107 108 Iterator 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 } 119 120 return qualifyers; 121 } 122 123 124 128 129 public static QualifyerVO screate(QualifyerVO qualifyerVO, Integer serviceBindingId, Database db) throws ConstraintException, SystemException, Exception 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 148 public static List getBindingQualifyers(Integer serviceBindingId) throws SystemException, Bug, Exception 149 { 150 List qualifyers = new ArrayList (); 151 152 Database db = CastorDatabaseService.getDatabase(); 153 154 beginTransaction(db); 155 156 try 157 { 158 List unsortedQualifyers = ServiceBindingController.getQualifyerVOList(serviceBindingId); 159 160 Iterator i = unsortedQualifyers.iterator(); 161 while(i.hasNext()) 162 { 163 boolean isAdded = false; 164 QualifyerVO qualifyerVO = (QualifyerVO)i.next(); 165 Iterator 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 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 parseQualifyerList(String qualifyerXML) 200 { 201 List qualifyerVOList = new ArrayList (); 202 203 if(qualifyerXML != null) 204 { 205 try 206 { 207 logger.info("qualifyerXML:" + qualifyerXML); 208 InputSource inputSource = new InputSource (new StringReader (qualifyerXML)); 209 210 DOMParser parser = new DOMParser(); 211 parser.parse(inputSource); 212 Document document = parser.getDocument(); 213 214 NodeList nl = document.getDocumentElement().getChildNodes(); 215 for(int i=0; i<nl.getLength(); i++) 216 { 217 Node n = nl.item(i); 218 String name = n.getNodeName(); 219 String 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 (i)); 227 228 qualifyerVOList.add(qualifyerVO); 229 } 230 } 231 catch(Exception e) 232 { 233 e.printStackTrace(); 234 } 235 } 236 237 return qualifyerVOList; 238 } 239 240 244 245 public BaseEntityVO getNewVO() 246 { 247 return new QualifyerVO(); 248 } 249 250 } 251 | Popular Tags |