Update of 
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/action
In directory 
james.mmbase.org:/tmp/cvs-serv22399/applications/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/action

Modified Files:
        UpdateNodeAction.java AbstractNodeAction.java 
        CreateNodeAction.java CreateRelationAction.java 
Added Files:
        AbstractRelationAction.java DeleteNodeAction.java 
        ToggleRelationAction.java SortRelationAction.java 
Log Message:
work in progress


See also: 
http://cvs.mmbase.org/viewcvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/action


AbstractRelationAction.java is new



DeleteNodeAction.java is new



ToggleRelationAction.java is new



SortRelationAction.java is new



Index: UpdateNodeAction.java
===================================================================
RCS file: 
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/action/UpdateNodeAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- UpdateNodeAction.java       11 Aug 2008 13:03:30 -0000      1.1
+++ UpdateNodeAction.java       18 Aug 2008 19:13:05 -0000      1.2
@@ -46,7 +46,7 @@
                this.nodenr = nodenr;
        }
 
-       protected final String getNodenr() {
+       public final String getNodenr() {
                return this.nodenr;
        }
 


Index: AbstractNodeAction.java
===================================================================
RCS file: 
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/action/AbstractNodeAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- AbstractNodeAction.java     11 Aug 2008 13:03:30 -0000      1.1
+++ AbstractNodeAction.java     18 Aug 2008 19:13:05 -0000      1.2
@@ -15,7 +15,9 @@
 import org.mmbase.applications.vprowizards.spring.cache.CacheFlushHint;
 import org.mmbase.applications.vprowizards.spring.util.DateTime;
 import org.mmbase.bridge.Node;
+import org.mmbase.bridge.NodeList;
 import org.mmbase.bridge.NodeManager;
+import org.mmbase.bridge.RelationManager;
 import org.mmbase.bridge.Transaction;
 import org.mmbase.util.logging.Logging;
 import org.springframework.web.multipart.MultipartFile;
@@ -59,29 +61,81 @@
        @Override
        public final void process(Map<String, Node> nodeMap, ResultContainer 
resultContainer) {
                this.resultContainer = resultContainer;
+               
+               //get the node
                node = createNode(resultContainer.getTransaction(), nodeMap, 
resultContainer.getRequest());
                
-               if (resultContainer.containsGlobalErrors()) {
+               if (hasErrors()) {
                        return;
                }
-               if (node == null) {
+               
+               if (node == null && isNodeNullIllegal()) {
                        throw new IllegalStateException(
                                        "No node has been provided, and no 
error has been set. Either of these should happen");
                }
-               if (shouldProcess(node)) {
+               
+               //now check if no illegal fields are set for this node type
+               if(node != null){
+                       checkBasicFields();
+               }
+                       
+               //no error and no overridden 'proceed' flag? Set the values and 
+               //call the post processing callback method.
+               if (!hasErrors() &&  shouldProcess(node)) {
+                       if(node!= null){
                        setBasicFields();
+                       }
+                       
                        if (resultContainer.containsFieldErrors()) {
                                return;
                        }
+                       
+                       if (node != null) {
                        if (getId() != null) {
                                resultContainer.getIdMap().put(getId(), node);
-                               log.debug("node ["+node+"] is registered under 
id " + getId());
+                                       log.debug("node [" + node + "] is 
registered under id " + getId());
                        }
                        processNode(resultContainer.getTransaction());
+                       }
+                       
+                       //even with a null node it can be necessary to create a 
cache flush hint
+                       //for instance when a node was deleted.
                        createCacheFlushHints();
                }
        }
 
+       /**
+        * This template method determines if it is an error when the node is 
null.
+        * Override this for concrete actions that actually don't need a node 
always.
+        * @return true by default.
+        */
+       protected boolean isNodeNullIllegal() {
+               return true;
+       }
+
+       /**
+        * this template method is called before any changes are made to the 
node to edit. if it returns false, no more
+        * action will be taken. Use this if you want to use some kind of 
switch.
+        * 
+        * @param node
+        *            the current node.
+        * @return true if there is no reason not to process this node
+        */
+       protected boolean shouldProcess(Node node) {
+               return true;
+       }
+
+       /**
+        * Check if all the fields set for this node action actually exist in 
the nodemanager.
+        */
+       private void checkBasicFields() {
+               for(String fieldName: fields.keySet()){
+                       if(!node.getNodeManager().hasField(fieldName)){
+                               addFieldError(fieldName, 
"error.field.nonexistant", new String[]{fieldName, 
node.getNodeManager().getName()});
+                       }
+               }
+       }
+
        /*
         * these are the template methods
         */
@@ -109,22 +163,13 @@
         */
        protected void processNode(Transaction transaction) {};
 
-       /**
-        * this template method is called before any changes are made to the 
node to edit. if it returns false, no more
-        * action wil be taken. Use this if you want to use some kind of switch.
-        * 
-        * @param node
-        *            the current node.
-        * @return true if there is no reason not to process this node
-        */
-       protected boolean shouldProcess(Node node) {
-               return true;
-       }
-       
        protected final Locale getLocale(){
                return resultContainer.getLocale();
        }
        
+       /**
+        * @return the node that is created for this node action.
+        */
        protected final Node getNode(){
                return node;
        }
@@ -152,7 +197,7 @@
        }
        
        /**
-        * Creates a field error for this action
+        * Create a field error for this action, using a key without place 
holder values
         * @param field
         * @param key
         */
@@ -161,6 +206,30 @@
        }
 
        
+       
+       /**
+        * Creates a field error for this action, where there is some sort of 
error when setting the field.
+        * This version does not take the (offending) field value but the error 
message.
+        * This method uses it's own error message key.
+        * @param field
+        * @param message
+        */
+       protected final void addFieldErrorTypeMessage(String field, String 
message) {
+               resultContainer.getFieldErrors().add(new FieldError(field, 
"error.field.message",new String[]{field, message}, getLocale()));
+       }
+       
+       /**
+        * Creates a field error for this action, where the value set on some 
field is
+        * invalid.
+        * This method uses it's own error message key. 
+        * @param field
+        * @param value
+        */
+       protected final void addFieldErrorTypeValue(String field, String value) 
{
+               resultContainer.getFieldErrors().add(new FieldError(field, 
"error.field.value",new String[]{field, value}, getLocale()));
+       }
+
+       
        /**
         * Creates a global error for this action.
         * @param key
@@ -216,7 +285,7 @@
                                        setChanged();
                                }
                        } catch (ParseException e) {
-                               addFieldError(field, e.getMessage());
+                               addFieldErrorTypeMessage(field, e.getMessage());
                        }
                }
 
@@ -288,7 +357,7 @@
 
                        }
                } catch (IOException e) {
-                       addFieldError("file", "" + e);
+                       addFieldErrorTypeMessage("file", "" + e);
                }
                if (changed) {
                        setChanged();
@@ -323,6 +392,85 @@
                return id;
        }
 
+       /**
+        * Check if a relation is possible from the given source to the given 
destination with
+        * the given relation manager.
+        * @param relationManager
+        * @return
+        */
+       protected final boolean checkTypeRel(RelationManager relationManager, 
Node sourceNode, Node destinationNode) {
+               String constraints = String.format(
+                               "snumber=%s AND dnumber=%s and rnumber=%s", 
+                               "" + sourceNode.getNodeManager().getNumber(), 
+                               "" + 
destinationNode.getNodeManager().getNumber(),
+                               "" + relationManager.getNumber());
+               NodeList nl = relationManager.getList(constraints, null, null);
+               if(nl.size() == 0){
+                       addGlobalError(
+                                       "error.create.relation.typerel", 
+                                       new 
String[]{sourceNode.getNodeManager().getName(), 
destinationNode.getNodeManager().getName(), relationManager.getName()});
+                       addGlobalError("error.create.relation");
+                       return false;
+               }
+               return true;
+       }
+       
+
+       /**
+        * can the current owner create a node of this type?
+        * set global error when fail.
+        * @param nodeManager
+        * @return true when allowed.
+        */
+       protected final boolean mayWrite(NodeManager nodeManager) {
+               if(nodeManager == null){
+                       throw new NullPointerException("argument nodeManager is 
null");
+               }
+               boolean mayWrite= nodeManager.mayWrite();
+               if(!mayWrite){
+                       addGlobalError("error.authorization.write", new 
String[]{nodeManager.getName()});
+               }
+               return mayWrite;
+       }
+       
+       /**
+        * can the current owner create a node of this type?
+        * set global error when fail.
+        * @param nodeManager
+        * @return true when allowed.
+        */
+       protected final boolean mayCreate(NodeManager nodeManager) {
+               if(nodeManager == null){
+                       throw new NullPointerException("argument nodeManager is 
null");
+               }
+               boolean mayCreate = nodeManager.mayCreateNode();
+               if(!mayCreate){
+                       addGlobalError("error.authorization.create", new 
String[]{nodeManager.getName()});
+               }
+               return mayCreate;
+       }
+       
+       /**
+        * can the current owner delete this node?
+        * set global error when fail.
+        * @param nodeManager
+        * @return true when allowed.
+        */
+       protected final boolean mayDelete(Node node) {
+               if(node == null){
+                       throw new NullPointerException("argument node is null");
+               }
+               boolean mayDelete = node.mayDelete();
+               if(!mayDelete){
+                       addGlobalError("error.authorization.delete", new 
String[]{node.getNumber()+"", node.getNodeManager().getName()});
+               }
+               return mayDelete;
+       }
+       
+       protected final boolean hasErrors(){
+               return (resultContainer.getGlobalErrors().size() == 0 && 
resultContainer.getFieldErrors().size() == 0);
+       }
+
        @Override
        public String toString() {
                return new ReflectionToStringBuilder(this).toString();


Index: CreateNodeAction.java
===================================================================
RCS file: 
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/action/CreateNodeAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- CreateNodeAction.java       11 Aug 2008 13:03:30 -0000      1.1
+++ CreateNodeAction.java       18 Aug 2008 19:13:05 -0000      1.2
@@ -21,16 +21,20 @@
  */
 public class CreateNodeAction extends AbstractNodeAction {
 
-       private String nodemanager;
+       private String nodeType;
 
-       public final void setNodemanger(String nodemanger) {
-               this.nodemanager = nodemanger;
+       public final void setNodeType(String nodemanger) {
+               this.nodeType = nodemanger;
+       }
+
+       public final String getNodenType() {
+               return this.nodeType;
        }
 
        @Override
        protected final void createCacheFlushHints() {
                CacheFlushHint hint = new 
CacheFlushHint(CacheFlushHint.TYPE_NODE);
-               hint.setNodeType(nodemanager);
+               hint.setNodeType(nodeType);
                addCachFlushHint(hint);
        }
 
@@ -40,13 +44,10 @@
                if (nodeManager == null) {
                        return null;
                } else {
-                       if(nodeManager.mayCreateNode()){
+                       if(mayCreate(nodeManager)){
                                return nodeManager.createNode();
-                       }else{
-                               addGlobalError("error.create.authorization", 
new String[]{nodemanager});
-                               addGlobalError("error.create.node");
-                               return null;
                        }
+                       return null;
                }
        }
 
@@ -57,21 +58,15 @@
         * @return the node manager used to create a new node with
         */
        protected NodeManager resolveNodemanager(Transaction transaction) {
-               if (StringUtils.isBlank(nodemanager)) {
+               if (StringUtils.isBlank(nodeType)) {
                        addGlobalError("error.property.required", new String[] 
{ "nodemanager", CreateNodeAction.class.getName() });
-                       addGlobalError("error.create.node");
                        return null;
-               } else if (transaction.hasNodeManager(nodemanager)) {
-                       return transaction.getNodeManager(nodemanager);
+               } else if (transaction.hasNodeManager(nodeType)) {
+                       return transaction.getNodeManager(nodeType);
                } else {
-                       addGlobalError("error.illegal.nodemanager", new 
String[] { nodemanager });
-                       addGlobalError("error.create.node");
+                       addGlobalError("error.illegal.nodemanager", new 
String[] { nodeType });
                        return null;
                }
        }
 
-       protected final String getNodenmanager() {
-               return this.nodemanager;
-       }
-
 }


Index: CreateRelationAction.java
===================================================================
RCS file: 
/var/cvs/speeltuin/ernst/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/action/CreateRelationAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- CreateRelationAction.java   11 Aug 2008 13:03:30 -0000      1.1
+++ CreateRelationAction.java   18 Aug 2008 19:13:05 -0000      1.2
@@ -11,6 +11,7 @@
 import org.mmbase.applications.vprowizards.spring.util.PathBuilder;
 import org.mmbase.bridge.Node;
 import org.mmbase.bridge.NodeList;
+import org.mmbase.bridge.NodeManager;
 import org.mmbase.bridge.NotFoundException;
 import org.mmbase.bridge.Query;
 import org.mmbase.bridge.Relation;
@@ -22,34 +23,21 @@
 import org.mmbase.util.logging.Logging;
 
 /**
- * this action creates a relation. source and destiation nodes can be set 
either
- * as nodennr or as nodenr reference, which meens they are looked up in the id
- * map of the [EMAIL PROTECTED] ResultContainer}.
+ * this action creates a relation. source and destiation nodes can be set 
either as nodennr or as nodenr reference,
+ * which meens they are looked up in the id map of the [EMAIL PROTECTED] 
ResultContainer}.
  * 
  * @author Ernst Bunders
  * 
  */
-public class CreateRelationAction extends AbstractNodeAction {
+public class CreateRelationAction extends AbstractRelationAction {
        
        public static final String SORT_POSITION_BEGIN = "begin";
-
        public static final String SORT_POSITION_END = "end";
 
        private static final Logger log = 
Logging.getLoggerInstance(CreateRelationAction.class);
 
-       private Node sourceNode = null;
-       private Node destinationNode = null;
-       
-       private String sourceNodeNumber;
-       private String destinationNodeNumber;
-
-       private String sourceNodeRef;
-       private String destinationNodeRef;
-
-       private String role;
-
        private String sortPosition = CreateRelationAction.SORT_POSITION_END;
-       private String sortField ;
+       private String sortField;
        
        @Override
        protected void createCacheFlushHints() {
@@ -60,135 +48,41 @@
                addCachFlushHint(hint);
                
        }
+
        @Override
-       protected Node createNode(Transaction transaction, Map<String,Node> 
idMap, HttpServletRequest request) {
-               if(StringUtils.isBlank(role)){
-                       addGlobalError("error.property.required", new 
String[]{"role", this.getClass().getName()});
-                       addGlobalError("error.create.relation");
-                       return null;
-               }else{
-                       RelationManager relationManager = 
transaction.getRelationManager(role);
-                       if(relationManager != null){
-                               if(resolveSourceAndDestination(transaction, 
idMap )){
-                                       //create the relation node.
-                                       if(checkAuthorization(relationManager) 
&& checkTypeRel(relationManager)){
-                                               Relation rel = 
relationManager.createRelation(sourceNode, destinationNode);
-                                               return rel;
-                                       }else{
-                                               return null;
+       protected Node doCreateNode(Transaction transaction, Map<String, Node> 
idMap, HttpServletRequest request) {
+               //preconditions
+               if(!SORT_POSITION_BEGIN.equals(sortPosition) && 
!SORT_POSITION_END.equals(sortPosition)){
+                       addFieldErrorTypeValue("sortPosition", sortPosition);
                                        }
+               if(!StringUtils.isBlank(sortField)){
+                       if(!relationManager.hasField(sortField)){
+                               addFieldError("sortField", 
"error.field.nonexistant", new String[]{"sortField", 
relationManager.getName()});
                                }
-                       }else{
-                               addGlobalError("error.illegal.relationmanager", 
new String[]{"role"});
-                               addGlobalError("error.create.relation");
-                               return null;
                        }
+               
+               if (!hasErrors()) {
+                       Relation rel = 
relationManager.createRelation(sourceNode, destinationNode);
+                       return rel;
                }
                return null;
        }
        
        /**
         * When sortField is set, and it is a valid field, the sort position is 
calculated and set on the new relation.
+        * 
         * @see 
org.mmbase.applications.vprowizards.spring.action.AbstractNodeAction#processNode(org.mmbase.bridge.Transaction)
         */
        @Override
        protected void processNode(Transaction transaction) {
-               if(!StringUtils.isBlank(sortField)){
-                       //check if the sortField exists
-                       try{
-                               getNode().getNodeManager().getField(sortField);
+               if (!StringUtils.isBlank(sortField)) {
                                
-                               //set the position if we need to do that
+                       // set the position if we need to do that
                                Integer sortPositionValue = 
resolvePosition(transaction);
-                               if(sortPositionValue != null){
+                       if (sortPositionValue != null) {
                                        getNode().setIntValue(sortField, 
sortPositionValue);
                                }
-                       }catch(NotFoundException e){
-                               addGlobalError("error.field.unknown", 
-                                               new String[]{sortField, 
this.getClass().getName(), getNode().getNodeManager().getName()});
-                       }
-               }
-       }
-       
-       /**
-        * Check if a relation is possible from the given source to the given 
destination with
-        * the given relation manager.
-        * @param relationManager
-        * @return
-        */
-       private boolean checkTypeRel(RelationManager relationManager) {
-               String constraints = String.format(
-                               "snumber=%s AND dnumber=%s and rnumber=%s", 
-                               "" + sourceNode.getNodeManager().getNumber(), 
-                               "" + 
destinationNode.getNodeManager().getNumber(),
-                               "" + relationManager.getNumber());
-               NodeList nl = relationManager.getList(constraints, null, null);
-               if(nl.size() == 0){
-                       addGlobalError(
-                                       "error.create.relation.typerel", 
-                                       new 
String[]{sourceNode.getNodeManager().getName(), 
destinationNode.getNodeManager().getName(), role});
-                       addGlobalError("error.create.relation");
-                       return false;
-               }
-               return true;
-       }
-       /**
-        * can the current owner create a node of this type?
-        * set glabal error when fale.
-        * @param relationManager
-        * @return true when allowed.
-        */
-       private boolean checkAuthorization(RelationManager relationManager) {
-               boolean mayCreate = relationManager.mayCreateNode();
-               if(!mayCreate){
-                       addGlobalError("error.create.authorization", new 
String[]{relationManager.getName()});
-               }
-               return mayCreate;
        }
-       /**
-        * (try to) resolve the source and destination nodes for this relation.
-        * Set global errors when fail. 
-        * @return true when source and destination nodes are found
-        */
-       private boolean resolveSourceAndDestination(Transaction transaction, 
Map<String,Node> idMap) {
-               sourceNode = resolveNode("error.create.relation.nosource", 
sourceNodeRef, sourceNodeNumber, idMap, transaction); 
-               destinationNode = 
resolveNode("error.create.relation.nodestination", destinationNodeRef, 
destinationNodeNumber, idMap, transaction);
-               return (sourceNode != null && destinationNode != null);
-       }
-       
-       /**
-        * Try to resolve a node, by trying either the nodenr or node ref. If 
there are problems, relevant global errors are created. 
-        * @param refNotFoundErrorKey
-        * @param nodeRef
-        * @param nodenr
-        * @param idMap
-        * @param transaction
-        * @return the node
-        */
-       private final Node resolveNode(String refNotFoundErrorKey, String 
nodeRef, String nodenr, Map<String,Node>idMap, Transaction transaction){
-               Node result = null;
-               if(nodenr == null){
-                       if(nodeRef == null){
-                               addGlobalError(refNotFoundErrorKey);
-                               addGlobalError("error.create.relation");
-                       }else{
-                               if(idMap.get(nodeRef) == null){
-                                       
addGlobalError("error.node.notfound.idmap", new String[]{nodeRef});
-                                       addGlobalError("error.create.relation");
-                               }else{
-                                       result = idMap.get(nodeRef);
-                               }
-                       }
-               }else{
-                       //try to load the node
-                       try{
-                               result = transaction.getNode(nodenr);
-                       }catch(NotFoundException e){
-                               addGlobalError("error.node.notfound", new 
String[]{nodenr});
-                               addGlobalError("error.create.relation");
-                       }
-               }
-               return result;
        }
        
        /**
@@ -196,14 +90,10 @@
         * position value will be, depending on the sortPostion field, either 
the lowest current sort position value minus
         * 1, or the highest current sort position value plus one.
         * 
-        * @param sortField
-        * @param sortPosition
-        * @param source
-        * @param destination
-        * @param role
-        * @return the new sort position or null, if something went wrong.
+        * @param transaction
+        * @return the value to be inserted as the new sort position.
         */
-       private Integer resolvePosition(Transaction transaction) {
+       protected final Integer resolvePosition(Transaction transaction) {
         if (StringUtils.isBlank(sortField)) {
             return null;
         }
@@ -213,22 +103,13 @@
         try {
             // find the lowest or highest relation number
                
-               //it is unlikely that the path matches duplicate builder names 
here, but who knows? 
-               PathBuilder pathBuilder = new PathBuilder(new String[]{
-                               sourceNode.getNodeManager().getName(),
-                               role,
-                               destinationNode.getNodeManager().getName()
-               });
-            q = Queries.createQuery(
-                       transaction, 
-                       sourceNode.getNumber() + "", 
-                       pathBuilder.getPath(), 
-                       pathBuilder.getStep(1) + "." + sortField, 
-                       null, 
-                       pathBuilder.getStep(1) + "." + sortField, 
-                       (sortPosition.equals("begin") ? "up" : "down"), 
-                       null, 
-                       false);
+                       // it is unlikely that the path matches duplicate 
builder names here, but who knows?
+                       PathBuilder pathBuilder = new PathBuilder(new String[] 
{ sourceNode.getNodeManager().getName(), role,
+                                       
destinationNode.getNodeManager().getName() });
+                       q = Queries.createQuery(transaction, 
sourceNode.getNumber() + "", pathBuilder.getPath(), pathBuilder
+                                       .getStep(1)
+                                       + "." + sortField, null, 
pathBuilder.getStep(1) + "." + sortField,
+                                       (sortPosition.equals("begin") ? "up" : 
"down"), null, false);
             q.setMaxNumber(1);
             NodeList nl = transaction.getList(q);
             if (nl.size() > 0) {
@@ -238,10 +119,27 @@
 
             return new Integer(position);
         } catch (RuntimeException e) {
-            addGlobalError("error.unexpected", new String[]{e.getMessage()});
-            log.error("something went wrong running a query to find out the 
position of a new relation. query: ["+q.toString()+"]", e);
+                       addGlobalError("error.unexpected", new String[] { 
e.getMessage() });
+                       log.error("something went wrong running a query to find 
out the position of a new relation. query: ["
+                                       + q.toString() + "]", e);
         }
         return null;
     }
 
+       public String getSortPosition() {
+               return sortPosition;
+       }
+
+       public void setSortPosition(String sortPosition) {
+               this.sortPosition = sortPosition;
+       }
+
+       public String getSortField() {
+               return sortField;
+       }
+
+       public void setSortField(String sortField) {
+               this.sortField = sortField;
+       }
+       
 }
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to