Package ch.tocco.nice2.outputcenter.api.sorting


package ch.tocco.nice2.outputcenter.api.sorting
Contains classes to implement the sorting of merged output jobs.

An output job is personalized or not personalized. Personalized jobs have a set of output job items, where as an unpersonalized job solely consists of the document.

So a list of output jobs may look like this:

     job1 → 2
       item1(addr_str1!, u?, addr?, doc)
       item2(addr_str3!, u?, addr?, doc)
       item3(addr_str3!, u?, addr?, doc)
     job2 → 1
       item1(addr_str2!, u?, addr?, doc)
       item2(addr_str1!, u?, addr?, doc)
       item3(addr_str3!, u?, addr?, doc)
     job3 → 4 (doc)
     job4 → 3
       item1(addr_str2!, u?, addr?, doc)
       item2(addr_str1!, u?, addr?, doc)
       item3(addr_str1!, u?, addr?, doc)
 
All but the job3 are personalized jobs. The job3 only has a document and no other information.

The addr_str is the Address as String field, that denotes the address that would finally be printed on the "envelope". Thus it is now refered to as recipient_address. The u? and addr? are an optional linked user or address entity that is only available for Output_job_items. The number behind the arrow (→ 3) denotes the manual sorting as given by the user that submitted a merge request. Since unpersonalized jobs have no properties, i.e. no receiver or address, they are send to all existing recipient_address (put in all envelopes).

There may be documents regarding different persons (linked users -- the u?) but all in the same envelope. Think for example of invoices for multiple employees send to their firm address. In this case the envelope address is the address of the company, but it contains documents regarding multiple persons.

Additionally, the user can add documents from the DMS and attach them to a single job or to the whole merge process. If a DMS document is added to a job, the document goes to all recipient_address that are defined in the job. If the document is added to the whole, it goes to all recipient_address of all jobs (same way as for unpersonalized jobs, but this time without sorting information).

The sorting affects once the order of the "envelopes" and second the order of the documents within an envelope.

Order of envelopes

The set of envelopes is divided into two disjoint sets:

 1. any envelopes containing documents linked to the same single user
 2. all others
 
The first set is sorted by lastname, firstname of the single user and the second set is sorted by the recipient_address field and then concatenated to the first set.

Creation of envelopes

As said, the envelope is defined by the recipient_address field. Thus, the whole tree must be grouped by this field. For the example above, this would result in something like this:

     job1 → 2
       item1(addr_str1!, u?, addr?, doc)
     job4 → 3
       item2(addr_str1!, u?, addr?, doc)
     job4 → 3
       item3(addr_str1!, u?, addr?, doc)
     job2 → 1
       item2(addr_str1!, u?, addr?, doc)
     job3 → 4 (doc)

     job1 → 2
       item2(addr_str3!, u?, addr?, doc)
     job1 → 2
       item3(addr_str3!, u?, addr?, doc)
     job2 → 1
       item3(addr_str3!, u?, addr?, doc)
     job3 → 4 (doc)

     job2 → 1
       item1(addr_str2!, u?, addr?, doc)
     job4 → 3
       item1(addr_str2!, u?, addr?, doc)
     job3 → 4 (doc)
 
There are now 3 envelopes and the unpersonalized job that is added to all envelopes.

Order of documents

Inside each envelope, the items are at first sorted as specified by the user. The user defined a manual sorting for each of the documents which is written as → [num] above. Applying this to the example results the following:

     job2 → 1
       item2(addr_str1!, u?, addr?, doc)
     job1 → 2
       item1(addr_str1!, u?, addr?, doc)
     job4 → 3
       item2(addr_str1!, u?, addr?, doc)
     job4 → 3
       item3(addr_str1!, u?, addr?, doc)
     job3 → 4 (doc)

     job2 → 1
       item3(addr_str3!, u?, addr?, doc)
     job1 → 2
       item2(addr_str3!, u?, addr?, doc)
     job1 → 2
       item3(addr_str3!, u?, addr?, doc)
     job3 → 4 (doc)

     job2 → 1
       item1(addr_str2!, u?, addr?, doc)
     job4 → 3
       item1(addr_str2!, u?, addr?, doc)
     job3 → 4 (doc)
 
This is the user specified order. But it may not be deterministic, because of some ambiguities that occur if there are jobs with items with equal envelope addresses. In the first envelope in the example, the items in job4 could be swapped without breaking the user defined ordering. So there are a few more restrictions that apply:

 1. if the documents are linked to different users, they are sorted within this
    group by lastname, firstname of the corresponding user.
 2. If it is the same user, the sorting is undefined but must be deterministic.