Update of 
/var/cvs/contributions/CMSContainer/cmsc/maintenance/src/java/com/finalist/cmsc/maintenance/beans
In directory 
james.mmbase.org:/tmp/cvs-serv11135/java/com/finalist/cmsc/maintenance/beans

Modified Files:
        CreateRelationsForSecondaryContent.java 
Log Message:
CMSC-1206   Asset: enhance the create-relations-asset-channel.jsp script to 
find a good channel


See also: 
http://cvs.mmbase.org/viewcvs/contributions/CMSContainer/cmsc/maintenance/src/java/com/finalist/cmsc/maintenance/beans
See also: http://www.mmbase.org/jira/browse/CMSC-1206


Index: CreateRelationsForSecondaryContent.java
===================================================================
RCS file: 
/var/cvs/contributions/CMSContainer/cmsc/maintenance/src/java/com/finalist/cmsc/maintenance/beans/CreateRelationsForSecondaryContent.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- CreateRelationsForSecondaryContent.java     24 Dec 2008 17:05:34 -0000      
1.9
+++ CreateRelationsForSecondaryContent.java     26 Dec 2008 09:36:04 -0000      
1.10
@@ -1,38 +1,49 @@
 package com.finalist.cmsc.maintenance.beans;
 
+import org.apache.commons.lang.StringUtils;
 import org.mmbase.bridge.Cloud;
 import org.mmbase.bridge.Node;
 import org.mmbase.bridge.NodeList;
 import org.mmbase.bridge.NodeManager;
 import org.mmbase.bridge.NodeQuery;
 import org.mmbase.bridge.Relation;
+import org.mmbase.util.logging.Logger;
+import org.mmbase.util.logging.Logging;
 
 import com.finalist.cmsc.mmbase.RelationUtil;
 import com.finalist.cmsc.repository.RepositoryUtil;
 import com.finalist.cmsc.security.SecurityUtil;
 import com.finalist.cmsc.services.publish.Publish;
 
-import org.mmbase.util.logging.Logger;
-import org.mmbase.util.logging.Logging;
-
 
 public class CreateRelationsForSecondaryContent {
 
+   private static final String OWNERREL = "ownerrel";
+   private static final String CONTENTELEMENT = "contentelement";
+   private static final String IMAGEREL = "imagerel";
+   private static final String IMAGEINLINEREL = "imageinlinerel";
+   private static final String POSREL = "posrel";
+   private static final String SOURCE = "SOURCE";
+   private static final String INLINEREL = "inlinerel";
+   private static final String DESTINATION = "DESTINATION";
+   private static final String CREATIONREL = "creationrel";
+   private static final String CONTENTCHANNEL = "contentchannel";
+
    private Cloud cloud;
    private Integer parentNumber;
-
+   private String type ;
    /** MMbase logging system */
    private static final Logger log = 
Logging.getLoggerInstance(CreateRelationsForSecondaryContent.class.getName());
 
-
-   public CreateRelationsForSecondaryContent(Cloud cloud, Integer 
parentNumber) {
+   public CreateRelationsForSecondaryContent(Cloud cloud) {
       this.cloud = cloud;
-      this.parentNumber = parentNumber;
    }
 
-   public String execute() throws Exception {
+   public String execute(Integer parentNumber,String type) throws Exception {
       //Creating relations between assets and content channel
       //Only process assets that don't have creationrels!
+      this.parentNumber = parentNumber;
+      this.type = type;
       
       NodeManager assetManager = cloud.getNodeManager("assetelement");
       NodeManager ownerManager = cloud.getNodeManager("user");
@@ -57,7 +68,22 @@
          Node asset = assets.getNode(i);
          if (!RepositoryUtil.hasCreationChannel(asset)) {
             counter++;
-            Relation relation = RelationUtil.createRelation(asset, root, 
"creationrel");
+            Relation relation = null;
+            //if type is not null . create relations acording to the realations
+            if(StringUtils.isNotBlank(type)) {
+               
+               Node channel = getRelatedChannel(asset);
+               if(channel == null) {
+                  relation = RelationUtil.createRelation(asset, root, 
CREATIONREL);
+               }
+               else {
+                  relation = RelationUtil.createRelation(asset, channel, 
CREATIONREL);
+               }
+            }
+            else {
+               relation = RelationUtil.createRelation(asset, root, 
"creationrel");
+            }
+
             Publish.publish(relation); // This method checks if it need to 
publish
                                        // otherwise, it doesn't harm anyone
          }
@@ -73,4 +99,48 @@
       return "success";
    }
 
+   private Node getRelatedChannel(Node asset) {
+      Node channel = null;
+      NodeManager contentManager = cloud.getNodeManager(CONTENTELEMENT);
+
+      if("images".equals(asset.getNodeManager().getName())) {
+         NodeList contents = asset.getRelatedNodes(contentManager, IMAGEREL, 
SOURCE);
+         channel = getChannel(contents);
+         if (channel != null) {
+            return channel;
+         }
+         contents = asset.getRelatedNodes(contentManager, IMAGEINLINEREL, 
SOURCE);
+         channel = getChannel(contents);
+         return channel;
+      }
+      else if ("attachments".equals(asset.getNodeManager().getName()) || 
"urls".equals(asset.getNodeManager().getName())) {
+
+         NodeList contents = asset.getRelatedNodes(contentManager, POSREL, 
SOURCE);
+         channel = getChannel(contents);
+         if (channel != null) {
+            return channel;
+         }
+         contents = asset.getRelatedNodes(contentManager, INLINEREL, SOURCE);
+         channel = getChannel(contents);
+         return channel;
+     
+      }
+      return channel;
+   }
+
+   private Node getChannel(NodeList contents) {
+      NodeManager channelManager = cloud.getNodeManager(CONTENTCHANNEL);
+      if(contents.size() > 0) {
+         for (int i = 0 ; i < contents.size() ; i++) {
+            Node relatedChannel = 
contents.getNode(i).getRelatedNodes(channelManager, CREATIONREL, 
DESTINATION).getNode(0);
+            if(RepositoryUtil.isTrash(relatedChannel)) {
+               continue;
+            }
+            else {
+               return relatedChannel;
+            }
+         }
+      }
+      return null;
+   }
 }
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to