5 Replies Latest reply on Jun 16, 2010 4:54 AM by julien_viet

    chromattic sameNameSiblings problem

    kentxu2007

      I am trying to store data in the default workspace using Chromattic, like the native portlets do. It works well. However, I get "Duplicate context name" error when I insert child node with the same name.

       

      I have two classes, Order and OrderUnit. Order contains a list of OrderUnit.  Here is a snippet of my node type declaration xml.

       

      <childNodeDefinition name="*" defaultPrimaryType="ss:orderunit" autoCreated="false" mandatory="false"
                                    onParentVersion="COPY" protected="false" sameNameSiblings="true">
                  <requiredPrimaryTypes>
                     <requiredPrimaryType>ss:orderunit</requiredPrimaryType>
                  </requiredPrimaryTypes>
      </childNodeDefinition>

       

      As you can see,  I set sameNameSiblings attribute to "true".  The following code fails

       

      Order order1 = session.create(Order.class, "order1");

      // add orderunit

      OrderUnit unit1 = session.create(OrderUnit.class,"unit1");
      OrderUnit unit2 = session.create(OrderUnit.class,"unit1");

      session.persist(order1);

      order1.getOrderUnits().add(unit1);
      order1.getOrderUnits().add(unit2); //<==fails

       

      It works when I use JCR directly though.

       

      javax.jcr.Session jcrs=session.getJCRSession();
      Node root=jcrs.getNodeByUUID(order1.getId());
      root.addNode("unit1");
      root.addNode("unit1");

       

      I can not find any test cases in Chromattic or GateIn that uses sameNameSiblings="true". Is it supported? Is it a bad practice to have node with the same name?

        • 1. Re: chromattic sameNameSiblings problem
          julien_viet

          Hi,

           

          actually Chromattic is not designed to use same name siblings. My advice : if you can do without same name siblings, I think you should.

           

          When we started to write it, we asked ourselves if it was relevant to provide support for this feature and it appeared that it would be simpler to not support it, as it was raising some technical issue I can't remember right now.

           

          However it would be interesting actually to provide support for it in the future if there is a clear mapping between a JavaBean and a node type allowing same name siblings.

           

          Same name siblings corresponding classes, could allow one to one named hierarchical relationship based on a multivalue like:

           

          @OneTone

          @MappedBy("child_name")

          public abstract List<Child> getChild();

           

          It would be easier to handle one to many

           

          @OneToMany

          public abstract List<AnyChildType> getChildren();

           

          let me know

          1 of 1 people found this helpful
          • 2. Re: chromattic sameNameSiblings problem
            kentxu2007

            Thank you for the feedback.  I end up generating unique node name by suffix it with node UUID to work around this. The only trick here is to setName temporarily and change it after being persisted.

             

             

            @PrimaryType(name = "ss:order")

            class abstract Order {

             

                @OneToMany
                   public abstract Collection<OrderUnit> getOrderUnits();

             

                     @Create
                   protected abstract OrderUnit createOrderUnit();

             

                public OrderUnit addOrderUnit(...) {
                       OrderUnit unit=createOrderUnit();
                       unit.setName("unit");
                       getOrderUnits().add(unit);
                       unit.setName("unit-"+unit.getId());

                      ...
                       return unit;
                   }

             

            }

            • 3. Re: chromattic sameNameSiblings problem
              julien_viet

              We have rarely this is used for GateIn, but I see now the problem you are facing.

               

              The only thing I don't get is why you are using two steps and not set directly the name in the first setName ?

               

              Actually for that kind of situation it could be possible and fairly easy to add in Chromattic a feature for automatic name generation in the next versions.

              • 4. Re: chromattic sameNameSiblings problem
                kentxu2007

                Correct me if I am wrong here. The reason that I set the name twice is because I do not create my own unique name generator. I just use the node's UUID.

                 

                On one hand, I can not add a new child without first specify a name, this causes null path exception. On the other hand, I can not get the child UUID until it is added to the child collection, calling getId() before that would cause an illegal state exception. So, I am forced to set the name twice.

                 

                The following examples show the scenarios:

                 

                public OrderUnit addOrderUnit(...) {
                           OrderUnit  unit=createOrderUnit();
                            getOrderUnits().add(unit); //<-- fails due to null path
                            unit.setName("unit-"+unit.getId());

                          ...
                            return unit;

                }

                 

                or

                 

                public OrderUnit addOrderUnit(...) {
                           OrderUnit   unit=createOrderUnit();

                           unit.setName("unit-"+unit.getId()); //<-- fails due to illegal state
                            getOrderUnits().add(unit);

                          ...
                            return unit;

                }

                 

                It would be great to see auto name generation feature in the next Chromattic release. Thank you.

                • 5. Re: chromattic sameNameSiblings problem
                  julien_viet

                  In Chromattic 1.1 you can use setName to rename a persistent object, not in 1.0.

                   

                  You can rename an object by using a Map<String, OrderUnit> (Map gives a lot of control on the node name) instead of Collection<OrderUnit> in your parent:

                   

                  @OneToMany

                  public abstract Map<String, OrderUnit> getOrderUnits();

                   

                  And then

                   

                  OrderUnit unit = createOrderUnit();

                  getOrderUnits().put("unit", unit);

                  getOrderUnits().put("unit-" + unit.getId(), unit);

                   

                  that should work.