Calculating segment count for UNT in EDIFACT messages

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating segment count for UNT in EDIFACT messages

Dr Immo Benjes
Hi,

I am using smooks 1.4  with the EDIFACT EJC classes.
When you generate an EDIFACT message you have to set the segment count in the UNT segment at the end of the message.
Is there any way to calculate the segment count automatically independent of the actual message?


Btw I am still using 1.4 as it seems to be the only one with pre-build EJC EDIFACT classes as far as I know.

Regards

Immo
 
Dr. Immo Benjes
Senior Software Architect

Locus Software Ltd
Castle Court
41 London Road
Reigate
Surrey RH2 9RJ
United Kingdom
Tel: +44 (0)1737 735012
Web: www.locussoftware.co.uk
Email: [hidden email]





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Tom Fennelly
Hi.

 From what I remember, I don't think there is.  As it's just a model, it
just populates from what it finds in the message i.e. doesn't
"calculate" missing values.  You could try hooking up a debugger.

Regards,

Tom.


On 09/01/2014 16:24, Dr Immo Benjes wrote:

> Hi,
>
> I am using smooks 1.4  with the EDIFACT EJC classes.
> When you generate an EDIFACT message you have to set the segment count in the UNT segment at the end of the message.
> Is there any way to calculate the segment count automatically independent of the actual message?
>
>
> Btw I am still using 1.4 as it seems to be the only one with pre-build EJC EDIFACT classes as far as I know.
>
> Regards
>
> Immo
>    
> Dr. Immo Benjes
> Senior Software Architect
>
> Locus Software Ltd
> Castle Court
> 41 London Road
> Reigate
> Surrey RH2 9RJ
> United Kingdom
> Tel: +44 (0)1737 735012
> Web: www.locussoftware.co.uk
> Email: [hidden email]
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Dr Immo Benjes
Hi Tom,

I was thinking of using reflection to calculate the segment count.

Basically you travel the tree and increment the counter for each non-null segment you come across. The only problem is to identify whether a class is a segment (should be in package org.milyn.edi.unedifact.dXXX.common) or a field in a segment (should be in package org.milyn.edi.unedifact.dXXX.common.field).
So judging by the package name seems to be the only way.

Unfortunately there are no Interfaces the segments implement (could have been an empty interface).

For future versions it might be nice to have messages implement one Interface, segments another one etc.

Do you foresee any problems with this approach?

Regards

Immo
 
Dr. Immo Benjes
Senior Software Architect

Locus Software Ltd
Castle Court
41 London Road
Reigate
Surrey RH2 9RJ
United Kingdom
Tel: +44 (0)1737 735012
Web: www.locussoftware.co.uk
Email: [hidden email]




On 9 Jan 2014, at 16:30, Tom Fennelly <[hidden email]> wrote:

> Hi.
>
> From what I remember, I don't think there is.  As it's just a model, it just populates from what it finds in the message i.e. doesn't "calculate" missing values.  You could try hooking up a debugger.
>
> Regards,
>
> Tom.
>
>
> On 09/01/2014 16:24, Dr Immo Benjes wrote:
>> Hi,
>>
>> I am using smooks 1.4  with the EDIFACT EJC classes.
>> When you generate an EDIFACT message you have to set the segment count in the UNT segment at the end of the message.
>> Is there any way to calculate the segment count automatically independent of the actual message?
>>
>>
>> Btw I am still using 1.4 as it seems to be the only one with pre-build EJC EDIFACT classes as far as I know.
>>
>> Regards
>>
>> Immo
>>   Dr. Immo Benjes
>> Senior Software Architect
>>
>> Locus Software Ltd
>> Castle Court
>> 41 London Road
>> Reigate
>> Surrey RH2 9RJ
>> United Kingdom
>> Tel: +44 (0)1737 735012
>> Web: www.locussoftware.co.uk
>> Email: [hidden email]
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Tom Fennelly
Hi Immo.

I think I'd try find a way of hooking into the event stream generated by
Smooks, and watch for new segment events and count based on that.  I
would try to avoid using reflection.

T.

On 09/01/2014 16:41, Dr Immo Benjes wrote:

> Hi Tom,
>
> I was thinking of using reflection to calculate the segment count.
>
> Basically you travel the tree and increment the counter for each non-null segment you come across. The only problem is to identify whether a class is a segment (should be in package org.milyn.edi.unedifact.dXXX.common) or a field in a segment (should be in package org.milyn.edi.unedifact.dXXX.common.field).
> So judging by the package name seems to be the only way.
>
> Unfortunately there are no Interfaces the segments implement (could have been an empty interface).
>
> For future versions it might be nice to have messages implement one Interface, segments another one etc.
>
> Do you foresee any problems with this approach?
>
> Regards
>
> Immo
>    
> Dr. Immo Benjes
> Senior Software Architect
>
> Locus Software Ltd
> Castle Court
> 41 London Road
> Reigate
> Surrey RH2 9RJ
> United Kingdom
> Tel: +44 (0)1737 735012
> Web: www.locussoftware.co.uk
> Email: [hidden email]
>
>
>
>
> On 9 Jan 2014, at 16:30, Tom Fennelly <[hidden email]> wrote:
>
>> Hi.
>>
>>  From what I remember, I don't think there is.  As it's just a model, it just populates from what it finds in the message i.e. doesn't "calculate" missing values.  You could try hooking up a debugger.
>>
>> Regards,
>>
>> Tom.
>>
>>
>> On 09/01/2014 16:24, Dr Immo Benjes wrote:
>>> Hi,
>>>
>>> I am using smooks 1.4  with the EDIFACT EJC classes.
>>> When you generate an EDIFACT message you have to set the segment count in the UNT segment at the end of the message.
>>> Is there any way to calculate the segment count automatically independent of the actual message?
>>>
>>>
>>> Btw I am still using 1.4 as it seems to be the only one with pre-build EJC EDIFACT classes as far as I know.
>>>
>>> Regards
>>>
>>> Immo
>>>    Dr. Immo Benjes
>>> Senior Software Architect
>>>
>>> Locus Software Ltd
>>> Castle Court
>>> 41 London Road
>>> Reigate
>>> Surrey RH2 9RJ
>>> United Kingdom
>>> Tel: +44 (0)1737 735012
>>> Web: www.locussoftware.co.uk
>>> Email: [hidden email]
>>>
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>      http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Dr Immo Benjes
Hi Tom,

this is for generating EDIFACT messages not parsing them. I need to set the UNT segment count before I generate the actual EDIFACT message.

Regards

Immo
 
Dr. Immo Benjes
Senior Software Architect

Locus Software Ltd
Castle Court
41 London Road
Reigate
Surrey RH2 9RJ
United Kingdom
Tel: +44 (0)1737 735012
Web: www.locussoftware.co.uk
Email: [hidden email]




On 9 Jan 2014, at 16:47, Tom Fennelly <[hidden email]> wrote:

> Hi Immo.
>
> I think I'd try find a way of hooking into the event stream generated by Smooks, and watch for new segment events and count based on that.  I would try to avoid using reflection.
>
> T.
>
> On 09/01/2014 16:41, Dr Immo Benjes wrote:
>> Hi Tom,
>>
>> I was thinking of using reflection to calculate the segment count.
>>
>> Basically you travel the tree and increment the counter for each non-null segment you come across. The only problem is to identify whether a class is a segment (should be in package org.milyn.edi.unedifact.dXXX.common) or a field in a segment (should be in package org.milyn.edi.unedifact.dXXX.common.field).
>> So judging by the package name seems to be the only way.
>>
>> Unfortunately there are no Interfaces the segments implement (could have been an empty interface).
>>
>> For future versions it might be nice to have messages implement one Interface, segments another one etc.
>>
>> Do you foresee any problems with this approach?
>>
>> Regards
>>
>> Immo
>>   Dr. Immo Benjes
>> Senior Software Architect
>>
>> Locus Software Ltd
>> Castle Court
>> 41 London Road
>> Reigate
>> Surrey RH2 9RJ
>> United Kingdom
>> Tel: +44 (0)1737 735012
>> Web: www.locussoftware.co.uk
>> Email: [hidden email]
>>
>>
>>
>>
>> On 9 Jan 2014, at 16:30, Tom Fennelly <[hidden email]> wrote:
>>
>>> Hi.
>>>
>>> From what I remember, I don't think there is.  As it's just a model, it just populates from what it finds in the message i.e. doesn't "calculate" missing values.  You could try hooking up a debugger.
>>>
>>> Regards,
>>>
>>> Tom.
>>>
>>>
>>> On 09/01/2014 16:24, Dr Immo Benjes wrote:
>>>> Hi,
>>>>
>>>> I am using smooks 1.4  with the EDIFACT EJC classes.
>>>> When you generate an EDIFACT message you have to set the segment count in the UNT segment at the end of the message.
>>>> Is there any way to calculate the segment count automatically independent of the actual message?
>>>>
>>>>
>>>> Btw I am still using 1.4 as it seems to be the only one with pre-build EJC EDIFACT classes as far as I know.
>>>>
>>>> Regards
>>>>
>>>> Immo
>>>>   Dr. Immo Benjes
>>>> Senior Software Architect
>>>>
>>>> Locus Software Ltd
>>>> Castle Court
>>>> 41 London Road
>>>> Reigate
>>>> Surrey RH2 9RJ
>>>> United Kingdom
>>>> Tel: +44 (0)1737 735012
>>>> Web: www.locussoftware.co.uk
>>>> Email: [hidden email]
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>     http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>   http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Ioan Eugen Stan
Hello Immo,

We also had that problem and our solution was to compute it. I've
added Alex to the list. He wrote the code wit reflection. He's
planning to add support for  segemnt counting in 1.6-SNAPSHOT but will
take a while.

@Alex: can you help with some code?


On Thu, Jan 9, 2014 at 7:02 PM, Dr Immo Benjes <[hidden email]> wrote:

> Hi Tom,
>
> this is for generating EDIFACT messages not parsing them. I need to set the UNT segment count before I generate the actual EDIFACT message.
>
> Regards
>
> Immo
>
> Dr. Immo Benjes
> Senior Software Architect
>
> Locus Software Ltd
> Castle Court
> 41 London Road
> Reigate
> Surrey RH2 9RJ
> United Kingdom
> Tel: +44 (0)1737 735012
> Web: www.locussoftware.co.uk
> Email: [hidden email]
>
>
>
>
> On 9 Jan 2014, at 16:47, Tom Fennelly <[hidden email]> wrote:
>
>> Hi Immo.
>>
>> I think I'd try find a way of hooking into the event stream generated by Smooks, and watch for new segment events and count based on that.  I would try to avoid using reflection.
>>
>> T.
>>
>> On 09/01/2014 16:41, Dr Immo Benjes wrote:
>>> Hi Tom,
>>>
>>> I was thinking of using reflection to calculate the segment count.
>>>
>>> Basically you travel the tree and increment the counter for each non-null segment you come across. The only problem is to identify whether a class is a segment (should be in package org.milyn.edi.unedifact.dXXX.common) or a field in a segment (should be in package org.milyn.edi.unedifact.dXXX.common.field).
>>> So judging by the package name seems to be the only way.
>>>
>>> Unfortunately there are no Interfaces the segments implement (could have been an empty interface).
>>>
>>> For future versions it might be nice to have messages implement one Interface, segments another one etc.
>>>
>>> Do you foresee any problems with this approach?
>>>
>>> Regards
>>>
>>> Immo
>>>   Dr. Immo Benjes
>>> Senior Software Architect
>>>
>>> Locus Software Ltd
>>> Castle Court
>>> 41 London Road
>>> Reigate
>>> Surrey RH2 9RJ
>>> United Kingdom
>>> Tel: +44 (0)1737 735012
>>> Web: www.locussoftware.co.uk
>>> Email: [hidden email]
>>>
>>>
>>>
>>>
>>> On 9 Jan 2014, at 16:30, Tom Fennelly <[hidden email]> wrote:
>>>
>>>> Hi.
>>>>
>>>> From what I remember, I don't think there is.  As it's just a model, it just populates from what it finds in the message i.e. doesn't "calculate" missing values.  You could try hooking up a debugger.
>>>>
>>>> Regards,
>>>>
>>>> Tom.
>>>>
>>>>
>>>> On 09/01/2014 16:24, Dr Immo Benjes wrote:
>>>>> Hi,
>>>>>
>>>>> I am using smooks 1.4  with the EDIFACT EJC classes.
>>>>> When you generate an EDIFACT message you have to set the segment count in the UNT segment at the end of the message.
>>>>> Is there any way to calculate the segment count automatically independent of the actual message?
>>>>>
>>>>>
>>>>> Btw I am still using 1.4 as it seems to be the only one with pre-build EJC EDIFACT classes as far as I know.
>>>>>
>>>>> Regards
>>>>>
>>>>> Immo
>>>>>   Dr. Immo Benjes
>>>>> Senior Software Architect
>>>>>
>>>>> Locus Software Ltd
>>>>> Castle Court
>>>>> 41 London Road
>>>>> Reigate
>>>>> Surrey RH2 9RJ
>>>>> United Kingdom
>>>>> Tel: +44 (0)1737 735012
>>>>> Web: www.locussoftware.co.uk
>>>>> Email: [hidden email]
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>     http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>   http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>     http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Ioan Eugen Stan
0720 898 747

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Dr Immo Benjes
Hi Ioan,

much appreciated.

Regards

Immo
 
Dr. Immo Benjes
Senior Software Architect

Locus Software Ltd
Castle Court
41 London Road
Reigate
Surrey RH2 9RJ
United Kingdom
Tel: +44 (0)1737 735012
Web: www.locussoftware.co.uk
Email: [hidden email]




On 10 Jan 2014, at 15:49, Ioan Eugen Stan <[hidden email]> wrote:

> Hello Immo,
>
> We also had that problem and our solution was to compute it. I've
> added Alex to the list. He wrote the code wit reflection. He's
> planning to add support for  segemnt counting in 1.6-SNAPSHOT but will
> take a while.
>
> @Alex: can you help with some code?
>
>
> On Thu, Jan 9, 2014 at 7:02 PM, Dr Immo Benjes <[hidden email]> wrote:
>> Hi Tom,
>>
>> this is for generating EDIFACT messages not parsing them. I need to set the UNT segment count before I generate the actual EDIFACT message.
>>
>> Regards
>>
>> Immo
>>
>> Dr. Immo Benjes
>> Senior Software Architect
>>
>> Locus Software Ltd
>> Castle Court
>> 41 London Road
>> Reigate
>> Surrey RH2 9RJ
>> United Kingdom
>> Tel: +44 (0)1737 735012
>> Web: www.locussoftware.co.uk
>> Email: [hidden email]
>>
>>
>>
>>
>> On 9 Jan 2014, at 16:47, Tom Fennelly <[hidden email]> wrote:
>>
>>> Hi Immo.
>>>
>>> I think I'd try find a way of hooking into the event stream generated by Smooks, and watch for new segment events and count based on that.  I would try to avoid using reflection.
>>>
>>> T.
>>>
>>> On 09/01/2014 16:41, Dr Immo Benjes wrote:
>>>> Hi Tom,
>>>>
>>>> I was thinking of using reflection to calculate the segment count.
>>>>
>>>> Basically you travel the tree and increment the counter for each non-null segment you come across. The only problem is to identify whether a class is a segment (should be in package org.milyn.edi.unedifact.dXXX.common) or a field in a segment (should be in package org.milyn.edi.unedifact.dXXX.common.field).
>>>> So judging by the package name seems to be the only way.
>>>>
>>>> Unfortunately there are no Interfaces the segments implement (could have been an empty interface).
>>>>
>>>> For future versions it might be nice to have messages implement one Interface, segments another one etc.
>>>>
>>>> Do you foresee any problems with this approach?
>>>>
>>>> Regards
>>>>
>>>> Immo
>>>>  Dr. Immo Benjes
>>>> Senior Software Architect
>>>>
>>>> Locus Software Ltd
>>>> Castle Court
>>>> 41 London Road
>>>> Reigate
>>>> Surrey RH2 9RJ
>>>> United Kingdom
>>>> Tel: +44 (0)1737 735012
>>>> Web: www.locussoftware.co.uk
>>>> Email: [hidden email]
>>>>
>>>>
>>>>
>>>>
>>>> On 9 Jan 2014, at 16:30, Tom Fennelly <[hidden email]> wrote:
>>>>
>>>>> Hi.
>>>>>
>>>>> From what I remember, I don't think there is.  As it's just a model, it just populates from what it finds in the message i.e. doesn't "calculate" missing values.  You could try hooking up a debugger.
>>>>>
>>>>> Regards,
>>>>>
>>>>> Tom.
>>>>>
>>>>>
>>>>> On 09/01/2014 16:24, Dr Immo Benjes wrote:
>>>>>> Hi,
>>>>>>
>>>>>> I am using smooks 1.4  with the EDIFACT EJC classes.
>>>>>> When you generate an EDIFACT message you have to set the segment count in the UNT segment at the end of the message.
>>>>>> Is there any way to calculate the segment count automatically independent of the actual message?
>>>>>>
>>>>>>
>>>>>> Btw I am still using 1.4 as it seems to be the only one with pre-build EJC EDIFACT classes as far as I know.
>>>>>>
>>>>>> Regards
>>>>>>
>>>>>> Immo
>>>>>>  Dr. Immo Benjes
>>>>>> Senior Software Architect
>>>>>>
>>>>>> Locus Software Ltd
>>>>>> Castle Court
>>>>>> 41 London Road
>>>>>> Reigate
>>>>>> Surrey RH2 9RJ
>>>>>> United Kingdom
>>>>>> Tel: +44 (0)1737 735012
>>>>>> Web: www.locussoftware.co.uk
>>>>>> Email: [hidden email]
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe from this list, please visit:
>>>>>>
>>>>>>    http://xircles.codehaus.org/manage_email
>>>>>>
>>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>  http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>  http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>
>
> --
> Ioan Eugen Stan
> 0720 898 747
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Alex Proca
Hello,

Short answer:
Because I needed a solution in a short period of time I used Groovy and reflection to count segments(maybe it cand be written in java but I was in a hurry). You can see here a gist with the method https://gist.github.com/alexproca/8360443

Long answer:
The solution to this problem is to modify the smooks ect compiler in order to generate code with a segment counting method. Right now the ect compiler generate 'write' methods like this:

'''

public void write(Writer writer, Delimiters delimiters) throws IOException {

        Writer nodeWriter = new StringWriter();

        List<String> nodeTokens = new ArrayList<String>();

        if(c516MonetaryAmount != null) {
            c516MonetaryAmount.write(nodeWriter, delimiters);
            nodeTokens.add(nodeWriter.toString());
            ((StringWriter)nodeWriter).getBuffer().setLength(0);
        }
        nodeTokens.add(nodeWriter.toString());
        writer.write(EDIUtils.concatAndTruncate(nodeTokens, DelimiterType.FIELD, delimiters));
        writer.write(delimiters.getSegmentDelimiter());
        writer.flush();
    }

'''

we could easily add a method like:

'''

public int count(int accumulator) {

        if(c516MonetaryAmount != null) {
            accumulator += 1;
        }
    return accumulator;
}

'''

or in a similar fashion but this will take a while because we do not have time right now
Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

Dr Immo Benjes
Hi,

here is my quick hack on it:
You need to add 2 to the returned result from getSegmentCount() for the UNH and UNT segment.
It is not ideal as it relies on package names to identify segments but without any additional interfaces this will have to do.
Only tested on one message type yet.


public class SegmentCounter {
       
        private static boolean isSegment(EDIWritable smooksObject){
                if(smooksObject.getClass().getPackage().getName().endsWith("common")){
                        return true;
                }
                return false;
        }
       
        private static boolean isField(EDIWritable smooksObject){
                if(smooksObject.getClass().getPackage().getName().endsWith("common.field")){
                        return true;
                }
                return false;
        }
       
        private static final Log log = Logging.getLog(SegmentCounter.class);
       
        public static int getSegmentCount(EDIWritable smooksObject) throws IllegalArgumentException, IllegalAccessException{
                int count = 0;
                log.info("Getting count for #0", smooksObject.getClass().getName());
                if(isSegment(smooksObject)){
                        log.info("Found segment #0", smooksObject.getClass().getName());
                        return 1;
                }else if(isField(smooksObject)){
                        return 0;
                }
                for(Field f :smooksObject.getClass().getDeclaredFields()){
                        f.setAccessible(true);
                        Object obj = f.get(smooksObject);
                        if(obj instanceof Collection){
                                Iterator iter = ((Collection)obj).iterator();
                                while(iter.hasNext()){
                                        Object sobj = iter.next();
                                        if(sobj instanceof EDIWritable){
                                                count += getSegmentCount((EDIWritable)sobj);
                                        }
                                }
                        }else if(obj instanceof EDIWritable){
                                count += getSegmentCount((EDIWritable)obj);
                        }
                }
                log.info("Count for #0 is #1", smooksObject.getClass().getName(),count);
                return count;
        }

}


Regards

Immo
 
Dr. Immo Benjes
Senior Software Architect

Locus Software Ltd
Castle Court
41 London Road
Reigate
Surrey RH2 9RJ
United Kingdom
Tel: +44 (0)1737 735012
Web: www.locussoftware.co.uk
Email: [hidden email]




On 15 Jan 2014, at 10:36, Alex Proca <[hidden email]> wrote:

> Hello,
>
> Short answer:
> Because I needed a solution in a short period of time I used Groovy and
> reflection to count segments(maybe it cand be written in java but I was in a
> hurry). You can see here a gist with the method
> https://gist.github.com/alexproca/8360443
>
> Long answer:
> The solution to this problem is to modify the smooks ect compiler in order
> to generate code with a segment counting method. Right now the ect compiler
> generate 'write' methods like this:
>
> '''
>
> public void write(Writer writer, Delimiters delimiters) throws IOException {
>
>        Writer nodeWriter = new StringWriter();
>
>        List<String> nodeTokens = new ArrayList<String>();
>
>        if(c516MonetaryAmount != null) {
>            c516MonetaryAmount.write(nodeWriter, delimiters);
>            nodeTokens.add(nodeWriter.toString());
>            ((StringWriter)nodeWriter).getBuffer().setLength(0);
>        }
>        nodeTokens.add(nodeWriter.toString());
>        writer.write(EDIUtils.concatAndTruncate(nodeTokens,
> DelimiterType.FIELD, delimiters));
>        writer.write(delimiters.getSegmentDelimiter());
>        writer.flush();
>    }
>
> '''
>
> we could easily add a method like:
>
> '''
>
> public int count(int accumulator) {
>
>        if(c516MonetaryAmount != null) {
>            accumulator += 1;
>        }
>    return accumulator;
> }
>
> '''
>
> or in a similar fashion but this will take a while because we do not have
> time right now
>
>
>
> --
> View this message in context: http://milyn.996300.n3.nabble.com/Calculating-segment-count-for-UNT-in-EDIFACT-messages-tp8772p8783.html
> Sent from the milyn - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Calculating segment count for UNT in EDIFACT messages

niels
In reply to this post by Ioan Eugen Stan
Hey Ioan,

did you guys managed to get something working? I'm curious to see which progress you made because I'm also looking into enabling the segment counter (UNT field).

I was trying to see if it was added to the latest 1.6.0 release but I couldn't find any release notes anywhere.