/* SPDX-License-Identifier: Apache-2.0 */

// This is a generated file - do not edit - changes should be made to the templates amd/or generator to generate this file with changes.

package org.odpi.openmetadata.accessservices.subjectarea.generated.relationships.<$$uRelationshipName$$>;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.*;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.*;

import org.odpi.openmetadata.accessservices.subjectarea.generated.relationships.<$$uRelationshipName$$>.<$$uRelationshipName$$>;
import org.odpi.openmetadata.accessservices.subjectarea.properties.enums.*;
import org.odpi.openmetadata.accessservices.subjectarea.ffdc.exceptions.InvalidParameterException;
import org.odpi.openmetadata.accessservices.subjectarea.properties.enums.Status;
import org.odpi.openmetadata.accessservices.subjectarea.properties.objects.common.SystemAttributes;
import org.odpi.openmetadata.accessservices.subjectarea.properties.objects.line.Line;
import org.odpi.openmetadata.accessservices.subjectarea.utilities.SubjectAreaUtils;

import java.util.*;

/**
 * Static mapping methods to map between <$$uRelationshipName$$> and the omrs Relationship.
 */
public class <$$uRelationshipName$$>Mapper {
       private static final Logger log = LoggerFactory.getLogger(<$$uRelationshipName$$>Mapper.class);
       private static final String className = <$$uRelationshipName$$>Mapper.class.getName();

       public static <$$uRelationshipName$$> mapOmrsRelationshipTo<$$uRelationshipName$$>(Relationship omrsRelationship)  throws InvalidParameterException {
     
               if ("<$$uRelationshipName$$>".equals(omrsRelationship.getType().getTypeDefName())) {
                   <$$uRelationshipName$$> <$$RelationshipName$$> = new <$$uRelationshipName$$>(omrsRelationship);

                   SystemAttributes systemAttributes = new SystemAttributes();
                   InstanceStatus instanceStatus =  omrsRelationship.getStatus();
                   Status omrsBeanStatus = SubjectAreaUtils.convertInstanceStatusToStatus(instanceStatus);
                   systemAttributes.setStatus(omrsBeanStatus);
                   systemAttributes.setCreatedBy(omrsRelationship.getCreatedBy());
                   systemAttributes.setUpdatedBy(omrsRelationship.getUpdatedBy());
                   systemAttributes.setCreateTime(omrsRelationship.getCreateTime());
                   systemAttributes.setUpdateTime(omrsRelationship.getUpdateTime());
                   systemAttributes.setVersion(omrsRelationship.getVersion());
                   systemAttributes.setGUID(omrsRelationship.getGUID());
                   <$$RelationshipName$$>.setSystemAttributes(systemAttributes);

                   InstanceProperties omrsRelationshipProperties = omrsRelationship.getProperties();
                   Iterator omrsPropertyIterator = omrsRelationshipProperties.getPropertyNames();
                   while (omrsPropertyIterator.hasNext()) {
                       String name = (String) omrsPropertyIterator.next();
                       //TODO check if this is a property we expect or whether the type has been added to.
                       // this is a property we expect
                       InstancePropertyValue value = omrsRelationshipProperties.getPropertyValue(name);
                       // supplied guid matches the expected type
                       Object actualValue;
                       switch (value.getInstancePropertyCategory()) {
                                               case PRIMITIVE:
                                                   PrimitivePropertyValue primitivePropertyValue = (PrimitivePropertyValue) value;
                                                   actualValue = primitivePropertyValue.getPrimitiveValue();
                                                   if (<$$uRelationshipName$$>.ATTRIBUTE_NAMES_SET.contains(name)) {
                                                      <$$Attr$$
                                                      if (name.equals("<$$AttrName$$>")) {
                                                           <$$RelationshipName$$>.set<$$uAttrName$$>((<$$AttrType$$>)actualValue);
                                                      }
                                                      $$Attr$$>
                       
                                                   } else {
                                                       // put out the omrs value object
                                                       if (<$$RelationshipName$$>.getExtraAttributes() ==null) {
                                                            Map<String, Object> extraAttributes = new HashMap();
                                                            <$$RelationshipName$$>.setExtraAttributes(extraAttributes);
                                                        }
                                                      <$$RelationshipName$$>.getExtraAttributes().put(name, primitivePropertyValue);
                                                   }
                                                   break;
                                               case ENUM:
                                                   EnumPropertyValue enumPropertyValue = (EnumPropertyValue) value;
                                                   String symbolicName = enumPropertyValue.getSymbolicName();
                                                   if (<$$uRelationshipName$$>.ENUM_NAMES_SET.contains(name)) {
                                                       <$$Enum$$
                                                        if (name.equals("<$$EnumName$$>")) {
                                                              org.odpi.openmetadata.accessservices.subjectarea.properties.enums.<$$uEnumType$$> <$$EnumName$$> = org.odpi.openmetadata.accessservices.subjectarea.properties.enums.<$$uEnumType$$>.valueOf(symbolicName);
                                                             <$$RelationshipName$$>.set<$$uEnumName$$>(<$$EnumName$$>);
                                                        }
                                                       $$Enum$$>
                       
                                                   } else {
                                                       // put out the omrs value object
                                                        if (<$$RelationshipName$$>.getExtraAttributes() ==null) {
                                                            Map<String, Object> extraAttributes = new HashMap();
                                                            <$$RelationshipName$$>.setExtraAttributes(extraAttributes);
                                                        }

                                                        <$$RelationshipName$$>.getExtraAttributes().put(name, enumPropertyValue);
                                                    }
                       
                                                   break;
                                               case MAP:
                                                    if (<$$RelationshipName$$>.MAP_NAMES_SET.contains(name)) {
                                                        MapPropertyValue mapPropertyValue = (MapPropertyValue) value;
                                                        InstanceProperties instancePropertyForMap = (InstanceProperties) mapPropertyValue.getMapValues();

                                                        <$$Map$$
                                                        if (name.equals("<$$MapName$$>")) {

                                                             // Only support Map<String,String> as that is what is in the archive types at this time.
                                                             Map<String, String> actualMap = new HashMap();
                                                             Iterator iter = instancePropertyForMap.getPropertyNames();
                                                             while (iter.hasNext()) {
                                                                String mapkey = (String) iter.next();
                                                                PrimitivePropertyValue primitivePropertyMapValue = (PrimitivePropertyValue) instancePropertyForMap.getPropertyValue(mapkey);
                                                                String mapvalue = (String) primitivePropertyMapValue.getPrimitiveValue();
                                                                actualMap.put(mapkey, mapvalue);
                                                             }
                                                             <$$RelationshipName$$>.set<$$uMapName$$>(actualMap);
                                                         }
                                                        $$Map$$>

                                                    }
                                                    break;
                                               case ARRAY:
                                               case STRUCT:
                                               case UNKNOWN:
                                                   // error
                                                   break;
                                           }
                       
                                       }   // end while
                                       return <$$RelationshipName$$>;

                   } else {
                       // TODO wrong type for this guid
                   }
                   return null;
       }
       public static Relationship map<$$uRelationshipName$$>ToOmrsRelationship(<$$uRelationshipName$$> <$$RelationshipName$$>) {
           Relationship omrsRelationship = Line.createOmrsRelationship(<$$RelationshipName$$>);

           SystemAttributes systemAttributes = <$$RelationshipName$$>.getSystemAttributes();
           if (systemAttributes!=null) {
               if (systemAttributes.getCreatedBy()!=null)
                   omrsRelationship.setCreatedBy(systemAttributes.getCreatedBy());
               if (systemAttributes.getUpdatedBy()!=null)
                   omrsRelationship.setUpdatedBy(systemAttributes.getUpdatedBy());
               if (systemAttributes.getCreateTime()!=null)
                   omrsRelationship.setCreateTime(systemAttributes.getCreateTime());
               if (systemAttributes.getUpdateTime()!=null)
                   omrsRelationship.setUpdateTime(systemAttributes.getUpdateTime());
               if (systemAttributes.getVersion()!=null)
                   omrsRelationship.setVersion(systemAttributes.getVersion());
               if (systemAttributes.getStatus()!=null) {
                   InstanceStatus instanceStatus = SubjectAreaUtils.convertStatusToStatusInstance(systemAttributes.getStatus());
                   omrsRelationship.setStatus(instanceStatus);
               }
           }

           InstanceProperties instanceProperties = new InstanceProperties();
           // primitives

            <$$Attr$$
            if (<$$RelationshipName$$>.get<$$uAttrName$$>()!=null) {
                PrimitivePropertyValue primitivePropertyValue = new PrimitivePropertyValue();
                primitivePropertyValue.setPrimitiveDefCategory(PrimitiveDefCategory.OM_PRIMITIVE_TYPE_<$$uAttrType$$>);
                primitivePropertyValue.setPrimitiveValue(<$$RelationshipName$$>.get<$$uAttrName$$>());
                instanceProperties.setProperty("<$$AttrName$$>", primitivePropertyValue);
            }
            $$Attr$$>

            <$$Enum$$
            if (<$$RelationshipName$$>.get<$$uEnumName$$>()!=null) {
                <$$uEnumType$$> enumType = <$$RelationshipName$$>.get<$$uEnumName$$>();
                EnumPropertyValue enumPropertyValue = new EnumPropertyValue();
                enumPropertyValue.setOrdinal(enumType.ordinal());
                enumPropertyValue.setSymbolicName(enumType.name());
                instanceProperties.setProperty("<$$EnumName$$>", enumPropertyValue);
            }
            $$Enum$$>

            omrsRelationship.setProperties(instanceProperties);

           return omrsRelationship;
       }
}
