Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to copy a Criteria object

    Hi there,

    I would like to make an exact copy of a criteria object, so that I can modify the old and the new copies independently. How do I do that?

    The incoming criteria might be an AdvancedCriteria or a simple one, and it might contain array elements, too.

    Thank you for yout help:

    Csillag

    #2
    There is not currently a shortcut to implementing a recursive copy.

    Comment


      #3
      OK, so what's the recommended approach to do this manually?

      My guess would be that this should be probably handled in JS. Is there a way to serialize / deserialize, or uneval / eval a criteria, or something like that? Or does one have to recursively loop over the object manually?

      (I am not really proficient in JS.)

      Thank you for your help:

      Csillag

      Comment


        #4
        You could handle this in JavaScript via JSNI but there are some wrinkles around the Java <--> JavaScript data translation etc that could make this trickier than expected or introduce unexpected side effects.

        For now we'd recommend simply doing this in Java manually (creating a new instance, and looping through the various properties creating new sub-criterion instances). This should be relatively straightforward to achieve.

        Comment


          #5
          Originally posted by Isomorphic
          For now we'd recommend simply doing this in Java manually (creating a new instance, and looping through the various properties creating new sub-criterion instances). This should be relatively straightforward to achieve.
          Since the criteria to copy can be an AdvancedCriteria, which can (in turn) contain further AdvancedCriteria, wouldn't this involve a lot of type-checking and sub-cases? In the end, I would need to copy/repeat all possible ways to build criteria (including the various constructors for AdvancedCriteria, criteria combiner, etc.)

          Does not really seem straightforward to me. (And especially not future-proof, since the Criteria system is expected to evolve over time.)

          So, it would help a lot is there was an other solution.

          Thank you for your help:

          Csillag

          Comment


            #6
            Is there really no solution for this? (Either I don't properly understand what was proposed, or there must be a better way...)

            Comment


              #7
              The solution is to write the code as previously suggested. It's not especially difficult; a helper method may be added one day that simply does exactly what we suggest you do now, but there is no ETA for such helper and it is not clear that it is even worthwhile. So please proceed.

              Comment


                #8
                Hi Csillag,
                This is what I am using to make a true copy of the whole DSRequest:
                Code:
                    public static native JavaScriptObject deepCopy(JavaScriptObject obj) /*-{
                    // http://oranlooney.com/functional-javascript/
                    // A clone of an object is an empty object
                            // with a prototype reference to the original.
                
                    // a private constructor, used only by this one clone.
                            function Clone() { }
                    Clone.prototype = obj;
                    var c = new Clone();
                            c.constructor = Clone;
                            return c;
                
                    }-*/;
                HTH
                MichalG

                Comment


                  #9
                  Hi MichailG,

                  Thank you for the tip. I was thinking about something similar, but Isomoprhic has specifically stated the using this in JS space could introduce unwanted side effects, so I would like to avoid this, if possible. (I am well aware that I am not proficient in JS to understart all possible implications.)

                  Thank you anyway:

                  Csillag

                  Comment


                    #10
                    Is there any reason that JSON.encode/decode is not a good idea for this? (I know there is the issue of trusting the code since it is an eval to decode it)

                    I want to persist Criteria (eg, save a filter view)

                    Comment


                      #11
                      That's a reasonable approach, and when we've implemented saved search we've done so via JSON serialization.

                      Comment


                        #12
                        As far as I remember I have got js exceptions when AdvancedCriteria object was complicated enough (using JSON.encode/decode).
                        Could be corrected till now.
                        MichalG

                        Comment

                        Working...
                        X