class SedaConverterTool {

    public static Unit convertUnitToSeda21(Unit unit) {

        for(ObjectFieldsToDelete objectWrapperFieldsDelete : ObjectFieldsToDelete.values()) {
            //remove first level fields
            if(ObjectFieldsToDelete.Root.compareTo(objectWrapperFieldsDelete) == 0){
                for(String field : objectWrapperFieldsDelete.getFields()){
                    if(unit.get(field) != null) {
                        unit.remove(field);
                    }
                }
            }
            //remove embedded fields
            Object object = unit.get(objectWrapperFieldsDelete.name());
            if (object != null && object instanceof  Document) {
                Document document = (Document)object;
                for(String field : objectWrapperFieldsDelete.getFields()){
                    if(document.get(field) != null) {
                        document.remove(field);
                    }
                }
            }
        }
        //update fields
        for(FieldsTreeToTransformToList firstLevelField : FieldsTreeToTransformToList.values()) {
            Object object = unit.get(firstLevelField.name());
            if (object != null && !object.getClass().isArray() &&
                !object.getClass().isAssignableFrom(List.class) &&
                !object.getClass().isAssignableFrom(ArrayList.class)) {

                if(object instanceof  Document &&
                    firstLevelField.getFields() != null &&
                    firstLevelField.getFields().length > 0){

                    Document document = (Document)object;
                    for(String subField : firstLevelField.getFields()) {
                        Object subFieldValue = document.get(subField);
                        if(subFieldValue != null){
                               document.replace(subField, Arrays.asList(subFieldValue));
                        }
                    }
                }

                unit.replace(firstLevelField.name(), Arrays.asList(object));
            }
        }

        return unit;
    }

}