KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: RegistryController.java,v 1.32 2006/10/29 08:38:32 mattias Exp $
24  */

25
26 package org.infoglue.cms.controllers.kernel.impl.simple;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.regex.Matcher JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36
37 import org.apache.log4j.Logger;
38 import org.exolab.castor.jdo.Database;
39 import org.exolab.castor.jdo.OQLQuery;
40 import org.exolab.castor.jdo.QueryResults;
41 import org.infoglue.cms.applications.databeans.ReferenceBean;
42 import org.infoglue.cms.applications.databeans.ReferenceVersionBean;
43 import org.infoglue.cms.entities.content.Content;
44 import org.infoglue.cms.entities.content.ContentVersion;
45 import org.infoglue.cms.entities.content.ContentVersionVO;
46 import org.infoglue.cms.entities.kernel.BaseEntityVO;
47 import org.infoglue.cms.entities.management.CategoryVO;
48 import org.infoglue.cms.entities.management.Language;
49 import org.infoglue.cms.entities.management.Registry;
50 import org.infoglue.cms.entities.management.RegistryVO;
51 import org.infoglue.cms.entities.management.impl.simple.RegistryImpl;
52 import org.infoglue.cms.entities.structure.Qualifyer;
53 import org.infoglue.cms.entities.structure.ServiceBinding;
54 import org.infoglue.cms.entities.structure.SiteNode;
55 import org.infoglue.cms.entities.structure.SiteNodeVersion;
56 import org.infoglue.cms.entities.structure.SiteNodeVersionVO;
57 import org.infoglue.cms.exception.ConstraintException;
58 import org.infoglue.cms.exception.SystemException;
59
60
61 /**
62  * The RegistryController manages the registry-parts of InfoGlue.
63  * The Registry is metadata for how things are related - especially to handle bindings and inline links etc
64  * when looking them up in the model is to slow.
65  *
66  * @author Mattias Bogeblad
67  */

68
69 public class RegistryController extends BaseController
70 {
71     private final static Logger logger = Logger.getLogger(RegistryController.class.getName());
72
73     private static final RegistryController instance = new RegistryController();
74     
75     public static RegistryController getController()
76     {
77         return instance;
78     }
79
80     private RegistryController()
81     {
82     }
83     
84     /**
85      * This method creates a registry entity in the db.
86      * @param valueObject
87      * @return
88      * @throws ConstraintException
89      * @throws SystemException
90      */

91     
92     public RegistryVO create(RegistryVO valueObject, Database db) throws ConstraintException, SystemException, Exception JavaDoc
93     {
94         Registry registry = new RegistryImpl();
95         registry.setValueObject(valueObject);
96         db.create(registry);
97         return registry.getValueObject();
98     }
99
100     /**
101      * This method updates a registry entry
102      * @param vo
103      * @return
104      * @throws ConstraintException
105      * @throws SystemException
106      */

107     
108     public RegistryVO update(RegistryVO valueObject, Database db) throws ConstraintException, SystemException
109     {
110         return (RegistryVO) updateEntity(RegistryImpl.class, (BaseEntityVO) valueObject, db);
111     }
112     
113     
114     /**
115      * This method deletes a registry entry
116      * @return registryId
117      * @throws ConstraintException
118      * @throws SystemException
119      */

120     
121     public void delete(Integer JavaDoc registryId) throws ConstraintException, SystemException
122     {
123         this.deleteEntity(RegistryImpl.class, registryId);
124     }
125     
126     /**
127      * this method goes through all inline stuff and all relations if ordinary content
128      * and all components and bindings if a metainfo.
129      *
130      * @param contentVersionVO
131      * @throws SystemException
132      * @throws Exception
133      */

134     
135     public void updateContentVersion(ContentVersionVO contentVersionVO) throws ConstraintException, SystemException
136     {
137         String JavaDoc versionValue = contentVersionVO.getVersionValue();
138         
139         Database db = CastorDatabaseService.getDatabase();
140         
141         try
142         {
143             beginTransaction(db);
144         
145             ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionVO.getContentVersionId(), db);
146             updateContentVersion(contentVersion, db);
147             
148             commitTransaction(db);
149         }
150         catch (Exception JavaDoc e)
151         {
152             rollbackTransaction(db);
153             throw new SystemException("An error occurred when we tried to fetch which sitenode uses a metainfo. Reason:" + e.getMessage(), e);
154         }
155     }
156
157     
158     
159     /**
160      * this method goes through all inline stuff and all relations if ordinary content
161      * and all components and bindings if a metainfo.
162      *
163      * @param contentVersionVO
164      * @throws SystemException
165      * @throws Exception
166      */

167     
168     public void updateContentVersion(ContentVersion contentVersion, Database db) throws ConstraintException, SystemException, Exception JavaDoc
169     {
170         String JavaDoc versionValue = contentVersion.getVersionValue();
171         
172         ContentVersion oldContentVersion = contentVersion; //ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionVO.getContentVersionId(), db);
173
Content oldContent = oldContentVersion.getOwningContent();
174         
175         if(oldContent.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info"))
176         {
177             logger.info("It was a meta info so lets check it for other stuff as well");
178             
179             SiteNodeVersion siteNodeVersion = getLatestActiveSiteNodeVersionWhichUsesContentVersionAsMetaInfo(oldContentVersion, db);
180             if(siteNodeVersion != null)
181             {
182                 logger.info("Going to use " + siteNodeVersion.getId() + " as reference");
183                 clearRegistryVOList(SiteNodeVersion.class.getName(), siteNodeVersion.getId().toString(), db);
184                 
185                 getComponents(siteNodeVersion, versionValue, db);
186                 getComponentBindings(siteNodeVersion, versionValue, db);
187                 getPageBindings(siteNodeVersion, db);
188             }
189             
190             getInlineSiteNodes(oldContentVersion, versionValue, db);
191             getInlineContents(oldContentVersion, versionValue, db);
192             getRelationSiteNodes(oldContentVersion, versionValue, db);
193             getRelationContents(oldContentVersion, versionValue, db);
194         }
195         else
196         {
197             clearRegistryVOList(ContentVersion.class.getName(), oldContentVersion.getContentVersionId().toString(), db);
198             getInlineSiteNodes(oldContentVersion, versionValue, db);
199             getInlineContents(oldContentVersion, versionValue, db);
200             getRelationSiteNodes(oldContentVersion, versionValue, db);
201             getRelationContents(oldContentVersion, versionValue, db);
202         }
203     }
204     
205     
206     /**
207      * this method goes through all inline stuff and all relations if ordinary content
208      * and all components and bindings if a metainfo.
209      *
210      * @param contentVersionVO
211      * @throws SystemException
212      * @throws Exception
213      */

214     
215     public void updateContentVersion(ContentVersion contentVersion, SiteNodeVersion siteNodeVersion, Database db) throws ConstraintException, SystemException, Exception JavaDoc
216     {
217         String JavaDoc versionValue = contentVersion.getVersionValue();
218         
219         ContentVersion oldContentVersion = contentVersion; //ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionVO.getContentVersionId(), db);
220
Content oldContent = oldContentVersion.getOwningContent();
221         
222         if(oldContent.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info"))
223         {
224             logger.info("It was a meta info so lets check it for other stuff as well");
225             
226             if(siteNodeVersion != null)
227             {
228                 logger.info("Going to use " + siteNodeVersion.getId() + " as reference");
229                 clearRegistryVOList(SiteNodeVersion.class.getName(), siteNodeVersion.getId().toString(), db);
230                 
231                 getComponents(siteNodeVersion, versionValue, db);
232                 getComponentBindings(siteNodeVersion, versionValue, db);
233                 getPageBindings(siteNodeVersion, db);
234             }
235             
236             getInlineSiteNodes(oldContentVersion, versionValue, db);
237             getInlineContents(oldContentVersion, versionValue, db);
238             getRelationSiteNodes(oldContentVersion, versionValue, db);
239             getRelationContents(oldContentVersion, versionValue, db);
240         }
241         else
242         {
243             clearRegistryVOList(ContentVersion.class.getName(), oldContentVersion.getContentVersionId().toString(), db);
244             if(siteNodeVersion != null)
245                 getPageBindings(siteNodeVersion, db);
246             getInlineSiteNodes(oldContentVersion, versionValue, db);
247             getInlineContents(oldContentVersion, versionValue, db);
248             getRelationSiteNodes(oldContentVersion, versionValue, db);
249             getRelationContents(oldContentVersion, versionValue, db);
250         }
251     }
252
253     /**
254      * this method goes through all page bindings and makes registry entries for them
255      *
256      * @param siteNodeVersion
257      * @throws SystemException
258      * @throws Exception
259      */

260     
261     public void updateSiteNodeVersion(SiteNodeVersionVO siteNodeVersionVO) throws ConstraintException, SystemException
262     {
263         Database db = CastorDatabaseService.getDatabase();
264         
265         try
266         {
267             beginTransaction(db);
268         
269             logger.info("Starting RegistryController.updateSiteNodeVersion...");
270             SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionVO.getId(), db);
271             logger.info("Before RegistryController.updateSiteNodeVersion...");
272             updateSiteNodeVersion(siteNodeVersion, db);
273             logger.info("Before commit RegistryController.updateSiteNodeVersion...");
274             
275             commitTransaction(db);
276         }
277         catch (Exception JavaDoc e)
278         {
279             rollbackTransaction(db);
280             throw new SystemException("An error occurred when we tried to fetch which sitenode uses a metainfo. Reason:" + e.getMessage(), e);
281         }
282     }
283
284     /**
285      * this method goes through all page bindings and makes registry entries for them
286      *
287      * @param siteNodeVersion
288      * @throws SystemException
289      * @throws Exception
290      */

291     
292     public void updateSiteNodeVersion(SiteNodeVersion siteNodeVersion, Database db) throws ConstraintException, SystemException, Exception JavaDoc
293     {
294         SiteNodeVersion oldSiteNodeVersion = siteNodeVersion;
295         SiteNode oldSiteNode = oldSiteNodeVersion.getOwningSiteNode();
296         
297         logger.info("Before clearing old registry...");
298         clearRegistryVOList(SiteNodeVersion.class.getName(), siteNodeVersion.getId().toString(), db);
299         logger.info("After clearing old registry...");
300         
301         Collection JavaDoc serviceBindings = siteNodeVersion.getServiceBindings();
302         Iterator JavaDoc serviceBindingIterator = serviceBindings.iterator();
303         while(serviceBindingIterator.hasNext())
304         {
305             ServiceBinding serviceBinding = (ServiceBinding)serviceBindingIterator.next();
306             if(serviceBinding.getBindingQualifyers() != null)
307             {
308                 Iterator JavaDoc qualifyersIterator = serviceBinding.getBindingQualifyers().iterator();
309                 while(qualifyersIterator.hasNext())
310                 {
311                     Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next();
312                     String JavaDoc name = qualifyer.getName();
313                     String JavaDoc value = qualifyer.getValue();
314     
315                     try
316                     {
317                         RegistryVO registryVO = new RegistryVO();
318                         registryVO.setReferenceType(RegistryVO.PAGE_BINDING);
319                         if(name.equalsIgnoreCase("contentId"))
320                         {
321                             Content content = ContentController.getContentController().getContentWithId(new Integer JavaDoc(value), db);
322                         
323                             registryVO.setEntityId(value);
324                             registryVO.setEntityName(Content.class.getName());
325                             
326                             registryVO.setReferencingEntityId(siteNodeVersion.getId().toString());
327                             registryVO.setReferencingEntityName(SiteNodeVersion.class.getName());
328                             registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString());
329                             registryVO.setReferencingEntityCompletingName(SiteNode.class.getName());
330                         
331                             Language masterLanguage = LanguageController.getController().getMasterLanguage(db, siteNodeVersion.getOwningSiteNode().getRepository().getId());
332                             ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(content.getContentId(), masterLanguage.getId(), db);
333                             getComponents(siteNodeVersion, contentVersion.getVersionValue(), db);
334                             getComponentBindings(siteNodeVersion, contentVersion.getVersionValue(), db);
335                         
336                             /*
337                             Collection contentVersions = content.getContentVersions();
338                             Iterator contentVersionIterator = contentVersions.iterator();
339                             while(contentVersionIterator.hasNext())
340                             {
341                                 ContentVersion contentVersion = (ContentVersion)contentVersionIterator.next();
342                                 System.out.println("contentVersion:" + contentVersion.getId());
343                                 getComponents(siteNodeVersion, contentVersion.getVersionValue(), db);
344                                 getComponentBindings(siteNodeVersion, contentVersion.getVersionValue(), db);
345                             }
346                             */

347                         }
348                         else if(name.equalsIgnoreCase("siteNodeId"))
349                         {
350                             SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(value), db);
351                             
352                             registryVO.setEntityId(value);
353                             registryVO.setEntityName(SiteNode.class.getName());
354                             
355                             registryVO.setReferencingEntityId(siteNodeVersion.getId().toString());
356                             registryVO.setReferencingEntityName(SiteNodeVersion.class.getName());
357                             registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString());
358                             registryVO.setReferencingEntityCompletingName(SiteNode.class.getName());
359                         }
360                         
361                         logger.info("Before creating registry entry...");
362                         
363                         this.create(registryVO, db);
364                     }
365                     catch(Exception JavaDoc e)
366                     {
367                         e.printStackTrace();
368                     }
369                 }
370             }
371         }
372     }
373
374     
375     /**
376      * this method goes through all page bindings and makes registry entries for them
377      *
378      * @param siteNodeVersion
379      * @throws SystemException
380      * @throws Exception
381      */

382     
383     public void getPageBindings(SiteNodeVersion siteNodeVersion, Database db) throws ConstraintException, SystemException, Exception JavaDoc
384     {
385         SiteNode oldSiteNode = siteNodeVersion.getOwningSiteNode();
386         
387         Collection JavaDoc serviceBindings = siteNodeVersion.getServiceBindings();
388         Iterator JavaDoc serviceBindingIterator = serviceBindings.iterator();
389         while(serviceBindingIterator.hasNext())
390         {
391             ServiceBinding serviceBinding = (ServiceBinding)serviceBindingIterator.next();
392             if(serviceBinding.getBindingQualifyers() != null)
393             {
394                 Iterator JavaDoc qualifyersIterator = serviceBinding.getBindingQualifyers().iterator();
395                 while(qualifyersIterator.hasNext())
396                 {
397                     Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next();
398                     String JavaDoc name = qualifyer.getName();
399                     String JavaDoc value = qualifyer.getValue();
400     
401                     try
402                     {
403                         RegistryVO registryVO = new RegistryVO();
404                         registryVO.setReferenceType(RegistryVO.PAGE_BINDING);
405                         if(name.equalsIgnoreCase("contentId"))
406                         {
407                             Content content = ContentController.getContentController().getContentWithId(new Integer JavaDoc(value), db);
408                         
409                             registryVO.setEntityId(value);
410                             registryVO.setEntityName(Content.class.getName());
411                             
412                             registryVO.setReferencingEntityId(siteNodeVersion.getId().toString());
413                             registryVO.setReferencingEntityName(SiteNodeVersion.class.getName());
414                             registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString());
415                             registryVO.setReferencingEntityCompletingName(SiteNode.class.getName());
416                         
417                             /*
418                             Collection contentVersions = content.getContentVersions();
419                             Iterator contentVersionIterator = contentVersions.iterator();
420                             while(contentVersionIterator.hasNext())
421                             {
422                                 ContentVersion contentVersion = (ContentVersion)contentVersionIterator.next();
423                                 getComponents(siteNodeVersion, contentVersion.getVersionValue(), db);
424                                 getComponentBindings(siteNodeVersion, contentVersion.getVersionValue(), db);
425                             }
426                             */

427                         }
428                         else if(name.equalsIgnoreCase("siteNodeId"))
429                         {
430                             SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(value), db);
431                             
432                             registryVO.setEntityId(value);
433                             registryVO.setEntityName(SiteNode.class.getName());
434                             
435                             registryVO.setReferencingEntityId(siteNodeVersion.getId().toString());
436                             registryVO.setReferencingEntityName(SiteNodeVersion.class.getName());
437                             registryVO.setReferencingEntityCompletingId(oldSiteNode.getId().toString());
438                             registryVO.setReferencingEntityCompletingName(SiteNode.class.getName());
439                         }
440                         
441                         logger.info("Before creating registry entry...");
442
443                         this.create(registryVO, db);
444                     }
445                     catch(Exception JavaDoc e)
446                     {
447                         e.printStackTrace();
448                     }
449                 }
450             }
451         }
452     }
453
454     /**
455      * This method fetches all inline links from any text.
456      */

457     
458     public void getInlineSiteNodes(ContentVersion contentVersion, String JavaDoc versionValue, Database db) throws ConstraintException, SystemException, Exception JavaDoc
459     {
460         Pattern JavaDoc pattern = Pattern.compile("\\$templateLogic\\.getPageUrl\\(.*?\\)");
461         Matcher JavaDoc matcher = pattern.matcher(versionValue);
462         while ( matcher.find() )
463         {
464             String JavaDoc match = matcher.group();
465             logger.info("Adding match to registry after some processing: " + match);
466             Integer JavaDoc siteNodeId;
467             
468             int siteNodeStartIndex = match.indexOf("(");
469             int siteNodeEndIndex = match.indexOf(",");
470             if(siteNodeStartIndex > 0 && siteNodeEndIndex > 0 && siteNodeEndIndex > siteNodeStartIndex)
471             {
472                 String JavaDoc siteNodeIdString = match.substring(siteNodeStartIndex + 1, siteNodeEndIndex);
473                 try
474                 {
475                     if(siteNodeIdString.indexOf("templateLogic.siteNodeId") == -1)
476                     {
477                         siteNodeId = new Integer JavaDoc(siteNodeIdString);
478                         logger.info("siteNodeId:" + siteNodeId);
479                         RegistryVO registryVO = new RegistryVO();
480                         registryVO.setEntityId(siteNodeId.toString());
481                         registryVO.setEntityName(SiteNode.class.getName());
482                         registryVO.setReferenceType(RegistryVO.INLINE_LINK);
483                         registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString());
484                         registryVO.setReferencingEntityName(ContentVersion.class.getName());
485                         registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString());
486                         registryVO.setReferencingEntityCompletingName(Content.class.getName());
487                         
488                         this.create(registryVO, db);
489                     }
490                 }
491                 catch(Exception JavaDoc e)
492                 {
493                     logger.warn("Tried to register inline sitenodes with exception as result:" + e.getMessage(), e);
494                 }
495             }
496         }
497     }
498     
499     /**
500      * This method fetches all inline links from any text.
501      */

502     
503     public void getInlineContents(ContentVersion contentVersion, String JavaDoc versionValue, Database db) throws ConstraintException, SystemException, Exception JavaDoc
504     {
505         Pattern JavaDoc pattern = Pattern.compile("\\$templateLogic\\.getInlineAssetUrl\\(.*?\\)");
506         Matcher JavaDoc matcher = pattern.matcher(versionValue);
507         while ( matcher.find() )
508         {
509             String JavaDoc match = matcher.group();
510             logger.info("Adding match to registry after some processing: " + match);
511             Integer JavaDoc contentId;
512             
513             int contentStartIndex = match.indexOf("(");
514             int contentEndIndex = match.indexOf(",");
515             if(contentStartIndex > 0 && contentEndIndex > 0 && contentEndIndex > contentStartIndex)
516             {
517                 contentId = new Integer JavaDoc(match.substring(contentStartIndex + 1, contentEndIndex));
518                 logger.info("contentId:" + contentId);
519                 
520                 RegistryVO registryVO = new RegistryVO();
521                 registryVO.setEntityId(contentId.toString());
522                 registryVO.setEntityName(Content.class.getName());
523                 registryVO.setReferenceType(RegistryVO.INLINE_ASSET);
524                 registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString());
525                 registryVO.setReferencingEntityName(ContentVersion.class.getName());
526                 registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString());
527                 registryVO.setReferencingEntityCompletingName(Content.class.getName());
528                 
529                 this.create(registryVO, db);
530             }
531         }
532     }
533     
534
535     /**
536      * This method fetches all inline links from any text.
537      */

538     
539     public void getRelationSiteNodes(ContentVersion contentVersion, String JavaDoc versionValue, Database db) throws ConstraintException, SystemException, Exception JavaDoc
540     {
541         Pattern JavaDoc pattern = Pattern.compile("<qualifyer entity='SiteNode'>.*?</qualifyer>");
542         Matcher JavaDoc matcher = pattern.matcher(versionValue);
543         while ( matcher.find() )
544         {
545             String JavaDoc match = matcher.group();
546             logger.info("Adding match to registry after some processing: " + match);
547             Integer JavaDoc siteNodeId;
548             
549             int siteNodeStartIndex = match.indexOf("<id>");
550             int siteNodeEndIndex = match.indexOf("</id>");
551             while(siteNodeStartIndex > 0 && siteNodeEndIndex > 0 && siteNodeEndIndex > siteNodeStartIndex)
552             {
553                 siteNodeId = new Integer JavaDoc(match.substring(siteNodeStartIndex + 4, siteNodeEndIndex));
554                 logger.info("siteNodeId:" + siteNodeId);
555                 RegistryVO registryVO = new RegistryVO();
556                 registryVO.setEntityId(siteNodeId.toString());
557                 registryVO.setEntityName(SiteNode.class.getName());
558                 registryVO.setReferenceType(RegistryVO.INLINE_SITE_NODE_RELATION);
559                 registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString());
560                 registryVO.setReferencingEntityName(ContentVersion.class.getName());
561                 registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString());
562                 registryVO.setReferencingEntityCompletingName(Content.class.getName());
563                 
564                 this.create(registryVO, db);
565                 
566                 siteNodeStartIndex = match.indexOf("<id>", siteNodeEndIndex);
567                 siteNodeEndIndex = match.indexOf("</id>", siteNodeStartIndex);
568             }
569         }
570     }
571     
572     /**
573      * This method fetches all inline links from any text.
574      */

575     
576     public void getRelationContents(ContentVersion contentVersion, String JavaDoc versionValue, Database db) throws ConstraintException, SystemException, Exception JavaDoc
577     {
578         Pattern JavaDoc pattern = Pattern.compile("<qualifyer entity='Content'>.*?</qualifyer>");
579         Matcher JavaDoc matcher = pattern.matcher(versionValue);
580         while ( matcher.find() )
581         {
582             String JavaDoc match = matcher.group();
583             logger.info("Adding match to registry after some processing: " + match);
584             Integer JavaDoc contentId;
585             
586             int contentStartIndex = match.indexOf("<id>");
587             int contentEndIndex = match.indexOf("</id>");
588             while(contentStartIndex > 0 && contentEndIndex > 0 && contentEndIndex > contentStartIndex)
589             {
590                 contentId = new Integer JavaDoc(match.substring(contentStartIndex + 4, contentEndIndex));
591                 logger.info("contentId:" + contentId);
592                 
593                 RegistryVO registryVO = new RegistryVO();
594                 registryVO.setEntityId(contentId.toString());
595                 registryVO.setEntityName(Content.class.getName());
596                 registryVO.setReferenceType(RegistryVO.INLINE_CONTENT_RELATION);
597                 registryVO.setReferencingEntityId(contentVersion.getContentVersionId().toString());
598                 registryVO.setReferencingEntityName(ContentVersion.class.getName());
599                 registryVO.setReferencingEntityCompletingId(contentVersion.getOwningContent().getContentId().toString());
600                 registryVO.setReferencingEntityCompletingName(Content.class.getName());
601                 
602                 this.create(registryVO, db);
603                 
604                 contentStartIndex = match.indexOf("<id>", contentEndIndex);
605                 contentEndIndex = match.indexOf("</id>", contentStartIndex);
606             }
607         }
608     }
609                     
610     
611     /**
612      * This method fetches all components and adds entries to the registry.
613      */

614     
615     public void getComponents(SiteNodeVersion siteNodeVersion, String JavaDoc versionValue, Database db) throws ConstraintException, SystemException, Exception JavaDoc
616     {
617         List JavaDoc foundComponents = new ArrayList JavaDoc();
618         
619         Pattern JavaDoc pattern = Pattern.compile("contentId=\".*?\"");
620         Matcher JavaDoc matcher = pattern.matcher(versionValue);
621         while ( matcher.find() )
622         {
623             String JavaDoc match = matcher.group();
624             logger.info("Adding match to registry after some processing: " + match);
625             Integer JavaDoc contentId;
626             
627             int contentStartIndex = match.indexOf("\"");
628             int contentEndIndex = match.lastIndexOf("\"");
629             if(contentStartIndex > 0 && contentEndIndex > 0 && contentEndIndex > contentStartIndex)
630             {
631                 contentId = new Integer JavaDoc(match.substring(contentStartIndex + 1, contentEndIndex));
632                 logger.info("contentId:" + contentId);
633                 
634                 if(!foundComponents.contains(contentId))
635                 {
636                     RegistryVO registryVO = new RegistryVO();
637                     registryVO.setEntityId(contentId.toString());
638                     registryVO.setEntityName(Content.class.getName());
639                     registryVO.setReferenceType(RegistryVO.PAGE_COMPONENT);
640                     registryVO.setReferencingEntityId(siteNodeVersion.getSiteNodeVersionId().toString());
641                     registryVO.setReferencingEntityName(SiteNodeVersion.class.getName());
642                     registryVO.setReferencingEntityCompletingId(siteNodeVersion.getOwningSiteNode().getSiteNodeId().toString());
643                     registryVO.setReferencingEntityCompletingName(SiteNode.class.getName());
644                     
645                     this.create(registryVO, db);
646                     
647                     foundComponents.add(contentId);
648                 }
649             }
650         }
651     }
652
653     /**
654      * This method fetches all components and adds entries to the registry.
655      */

656
657     public void getComponentBindings(SiteNodeVersion siteNodeVersion, String JavaDoc versionValue, Database db) throws ConstraintException, SystemException, Exception JavaDoc
658     {
659         List JavaDoc foundComponents = new ArrayList JavaDoc();
660
661         Pattern JavaDoc pattern = Pattern.compile("<binding entity=\".*?\" entityId=\".*?\">");
662         Matcher JavaDoc matcher = pattern.matcher(versionValue);
663         while ( matcher.find() )
664         {
665             String JavaDoc match = matcher.group();
666             logger.info("Adding match to registry after some processing: " + match);
667             String JavaDoc entityName;
668             String JavaDoc entityId;
669             
670             int entityNameStartIndex = match.indexOf("\"");
671             int entityNameEndIndex = match.indexOf("\"", entityNameStartIndex + 1);
672             logger.info("entityNameStartIndex:" + entityNameStartIndex);
673             logger.info("entityNameEndIndex:" + entityNameEndIndex);
674             if(entityNameStartIndex > 0 && entityNameEndIndex > 0 && entityNameEndIndex > entityNameStartIndex)
675             {
676                 entityName = match.substring(entityNameStartIndex + 1, entityNameEndIndex);
677                 logger.info("entityName:" + entityName);
678
679                 int entityIdStartIndex = match.indexOf("\"", entityNameEndIndex + 1);
680                 int entityIdEndIndex = match.indexOf("\"", entityIdStartIndex + 1);
681                 logger.info("entityIdStartIndex:" + entityIdStartIndex);
682                 logger.info("entityIdEndIndex:" + entityIdEndIndex);
683                 if(entityIdStartIndex > 0 && entityIdEndIndex > 0 && entityIdEndIndex > entityIdStartIndex)
684                 {
685                     entityId = match.substring(entityIdStartIndex + 1, entityIdEndIndex);
686                     logger.info("entityId:" + entityId);
687
688                     String JavaDoc key = entityName + ":" + entityId;
689                     if(!foundComponents.contains(key))
690                     {
691                         RegistryVO registryVO = new RegistryVO();
692                         if(entityName.indexOf("Content") > -1)
693                             registryVO.setEntityName(Content.class.getName());
694                         else
695                             registryVO.setEntityName(SiteNode.class.getName());
696                             
697                         registryVO.setEntityId(entityId);
698                         registryVO.setReferenceType(RegistryVO.PAGE_COMPONENT_BINDING);
699                         registryVO.setReferencingEntityId(siteNodeVersion.getSiteNodeVersionId().toString());
700                         registryVO.setReferencingEntityName(SiteNodeVersion.class.getName());
701                         registryVO.setReferencingEntityCompletingId(siteNodeVersion.getOwningSiteNode().getSiteNodeId().toString());
702                         registryVO.setReferencingEntityCompletingName(SiteNode.class.getName());
703                         
704                         this.create(registryVO, db);
705
706                         foundComponents.add(key);
707                     }
708                 }
709             }
710         }
711     }
712
713     /**
714      * Implemented for BaseController
715      */

716     public BaseEntityVO getNewVO()
717     {
718         return new CategoryVO();
719     }
720
721     /**
722      * This method gets all referencing content versions
723      *
724      * @param contentId
725      * @return
726      */

727     /*
728     public List getReferencingObjectsForContent(Integer contentId) throws SystemException
729     {
730         List referenceBeanList = new ArrayList();
731         
732         Database db = CastorDatabaseService.getDatabase();
733         
734         try
735         {
736             beginTransaction(db);
737             
738             Map entries = new HashMap();
739             
740             List registryEntires = getMatchingRegistryVOList(Content.class.getName(), contentId.toString(), db);
741             Iterator registryEntiresIterator = registryEntires.iterator();
742             while(registryEntiresIterator.hasNext())
743             {
744                 RegistryVO registryVO = (RegistryVO)registryEntiresIterator.next();
745                 logger.info("registryVO:" + registryVO.getReferencingEntityId() + ":" + registryVO.getReferencingEntityCompletingId());
746                 
747                 ReferenceBean referenceBean = new ReferenceBean();
748                                 
749                 if(registryVO.getReferencingEntityName().indexOf("Content") > -1)
750                 {
751                     ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer(registryVO.getReferencingEntityId()), db);
752                     logger.info("contentVersion:" + contentVersion.getContentVersionId());
753                     referenceBean.setName(contentVersion.getOwningContent().getName());
754                     referenceBean.setReferencingObject(contentVersion.getValueObject());
755                 }
756                 else
757                 {
758                     SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(new Integer(registryVO.getReferencingEntityId()), db);
759                     logger.info("siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId());
760                     referenceBean.setName(siteNodeVersion.getOwningSiteNode().getName());
761                     referenceBean.setReferencingObject(siteNodeVersion.getValueObject());
762                 }
763                 
764                 String key = "" + registryVO.getReferencingEntityName() + "_" + registryVO.getReferencingEntityId();
765                 ReferenceBean existingReferenceBean = (ReferenceBean)entries.get(key);
766                 if(existingReferenceBean == null)
767                 {
768                     List registryVOList = new ArrayList();
769                     registryVOList.add(registryVO);
770                     referenceBean.setRegistryVOList(registryVOList);
771                     logger.info("Adding referenceBean to entries with key:" + key);
772                     entries.put(key, referenceBean);
773                     referenceBeanList.add(referenceBean);
774                 }
775                 else
776                 {
777                     logger.info("Found referenceBean in entries with key:" + key);
778                     existingReferenceBean.getRegistryVOList().add(registryVO);
779                 }
780             }
781
782             commitTransaction(db);
783         }
784         catch ( Exception e)
785         {
786             logger.warn("An error occurred so we should not complete the transaction:" + e);
787             rollbackTransaction(db);
788         }
789
790         return referenceBeanList;
791     }
792     */

793     
794     public List JavaDoc getReferencingObjectsForContent(Integer JavaDoc contentId) throws SystemException
795     {
796         
797         List JavaDoc referenceBeanList = new ArrayList JavaDoc();
798         
799         Database db = CastorDatabaseService.getDatabase();
800         
801         try
802         {
803             beginTransaction(db);
804             
805             referenceBeanList = getReferencingObjectsForContent(contentId, db);
806             
807             /*
808             Map entries = new HashMap();
809             
810             List registryEntires = getMatchingRegistryVOList(Content.class.getName(), contentId.toString(), db);
811             logger.info("registryEntires:" + registryEntires.size());
812             Iterator registryEntiresIterator = registryEntires.iterator();
813             while(registryEntiresIterator.hasNext())
814             {
815                 RegistryVO registryVO = (RegistryVO)registryEntiresIterator.next();
816                 logger.info("registryVO:" + registryVO.getReferencingEntityId() + ":" + registryVO.getReferencingEntityCompletingId());
817                 boolean add = true;
818                 
819                 String key = "" + registryVO.getReferencingEntityCompletingName() + "_" + registryVO.getReferencingEntityCompletingId();
820                 //String key = "" + registryVO.getReferencingEntityName() + "_" + registryVO.getReferencingEntityId();
821                 ReferenceBean existingReferenceBean = (ReferenceBean)entries.get(key);
822                 if(existingReferenceBean == null)
823                 {
824                     
825                     existingReferenceBean = new ReferenceBean();
826                     logger.info("Adding referenceBean to entries with key:" + key);
827                     entries.put(key, existingReferenceBean);
828                     referenceBeanList.add(existingReferenceBean);
829                 }
830
831                 ReferenceVersionBean referenceVersionBean = new ReferenceVersionBean();
832                 if(registryVO.getReferencingEntityName().indexOf("Content") > -1)
833                 {
834                     try
835                     {
836                         ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer(registryVO.getReferencingEntityId()), db);
837                         logger.info("contentVersion:" + contentVersion.getContentVersionId());
838                         existingReferenceBean.setName(contentVersion.getOwningContent().getName());
839                         existingReferenceBean.setReferencingCompletingObject(contentVersion.getOwningContent().getValueObject());
840                         
841                         referenceVersionBean.setReferencingObject(contentVersion.getValueObject());
842                         referenceVersionBean.getRegistryVOList().add(registryVO);
843                     }
844                     catch(Exception e)
845                     {
846                         add = false;
847                         logger.info("content:" + registryVO.getReferencingEntityId() + " did not exist - skipping..");
848                     }
849                 }
850                 else
851                 {
852                     try
853                     {
854                         SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(new Integer(registryVO.getReferencingEntityId()), db);
855                         logger.info("siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId());
856                         logger.info("siteNode:" + siteNodeVersion.getOwningSiteNode().getId());
857                         existingReferenceBean.setName(siteNodeVersion.getOwningSiteNode().getName());
858                         existingReferenceBean.setReferencingCompletingObject(siteNodeVersion.getOwningSiteNode().getValueObject());
859     
860                         referenceVersionBean.setReferencingObject(siteNodeVersion.getValueObject());
861                         referenceVersionBean.getRegistryVOList().add(registryVO);
862                     }
863                     catch(Exception e)
864                     {
865                         add = false;
866                         logger.info("siteNode:" + registryVO.getReferencingEntityId() + " did not exist - skipping..");
867                     }
868                 }
869                 
870                 if(add)
871                 {
872                     boolean exists = false;
873                     ReferenceVersionBean existingReferenceVersionBean = null;
874                     Iterator versionsIterator = existingReferenceBean.getVersions().iterator();
875                     while(versionsIterator.hasNext())
876                     {
877                         existingReferenceVersionBean = (ReferenceVersionBean)versionsIterator.next();
878                         if(existingReferenceVersionBean.getReferencingObject().equals(referenceVersionBean.getReferencingObject()))
879                         {
880                             exists = true;
881                             break;
882                         }
883                     }
884
885                     if(!exists)
886                         existingReferenceBean.getVersions().add(referenceVersionBean);
887                     else
888                         existingReferenceVersionBean.getRegistryVOList().add(registryVO);
889
890                 }
891                 
892             }
893             
894             Iterator i = referenceBeanList.iterator();
895             while(i.hasNext())
896             {
897                 ReferenceBean referenceBean = (ReferenceBean)i.next();
898                 if(referenceBean.getVersions().size() == 0)
899                     i.remove();
900             }*/

901         
902             commitTransaction(db);
903         }
904         catch (Exception JavaDoc e)
905         {
906             e.printStackTrace();
907             logger.warn("One of the references was not found which is bad but not critical:" + e.getMessage(), e);
908             rollbackTransaction(db);
909             //throw new SystemException("An error occurred when we tried to fetch a list of roles in the repository. Reason:" + e.getMessage(), e);
910
}
911         
912         logger.info("referenceBeanList:" + referenceBeanList.size());
913         
914         return referenceBeanList;
915     }
916
917     public List JavaDoc getReferencingObjectsForContent(Integer JavaDoc contentId, Database db) throws SystemException, Exception JavaDoc
918     {
919         
920         List JavaDoc referenceBeanList = new ArrayList JavaDoc();
921
922         Map JavaDoc entries = new HashMap JavaDoc();
923         
924         List JavaDoc registryEntires = getMatchingRegistryVOList(Content.class.getName(), contentId.toString(), db);
925         logger.info("registryEntires:" + registryEntires.size());
926         Iterator JavaDoc registryEntiresIterator = registryEntires.iterator();
927         while(registryEntiresIterator.hasNext())
928         {
929             RegistryVO registryVO = (RegistryVO)registryEntiresIterator.next();
930             logger.info("registryVO:" + registryVO.getReferencingEntityId() + ":" + registryVO.getReferencingEntityCompletingId());
931             boolean add = true;
932             
933             String JavaDoc key = "" + registryVO.getReferencingEntityCompletingName() + "_" + registryVO.getReferencingEntityCompletingId();
934             //String key = "" + registryVO.getReferencingEntityName() + "_" + registryVO.getReferencingEntityId();
935
ReferenceBean existingReferenceBean = (ReferenceBean)entries.get(key);
936             if(existingReferenceBean == null)
937             {
938                 
939                 existingReferenceBean = new ReferenceBean();
940                 logger.info("Adding referenceBean to entries with key:" + key);
941                 entries.put(key, existingReferenceBean);
942                 referenceBeanList.add(existingReferenceBean);
943             }
944
945             ReferenceVersionBean referenceVersionBean = new ReferenceVersionBean();
946             if(registryVO.getReferencingEntityName().indexOf("Content") > -1)
947             {
948                 try
949                 {
950                     ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer JavaDoc(registryVO.getReferencingEntityId()), db);
951                     logger.info("contentVersion:" + contentVersion.getContentVersionId());
952                     existingReferenceBean.setName(contentVersion.getOwningContent().getName());
953                     existingReferenceBean.setReferencingCompletingObject(contentVersion.getOwningContent().getValueObject());
954                     
955                     referenceVersionBean.setReferencingObject(contentVersion.getValueObject());
956                     referenceVersionBean.getRegistryVOList().add(registryVO);
957                 }
958                 catch(Exception JavaDoc e)
959                 {
960                     add = false;
961                     logger.info("content:" + registryVO.getReferencingEntityId() + " did not exist - skipping..");
962                 }
963             }
964             else
965             {
966                 try
967                 {
968                     SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(new Integer JavaDoc(registryVO.getReferencingEntityId()), db);
969                     logger.info("siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId());
970                     logger.info("siteNode:" + siteNodeVersion.getOwningSiteNode().getId());
971                     existingReferenceBean.setName(siteNodeVersion.getOwningSiteNode().getName());
972                     existingReferenceBean.setReferencingCompletingObject(siteNodeVersion.getOwningSiteNode().getValueObject());
973
974                     referenceVersionBean.setReferencingObject(siteNodeVersion.getValueObject());
975                     referenceVersionBean.getRegistryVOList().add(registryVO);
976                 }
977                 catch(Exception JavaDoc e)
978                 {
979                     add = false;
980                     logger.info("siteNode:" + registryVO.getReferencingEntityId() + " did not exist - skipping..");
981                 }
982             }
983             
984             if(add)
985             {
986                 boolean exists = false;
987                 ReferenceVersionBean existingReferenceVersionBean = null;
988                 Iterator JavaDoc versionsIterator = existingReferenceBean.getVersions().iterator();
989                 while(versionsIterator.hasNext())
990                 {
991                     existingReferenceVersionBean = (ReferenceVersionBean)versionsIterator.next();
992                     if(existingReferenceVersionBean.getReferencingObject().equals(referenceVersionBean.getReferencingObject()))
993                     {
994                         exists = true;
995                         break;
996                     }
997                 }
998
999                 if(!exists)
1000                    existingReferenceBean.getVersions().add(referenceVersionBean);
1001                else
1002                    existingReferenceVersionBean.getRegistryVOList().add(registryVO);
1003
1004            }
1005            
1006        }
1007        
1008        Iterator JavaDoc i = referenceBeanList.iterator();
1009        while(i.hasNext())
1010        {
1011            ReferenceBean referenceBean = (ReferenceBean)i.next();
1012            if(referenceBean.getVersions().size() == 0)
1013                i.remove();
1014        }
1015        
1016        logger.info("referenceBeanList:" + referenceBeanList.size());
1017        
1018        return referenceBeanList;
1019    }
1020    
1021    
1022    /**
1023     * This method gets all referencing sitenode versions
1024     *
1025     * @param siteNodeId
1026     * @return
1027     */

1028    /*
1029    public List getReferencingObjectsForSiteNode(Integer siteNodeId) throws SystemException, Exception
1030    {
1031        List referenceBeanList = new ArrayList();
1032        
1033        Database db = CastorDatabaseService.getDatabase();
1034        
1035        try
1036        {
1037            beginTransaction(db);
1038            
1039            Map entries = new HashMap();
1040            
1041            List registryEntires = getMatchingRegistryVOList(SiteNode.class.getName(), siteNodeId.toString(), db);
1042            Iterator registryEntiresIterator = registryEntires.iterator();
1043            while(registryEntiresIterator.hasNext())
1044            {
1045                RegistryVO registryVO = (RegistryVO)registryEntiresIterator.next();
1046                logger.info("registryVO:" + registryVO.getReferencingEntityId() + ":" + registryVO.getReferencingEntityCompletingId());
1047                
1048                ReferenceBean referenceBean = new ReferenceBean();
1049               
1050                if(registryVO.getReferencingEntityName().indexOf("Content") > -1)
1051                {
1052                    ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer(registryVO.getReferencingEntityId()), db);
1053                    logger.info("contentVersion:" + contentVersion.getContentVersionId());
1054                    referenceBean.setName(contentVersion.getOwningContent().getName());
1055                    referenceBean.setReferencingObject(contentVersion.getValueObject());
1056                }
1057                else
1058                {
1059                    SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(new Integer(registryVO.getReferencingEntityId()), db);
1060                    logger.info("siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId());
1061                    referenceBean.setName(siteNodeVersion.getOwningSiteNode().getName());
1062                    referenceBean.setReferencingObject(siteNodeVersion.getValueObject());
1063                }
1064                
1065                String key = "" + registryVO.getReferencingEntityName() + "_" + registryVO.getReferencingEntityId();
1066                //String key = "" + registryVO.getReferencingEntityCompletingName() + "_" + registryVO.getReferencingEntityCompletingId();
1067                ReferenceBean existingReferenceBean = (ReferenceBean)entries.get(key);
1068                if(existingReferenceBean == null)
1069                {
1070                    List registryVOList = new ArrayList();
1071                    registryVOList.add(registryVO);
1072                    referenceBean.setRegistryVOList(registryVOList);
1073                    logger.info("Adding referenceBean to entries with key:" + key);
1074                    entries.put(key, referenceBean);
1075                    referenceBeanList.add(referenceBean);
1076                }
1077                else
1078                {
1079                    logger.info("Found referenceBean in entries with key:" + key);
1080                    existingReferenceBean.getRegistryVOList().add(registryVO);
1081                }
1082            }
1083            
1084            commitTransaction(db);
1085        }
1086        catch (Exception e)
1087        {
1088            logger.warn("An error occurred so we should not complete the transaction:" + e);
1089            rollbackTransaction(db);
1090            //throw new SystemException("An error occurred when we tried to fetch a list of roles in the repository. Reason:" + e.getMessage(), e);
1091        }
1092        
1093        return referenceBeanList;
1094    }
1095    */

1096
1097    public List JavaDoc getReferencingObjectsForSiteNode(Integer JavaDoc siteNodeId) throws SystemException, Exception JavaDoc
1098    {
1099        List JavaDoc referenceBeanList = new ArrayList JavaDoc();
1100        
1101        Database db = CastorDatabaseService.getDatabase();
1102        
1103        try
1104        {
1105            beginTransaction(db);
1106
1107            referenceBeanList = getReferencingObjectsForSiteNode(siteNodeId, db);
1108
1109            commitTransaction(db);
1110        }
1111        catch (Exception JavaDoc e)
1112        {
1113            e.printStackTrace();
1114            logger.warn("One of the references was not found which is bad but not critical:" + e.getMessage(), e);
1115            rollbackTransaction(db);
1116        }
1117        
1118        return referenceBeanList;
1119    }
1120
1121            
1122    public List JavaDoc getReferencingObjectsForSiteNode(Integer JavaDoc siteNodeId, Database db) throws SystemException, Exception JavaDoc
1123    {
1124        List JavaDoc referenceBeanList = new ArrayList JavaDoc();
1125        
1126        Map JavaDoc entries = new HashMap JavaDoc();
1127        
1128        List JavaDoc registryEntires = getMatchingRegistryVOList(SiteNode.class.getName(), siteNodeId.toString(), db);
1129        Iterator JavaDoc registryEntiresIterator = registryEntires.iterator();
1130        while(registryEntiresIterator.hasNext())
1131        {
1132            RegistryVO registryVO = (RegistryVO)registryEntiresIterator.next();
1133            logger.info("registryVO:" + registryVO.getReferencingEntityId() + ":" + registryVO.getReferencingEntityCompletingId());
1134            boolean add = true;
1135
1136            String JavaDoc key = "" + registryVO.getReferencingEntityCompletingName() + "_" + registryVO.getReferencingEntityCompletingId();
1137            //String key = "" + registryVO.getReferencingEntityName() + "_" + registryVO.getReferencingEntityId();
1138
ReferenceBean existingReferenceBean = (ReferenceBean)entries.get(key);
1139            if(existingReferenceBean == null)
1140            {
1141                existingReferenceBean = new ReferenceBean();
1142                logger.info("Adding referenceBean to entries with key:" + key);
1143                entries.put(key, existingReferenceBean);
1144                referenceBeanList.add(existingReferenceBean);
1145            }
1146
1147            ReferenceVersionBean referenceVersionBean = new ReferenceVersionBean();
1148            
1149            if(registryVO.getReferencingEntityName().indexOf("Content") > -1)
1150            {
1151                try
1152                {
1153                    ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(new Integer JavaDoc(registryVO.getReferencingEntityId()), db);
1154                    logger.info("contentVersion:" + contentVersion.getContentVersionId());
1155                    existingReferenceBean.setName(contentVersion.getOwningContent().getName());
1156                    existingReferenceBean.setReferencingCompletingObject(contentVersion.getOwningContent().getValueObject());
1157                    
1158                    referenceVersionBean.setReferencingObject(contentVersion.getValueObject());
1159                    referenceVersionBean.getRegistryVOList().add(registryVO);
1160                }
1161                catch(Exception JavaDoc e)
1162                {
1163                    add = false;
1164                    logger.info("content:" + registryVO.getReferencingEntityId() + " did not exist - skipping..");
1165                }
1166            }
1167            else
1168            {
1169                try
1170                {
1171                    SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(new Integer JavaDoc(registryVO.getReferencingEntityId()), db);
1172                    logger.info("siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId());
1173                    logger.info("siteNode:" + siteNodeVersion.getOwningSiteNode().getId());
1174                    existingReferenceBean.setName(siteNodeVersion.getOwningSiteNode().getName());
1175                    existingReferenceBean.setReferencingCompletingObject(siteNodeVersion.getOwningSiteNode().getValueObject());
1176
1177                    referenceVersionBean.setReferencingObject(siteNodeVersion.getValueObject());
1178                    referenceVersionBean.getRegistryVOList().add(registryVO);
1179                }
1180                catch(Exception JavaDoc e)
1181                {
1182                    add = false;
1183                    logger.info("siteNode:" + registryVO.getReferencingEntityId() + " did not exist - skipping..");
1184                }
1185            }
1186               
1187            if(add)
1188            {
1189                boolean exists = false;
1190                ReferenceVersionBean existingReferenceVersionBean = null;
1191                Iterator JavaDoc versionsIterator = existingReferenceBean.getVersions().iterator();
1192                while(versionsIterator.hasNext())
1193                {
1194                    existingReferenceVersionBean = (ReferenceVersionBean)versionsIterator.next();
1195                    if(existingReferenceVersionBean.getReferencingObject().equals(referenceVersionBean.getReferencingObject()))
1196                    {
1197                        exists = true;
1198                        break;
1199                    }
1200                }
1201                
1202                if(!exists)
1203                    existingReferenceBean.getVersions().add(referenceVersionBean);
1204                else
1205                    existingReferenceVersionBean.getRegistryVOList().add(registryVO);
1206            }
1207        }
1208        
1209        Iterator JavaDoc i = referenceBeanList.iterator();
1210        while(i.hasNext())
1211        {
1212            ReferenceBean referenceBean = (ReferenceBean)i.next();
1213            if(referenceBean.getVersions().size() == 0)
1214                i.remove();
1215        }
1216        
1217        return referenceBeanList;
1218    }
1219
1220    /**
1221     * Gets matching references
1222     */

1223    
1224    public List JavaDoc getMatchingRegistryVOList(String JavaDoc entityName, String JavaDoc entityId, Database db) throws SystemException, Exception JavaDoc
1225    {
1226        List JavaDoc matchingRegistryVOList = new ArrayList JavaDoc();
1227        
1228        OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.entityName = $1 AND r.entityId = $2 ORDER BY r.registryId");
1229        oql.bind(entityName);
1230        oql.bind(entityId);
1231        
1232        QueryResults results = oql.execute(Database.ReadOnly);
1233        
1234        while (results.hasMore())
1235        {
1236            Registry registry = (Registry)results.next();
1237            RegistryVO registryVO = registry.getValueObject();
1238            
1239            matchingRegistryVOList.add(registryVO);
1240        }
1241        
1242        results.close();
1243        oql.close();
1244
1245        return matchingRegistryVOList;
1246    }
1247    
1248    
1249    public List JavaDoc getReferencedObjects(String JavaDoc referencingEntityName, String JavaDoc referencingEntityId) throws SystemException, Exception JavaDoc
1250    {
1251        List JavaDoc result = new ArrayList JavaDoc();
1252        
1253        Database db = CastorDatabaseService.getDatabase();
1254        
1255        try
1256        {
1257            beginTransaction(db);
1258        
1259            List JavaDoc registryVOList = getMatchingRegistryVOListForReferencingEntity(referencingEntityName, referencingEntityId, db);
1260            
1261            Iterator JavaDoc i = registryVOList.iterator();
1262            while(i.hasNext())
1263            {
1264                RegistryVO registryVO = (RegistryVO)i.next();
1265                if(registryVO.getEntityName().indexOf("Content") > -1)
1266                {
1267                    try
1268                    {
1269                        Content content = ContentController.getContentController().getContentWithId(new Integer JavaDoc(registryVO.getEntityId()), db);
1270                        logger.info("contentVersion:" + content.getContentId());
1271                        result.add(content.getValueObject());
1272                    }
1273                    catch(Exception JavaDoc e)
1274                    {
1275                        logger.info("content:" + registryVO.getEntityId() + " did not exist - skipping..");
1276                    }
1277                }
1278                else if(registryVO.getEntityName().indexOf("SiteNode") > -1)
1279                {
1280                    try
1281                    {
1282                        SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(registryVO.getEntityId()), db);
1283                        logger.info("siteNode:" + siteNode.getId());
1284                        result.add(siteNode.getValueObject());
1285                    }
1286                    catch(Exception JavaDoc e)
1287                    {
1288                        logger.info("siteNode:" + registryVO.getEntityId() + " did not exist - skipping..");
1289                    }
1290                }
1291            }
1292            
1293            commitTransaction(db);
1294        }
1295        catch (Exception JavaDoc e)
1296        {
1297            rollbackTransaction(db);
1298            throw new SystemException("An error occurred when we tried to fetch which sitenode uses a metainfo. Reason:" + e.getMessage(), e);
1299        }
1300        
1301        return result;
1302    }
1303
1304    public List JavaDoc getReferencedObjects(String JavaDoc referencingEntityName, String JavaDoc referencingEntityId, Database db) throws SystemException, Exception JavaDoc
1305    {
1306        List JavaDoc result = new ArrayList JavaDoc();
1307        
1308        List JavaDoc registryVOList = getMatchingRegistryVOListForReferencingEntity(referencingEntityName, referencingEntityId, db);
1309        
1310        Iterator JavaDoc i = registryVOList.iterator();
1311        while(i.hasNext())
1312        {
1313            RegistryVO registryVO = (RegistryVO)i.next();
1314            if(registryVO.getEntityName().indexOf("Content") > -1)
1315            {
1316                try
1317                {
1318                    Content content = ContentController.getContentController().getContentWithId(new Integer JavaDoc(registryVO.getEntityId()), db);
1319                    logger.info("contentVersion:" + content.getContentId());
1320                    result.add(content.getValueObject());
1321                }
1322                catch(Exception JavaDoc e)
1323                {
1324                    logger.info("content:" + registryVO.getEntityId() + " did not exist - skipping..");
1325                }
1326            }
1327            else if(registryVO.getEntityName().indexOf("SiteNode") > -1)
1328            {
1329                try
1330                {
1331                    SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(registryVO.getEntityId()), db);
1332                    logger.info("siteNode:" + siteNode.getId());
1333                    result.add(siteNode.getValueObject());
1334                }
1335                catch(Exception JavaDoc e)
1336                {
1337                    logger.info("siteNode:" + registryVO.getEntityId() + " did not exist - skipping..");
1338                }
1339            }
1340        }
1341        
1342        return result;
1343    }
1344
1345    /**
1346     * Gets matching references
1347     */

1348    
1349    public List JavaDoc getMatchingRegistryVOListForReferencingEntity(String JavaDoc referencingEntityName, String JavaDoc referencingEntityId, Database db) throws SystemException, Exception JavaDoc
1350    {
1351        List JavaDoc matchingRegistryVOList = new ArrayList JavaDoc();
1352
1353        logger.info("referencingEntityName:" + referencingEntityName);
1354        logger.info("referencingEntityId:" + referencingEntityId);
1355        
1356        OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityName = $1 AND r.referencingEntityId = $2 ORDER BY r.registryId");
1357        oql.bind(referencingEntityName);
1358        oql.bind(referencingEntityId);
1359        
1360        QueryResults results = oql.execute(Database.ReadOnly);
1361        
1362        while (results.hasMore())
1363        {
1364            Registry registry = (Registry)results.next();
1365            RegistryVO registryVO = registry.getValueObject();
1366            logger.info("found match:" + registryVO.getEntityName() + ":" + registryVO.getEntityId());
1367            
1368            matchingRegistryVOList.add(registryVO);
1369        }
1370        
1371        results.close();
1372        oql.close();
1373
1374        return matchingRegistryVOList;
1375    }
1376    
1377    /**
1378     * Gets matching references
1379     */

1380    
1381    public List JavaDoc clearRegistryVOList(String JavaDoc referencingEntityName, String JavaDoc referencingEntityId, Database db) throws SystemException, Exception JavaDoc
1382    {
1383        List JavaDoc matchingRegistryVOList = new ArrayList JavaDoc();
1384        
1385        OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityName = $1 AND r.referencingEntityId = $2 ORDER BY r.registryId");
1386        oql.bind(referencingEntityName);
1387        oql.bind(referencingEntityId);
1388        
1389        QueryResults results = oql.execute();
1390        
1391        while (results.hasMore())
1392        {
1393            Registry registry = (Registry)results.next();
1394            db.remove(registry);
1395        }
1396        
1397        results.close();
1398        oql.close();
1399
1400        return matchingRegistryVOList;
1401    }
1402    
1403
1404    /**
1405     * Gets matching references
1406     */

1407    
1408    public void clearRegistryForReferencedEntity(String JavaDoc entityName, String JavaDoc entityId) throws SystemException, Exception JavaDoc
1409    {
1410        Database db = CastorDatabaseService.getDatabase();
1411        
1412        try
1413        {
1414            beginTransaction(db);
1415            
1416            OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.entityName = $1 AND r.entityId = $2 ORDER BY r.registryId");
1417            oql.bind(entityName);
1418            oql.bind(entityId);
1419                    
1420            QueryResults results = oql.execute();
1421
1422            while (results.hasMore())
1423            {
1424                Registry registry = (Registry)results.next();
1425                db.remove(registry);
1426            }
1427            
1428            results.close();
1429            oql.close();
1430
1431            commitTransaction(db);
1432        }
1433        catch (Exception JavaDoc e)
1434        {
1435            logger.warn("An error occurred so we should not complete the transaction:" + e);
1436            rollbackTransaction(db);
1437        }
1438    }
1439
1440    /**
1441     * Gets matching references
1442     */

1443    
1444    public void clearRegistryForReferencingEntityCompletingName(String JavaDoc entityCompletingName, String JavaDoc entityCompletingId) throws SystemException, Exception JavaDoc
1445    {
1446        Database db = CastorDatabaseService.getDatabase();
1447        
1448        try
1449        {
1450            beginTransaction(db);
1451            
1452            OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityCompletingName = $1 AND r.referencingEntityCompletingId = $2 ORDER BY r.registryId");
1453            oql.bind(entityCompletingName);
1454            oql.bind(entityCompletingId);
1455                    
1456            QueryResults results = oql.execute();
1457
1458            while (results.hasMore())
1459            {
1460                Registry registry = (Registry)results.next();
1461                db.remove(registry);
1462            }
1463            
1464            results.close();
1465            oql.close();
1466
1467            commitTransaction(db);
1468        }
1469        catch (Exception JavaDoc e)
1470        {
1471            logger.warn("An error occurred so we should not complete the transaction:" + e);
1472            rollbackTransaction(db);
1473        }
1474    }
1475
1476    /**
1477     * Gets matching references
1478     */

1479    
1480    public void clearRegistryForReferencingEntityName(String JavaDoc entityName, String JavaDoc entityId) throws SystemException, Exception JavaDoc
1481    {
1482        Database db = CastorDatabaseService.getDatabase();
1483        
1484        try
1485        {
1486            beginTransaction(db);
1487            
1488            OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.referencingEntityName = $1 AND r.referencingEntityId = $2 ORDER BY r.registryId");
1489            oql.bind(entityName);
1490            oql.bind(entityId);
1491                    
1492            QueryResults results = oql.execute();
1493
1494            while (results.hasMore())
1495            {
1496                Registry registry = (Registry)results.next();
1497                db.remove(registry);
1498            }
1499
1500            results.close();
1501            oql.close();
1502
1503            commitTransaction(db);
1504        }
1505        catch (Exception JavaDoc e)
1506        {
1507            logger.warn("An error occurred so we should not complete the transaction:" + e);
1508            rollbackTransaction(db);
1509        }
1510    }
1511
1512    /**
1513     * Clears all references to a entity
1514     */

1515/*
1516    public void clearRegistryForReferencedEntity(String entityName, String entityId) throws SystemException, Exception
1517    {
1518        Database db = CastorDatabaseService.getDatabase();
1519        
1520        try
1521        {
1522            beginTransaction(db);
1523            
1524            OQLQuery oql = db.getOQLQuery("DELETE FROM org.infoglue.cms.entities.management.impl.simple.RegistryImpl r WHERE r.entityName = $1 AND r.entityId = $2");
1525            oql.bind(entityName);
1526            oql.bind(entityId);
1527            QueryResults results = oql.execute();
1528            
1529            commitTransaction(db);
1530        }
1531        catch (Exception e)
1532        {
1533            logger.warn("An error occurred so we should not complete the transaction:" + e);
1534            rollbackTransaction(db);
1535        }
1536    }
1537*/

1538    
1539    /**
1540     * Gets siteNodeVersions which uses the metainfo
1541     */

1542    
1543    public List JavaDoc getSiteNodeVersionsWhichUsesContentVersionAsMetaInfo(ContentVersion contentVersion, Database db) throws SystemException, Exception JavaDoc
1544    {
1545        List JavaDoc siteNodeVersions = new ArrayList JavaDoc();
1546        
1547        OQLQuery oql = db.getOQLQuery("SELECT snv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl snv WHERE snv.serviceBindings.availableServiceBinding.name = $1 AND snv.serviceBindings.bindingQualifyers.name = $2 AND snv.serviceBindings.bindingQualifyers.value = $3");
1548        oql.bind("Meta information");
1549        oql.bind("contentId");
1550        oql.bind(contentVersion.getOwningContent().getId());
1551        
1552        QueryResults results = oql.execute();
1553        this.logger.info("Fetching entity in read/write mode");
1554
1555        while (results.hasMore())
1556        {
1557            SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next();
1558            siteNodeVersions.add(siteNodeVersion);
1559            //logger.info("siteNodeVersion:" + siteNodeVersion.getId());
1560
}
1561        
1562        results.close();
1563        oql.close();
1564
1565        return siteNodeVersions;
1566    }
1567
1568    /**
1569     * Gets siteNodeVersions which uses the metainfo
1570     */

1571    
1572    public SiteNodeVersion getLatestActiveSiteNodeVersionWhichUsesContentVersionAsMetaInfo(ContentVersion contentVersion, Database db) throws SystemException, Exception JavaDoc
1573    {
1574        SiteNodeVersion siteNodeVersion = null;
1575        
1576        OQLQuery oql = db.getOQLQuery("SELECT snv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl snv WHERE snv.serviceBindings.availableServiceBinding.name = $1 AND snv.serviceBindings.bindingQualifyers.name = $2 AND snv.serviceBindings.bindingQualifyers.value = $3 AND snv.isActive = $4 ORDER BY snv.siteNodeVersionId desc");
1577        oql.bind("Meta information");
1578        oql.bind("contentId");
1579        oql.bind(contentVersion.getOwningContent().getId());
1580        oql.bind(new Boolean JavaDoc(true));
1581        
1582        QueryResults results = oql.execute();
1583        this.logger.info("Fetching entity in read/write mode");
1584
1585        if (results.hasMore())
1586        {
1587            siteNodeVersion = (SiteNodeVersion)results.next();
1588        }
1589        
1590        results.close();
1591        oql.close();
1592
1593        return siteNodeVersion;
1594    }
1595
1596}
1597
Popular Tags