package ca.firstvoices.services;

import java.util.ArrayList;
import java.util.Arrays;

import org.nuxeo.ecm.core.api.DocumentModel;

public abstract class AbstractService {

    protected DocumentModel getDialect(DocumentModel doc) {
        DocumentModel parent = doc;
        while (parent != null && !"FVDialect".equals(parent.getType())) {
            parent = doc.getCoreSession().getDocument(parent.getParentRef());
        }
        return parent;
    }

    protected ArrayList<String> getPropertyValueAsArray(DocumentModel input, String dependency) {

    	ArrayList<String> propertyValueArray = new ArrayList<String>();

	    try {
	    	String[] propertyValues = (String[]) input.getPropertyValue(dependency);

	    	if (propertyValues != null) {
	    		propertyValueArray = new ArrayList<String>(Arrays.asList(propertyValues));
	    	}
	    }
	    // Convert a string to a string array for simplicity
	    catch (ClassCastException e) {
	    	propertyValueArray = new ArrayList<String>();
	    	propertyValueArray.add((String) input.getPropertyValue(dependency));
	    }

	    return propertyValueArray;
    }
}

@@ -240,34 +240,184 @@
     
-DocumentModel parent = doc
+if (%22FVDialect%22.equals(doc.getType())) %7B%0A            return doc; // doc is dialect%0A        %7D%0A        DocumentModel parent = doc.getCoreSession().getParentDocument(doc.getRef())
 ;%0A  
@@ -533,16 +533,22 @@
 on().get
+Parent
 Document
@@ -562,20 +562,79 @@
 .get
-Parent
 Ref())
+;%0A        %7D%0A        if (parent == null) %7B%0A            return null
 ;%0A  
@@ -774,17 +774,20 @@
  %7B%0A%0A    
-%09
+    
 ArrayLis
@@ -839,25 +839,28 @@
 String%3E();%0A%0A
-%09
+    
     try %7B%0A%09 
@@ -857,22 +857,28 @@
   try %7B%0A
-%09
+        
     
-%09
 String%5B%5D
@@ -943,22 +943,28 @@
 ency);%0A%0A
-%09
+   
     
-%09
+     
 if (prop
@@ -989,15 +989,24 @@
 ) %7B%0A
-%09
+          
     
-%09%09
+  
 prop
@@ -1080,24 +1080,36 @@
 ));%0A
-%09
+        
     
-%09
 %7D%0A
-%09
+    
     %7D%0A
-%09
+    
     
@@ -1161,17 +1161,20 @@
 plicity%0A
-%09
+    
     catc
@@ -1200,22 +1200,28 @@
 on e) %7B%0A
-%09
+   
     
-%09
+     
 property
@@ -1262,14 +1262,20 @@
 ();%0A
-%09
+   
     
-%09
+     
 prop
@@ -1343,17 +1343,23 @@
 ));%0A
-%09
+    
     %7D%0A%0A
-%09
+    
     

Fix iterating on parent