2626import org .openlowcode .server .data .properties .multichild .MultidimensionchildHelper ;
2727
2828/**
29- * A helper to manage the loading of objects with the Hasmultidimensionalchild property
29+ * A helper to manage the loading of objects with the Hasmultidimensionalchild
30+ * property
3031 *
3132 * @author <a href="https://openlowcode.com/" rel="nofollow">Open Lowcode
3233 * SAS</a>
@@ -62,11 +63,11 @@ public void setPayloadHelper(MultichildValueHelper<F, ?, E> payloadhelper) {
6263 public MultichildValueHelper <F , ?, E > getPayloadHelper () {
6364 return this .payloadhelper ;
6465 }
65-
66+
6667 /**
6768 * @return
6869 */
69- public MultichildValueHelper <F ,?, E > getMainValueHelper () {
70+ public MultichildValueHelper <F , ?, E > getMainValueHelper () {
7071 return this .mainvaluehelper ;
7172 }
7273
@@ -82,8 +83,6 @@ public HasmultidimensionalchildFlatFileLoaderHelper(
8283 payloadhelper = helper .getPayloadValueHelper ();
8384 }
8485
85-
86-
8786 @ Override
8887 public FlatFileLoaderColumn <E > getFlatFileLoaderColumn (
8988 DataObjectDefinition <E > objectdefinition ,
@@ -108,23 +107,26 @@ public FlatFileLoaderColumn<E> getFlatFileLoaderColumn(
108107 if (columnattributes .length >= 2 ) {
109108 if (maincolumnattribute .equals (this .mainvaluehelper .getFieldName ())) {
110109 String maincolumnvalue = columnattributes [1 ];
111- String [] extraattributes =null ;
112- if (columnattributes .length >2 ) {
113- extraattributes = new String [columnattributes .length -2 ];
114- for (int i =0 ;i <columnattributes .length -2 ;i ++) extraattributes [i ] = columnattributes [i +2 ];
110+ String [] extraattributes = null ;
111+ if (columnattributes .length > 2 ) {
112+ extraattributes = new String [columnattributes .length - 2 ];
113+ for (int i = 0 ; i < columnattributes .length - 2 ; i ++)
114+ extraattributes [i ] = columnattributes [i + 2 ];
115115 }
116-
117- return mainvaluehelper .new MainValueFlatFileLoader (this , locale , maincolumnvalue ,payloadhelper ,extraattributes );
116+
117+ return mainvaluehelper .new MainValueFlatFileLoader (this , locale , maincolumnvalue , payloadhelper ,
118+ extraattributes );
118119 }
119120 }
120121 if (maincolumnattribute .equals ("#TOTAL#" )) {
121122 logger .finest (" >>> get total flat file loader" );
122- String [] extraattributes =null ;
123- if (columnattributes .length >1 ) {
124- extraattributes = new String [columnattributes .length -1 ];
125- for (int i =0 ;i <columnattributes .length -1 ;i ++) extraattributes [i ] = columnattributes [i +1 ];
123+ String [] extraattributes = null ;
124+ if (columnattributes .length > 1 ) {
125+ extraattributes = new String [columnattributes .length - 1 ];
126+ for (int i = 0 ; i < columnattributes .length - 1 ; i ++)
127+ extraattributes [i ] = columnattributes [i + 1 ];
126128 }
127- return this .payloadhelper .new MainValueTotalFlatFileLoader (this ,locale ,payloadhelper ,extraattributes );
129+ return this .payloadhelper .new MainValueTotalFlatFileLoader (this , locale , payloadhelper , extraattributes );
128130 }
129131 potentialattributes .append (',' );
130132 potentialattributes .append (mainvaluehelper .getFieldName ());
@@ -160,8 +162,8 @@ public void executeAtEndOfLine(E objecforprocessing) {
160162
161163 }
162164
163- public String getDebugForLineAndColumnKey (String linekey ,String columnkey ) {
164- if (childrenbykey .get (linekey )== null ) {
165+ public String getDebugForLineAndColumnKey (String linekey , String columnkey ) {
166+ if (childrenbykey .get (linekey ) == null ) {
165167 StringBuffer droplinekey = new StringBuffer (" Line key not existing, given value = " );
166168 droplinekey .append (linekey );
167169 droplinekey .append (", valid keys = " );
@@ -184,18 +186,18 @@ public String getDebugForLineAndColumnKey(String linekey,String columnkey) {
184186 droplinekey .append (' ' );
185187 }
186188 return droplinekey .toString ();
187-
188-
189+
189190 }
190-
191+
191192 public ArrayList <F > getChildrenForLine (String linekey ) {
192193 Iterator <F > values = childrenbykey .get (linekey ).values ().iterator ();
193194 ArrayList <F > valueslist = new ArrayList <F >();
194- while (values .hasNext ()) valueslist .add (values .next ());
195+ while (values .hasNext ())
196+ valueslist .add (values .next ());
195197 return valueslist ;
196-
198+
197199 }
198-
200+
199201 public F getChildForLineAndColumnKey (String linekey , String columnkey ) {
200202 if (childrenbykey == null ) {
201203 StringBuffer error = new StringBuffer ("Children list not initiated for linekey = " + linekey
@@ -219,57 +221,63 @@ public F getFirstChildForLineKey(String key) {
219221 return childrenbykey .get (key ).values ().iterator ().next ();
220222 }
221223
224+ private void putChildInKeyMap (E currentobject ,F child ) {
225+ String keyforchild = this .helper .generateKeyForObject (child , true );
226+
227+ HashMap <String , F > currentobjectsforkey = childrenbykey .get (keyforchild );
228+ if (currentobjectsforkey == null ) {
229+ currentobjectsforkey = new HashMap <String , F >();
230+ childrenbykey .put (keyforchild , currentobjectsforkey );
231+ logger .finest ("--- adding child map for key " + keyforchild );
232+ }
233+ String mainvalue = this .helper .getMainValueHelper ().getAndPrint (child );
234+ F current = currentobjectsforkey .get (mainvalue );
235+ if (current != null )
236+ logger .warning (" -- Duplicate child for secondary key = " + keyforchild + " for main value "
237+ + mainvalue + " for object = " + currentobject .dropIdToString ());
238+ if (current == null ) {
239+ currentobjectsforkey .put (mainvalue , child );
240+ logger .finest (" adding element for primary " + keyforchild + " secondary " + mainvalue + " for id "
241+ + child .dropIdToString ());
242+ }
243+ }
244+
222245 public void initColumnsForObject (E currentobject ) {
223246 F [] children = hasmultidimensionalchilddefinition .getChildren (currentobject .getId ());
224- logger .finest (" ---------------- Generating keys for " +currentobject .dropIdToString ()+" - " +(children !=null ?children .length :"NULL" )+" child(ren)---------------------------" );
247+ logger .finest (" ---------------- Generating keys for " + currentobject .dropIdToString () + " - "
248+ + (children != null ? children .length : "NULL" ) + " child(ren)---------------------------" );
225249 childrenbykey = new HashMap <String , HashMap <String , F >>();
226250 for (int i = 0 ; i < children .length ; i ++) {
227251 F child = children [i ];
228- String keyforchild = this .helper .generateKeyForObject (child , true );
229-
230- HashMap <String , F > currentobjectsforkey = childrenbykey .get (keyforchild );
231- if (currentobjectsforkey == null ) {
232- currentobjectsforkey = new HashMap <String , F >();
233- childrenbykey .put (keyforchild , currentobjectsforkey );
234- logger .finest ("--- adding child map for key " +keyforchild );
235- }
236- String mainvalue = this .helper .getMainValueHelper ().getAndPrint (child );
237- F current = currentobjectsforkey .get (mainvalue );
238- if (current != null )
239- logger .warning (" -- Duplicate child for secondary key = " + keyforchild + " for main value "
240- + mainvalue + " for object = " + currentobject .dropIdToString ());
241- if (current == null ) {
242- currentobjectsforkey .put (mainvalue , child );
243- logger .finest (" adding element for primary " +keyforchild +" secondary " +mainvalue +" for id " +child .dropIdToString ());
244- }
252+ this .putChildInKeyMap (currentobject , child );
245253 }
246254
247255 }
248256
249257 public String [] generateKeyAndLoadExistingData (E currentobject ) {
250-
258+
251259 // Find why two logics with children in initColumn and method below
252260 initColumnsForObject (currentobject );
253261 F [] children = hasmultidimensionalchilddefinition .getChildren (currentobject .getId ());
254- HashMap <String ,String > classificationkeys = new HashMap <String ,String >();
262+ HashMap <String , String > classificationkeys = new HashMap <String , String >();
255263 for (int i = 0 ; i < children .length ; i ++) {
256264 F child = children [i ];
257265 String keyforchild = this .helper .generateKeyForObject (child , true );
258- classificationkeys .put (keyforchild ,keyforchild );
266+ classificationkeys .put (keyforchild , keyforchild );
259267 }
260268 Set <String > keys = classificationkeys .keySet ();
261269 Iterator <String > keyiterator = keys .iterator ();
262270 logger .finest (" ---------------- key drop -------------------" );
263- while (keyiterator .hasNext ()) logger .finest (" key " +keyiterator .next ());
271+ while (keyiterator .hasNext ())
272+ logger .finest (" key " + keyiterator .next ());
264273 logger .finest (" ----------------------------------------" );
265274 return keys .toArray (new String [0 ]);
266275
267276 }
268277
269278 private ArrayList <String > secondaryvalues = new ArrayList <String >();
270279 private E contextobject = null ;
271-
272-
280+
273281 public void setSecondaryValueForLoading (int index , String string ) {
274282 if (secondaryvalues .size () <= index )
275283 for (int i = secondaryvalues .size (); i < index + 1 ; i ++)
@@ -287,17 +295,66 @@ public String getContextKey() {
287295 }
288296
289297 public void setContext (E object ) {
290- boolean same = false ;
291- if (contextobject != null ) {
298+ boolean same = false ;
299+ if (contextobject != null ) {
292300 if (object .getId ().equals (contextobject .getId ())) {
293- same = true ;
301+ same = true ;
294302 }
295303 }
296304 if (!same ) {
297- contextobject = object ;
305+ contextobject = object ;
298306 this .initColumnsForObject (contextobject );
299307 }
300-
308+
309+ }
310+
311+ /**
312+ * generates a new row of objects for the given context, and inserts them.
313+ *
314+ * @param parent parent object for generating new row
315+ * @param extraattributes
316+ * @param applocale
317+ * @return true if the new row for context could be generated, false else
318+ */
319+ public boolean generateNewRowForContext (
320+ E parent ,
321+ ChoiceValue <ApplocaleChoiceDefinition > applocale ,
322+ String [] extraattributes ) {
323+ boolean iscontextvalid = true ;
324+ logger .fine ("---------------------- generate new row for context ----------------" );
325+ for (int i = 0 ; i < secondaryvalues .size (); i ++) {
326+ String value = secondaryvalues .get (i );
327+ logger .fine (" > Evaluating value " +value );
328+ MultichildValueHelper <F , ?, E > helper = secondvaluehelpers .get (i );
329+ boolean valid = helper .isTextValid (value , applocale , extraattributes );
330+ if (!valid ) {
331+ logger .fine (" !!! INVALID !!! " );
332+ iscontextvalid = false ;
333+ }
334+ }
335+ if (!iscontextvalid )
336+ return false ;
337+ DataObjectDefinition <F > childdefinition = this .hasmultidimensionalchilddefinition
338+ .getRelatedDefinitionLinkedFromChildren ().getChildObjectDefinition ();
339+ F firstblank = childdefinition .generateBlank ();
340+ for (int i = 0 ; i < secondaryvalues .size (); i ++) {
341+ String value = secondaryvalues .get (i );
342+ MultichildValueHelper <F , ?, E > helper = secondvaluehelpers .get (i );
343+ helper .fillWithValue (firstblank , value , applocale , extraattributes );
344+ }
345+ logger .fine (" -> First blank = " +firstblank .dropToString ());
346+ ArrayList <F > newobjects = mainvaluehelper .generateElementsForAllMandatory (firstblank ,parent );
347+ logger .fine ("Generating elements number = " +(newobjects ==null ?"null" :newobjects .size ()));
348+ if (newobjects !=null ) if (newobjects .size ()>0 ) {
349+ for (int i =0 ;i <newobjects .size ();i ++) {
350+ F newobject = newobjects .get (i );
351+ newobject .setmultidimensionparentidwithoutupdate (parent .getId ());
352+ putChildInKeyMap (parent ,newobject );
353+ }
354+ newobjects .get (0 ).getMassiveInsert ().insert (newobjects .toArray (childdefinition .generateArrayTemplate ()));
355+
356+ }
357+ return true ;
301358 }
302359
303360}
0 commit comments