package eu.dnetlib.espas.data.harvest;

import java.util.Formatter;
import java.util.LinkedHashMap;
import java.util.Map;

public class Subject
{
   // "dc:subhect" prefix
   public static final String SUBJECT_DC_PREFIX = "dc";
   // "ows:BoundingBox" element name.
   public static final String SUBJECT_ELEMENT_NAME = "subject";
   // "dc:subject" element attributes.
   public static final String ATT_SCHEME = "scheme";

   private String subject = null;
   Map<String, String> subjectAttributes = null;

   public Subject()
   {
      this.subjectAttributes = new LinkedHashMap<String, String>();
   }

   @Override
   public String toString()
   {
	  String _toString = null;
      Formatter formatter = new Formatter();
      formatter.format("<%s:%s", SUBJECT_DC_PREFIX, SUBJECT_ELEMENT_NAME);
      for(String key : this.subjectAttributes.keySet())
      {
         formatter.format(" %s=\"%s\"", key, this.subjectAttributes.get(key));
      }
      formatter.format(">%s", this.subject);
      formatter.format("</%s:%s>", SUBJECT_DC_PREFIX, SUBJECT_ELEMENT_NAME);
      _toString = formatter.toString();
      formatter.close();
      return _toString;
   }

   @Override
   public boolean equals(Object object)
   {
      boolean equals = true;
      if(this == object)
      {
         return true;
      }
      if(!(object instanceof Subject))
      {
         return false;
      }
      Subject record = (Subject)object;

      if(null != this.subjectAttributes && !(equals = equals && this.subjectAttributes.equals(record.getSubjectAttributes())))
      {
         return equals;
      }
      else if(!(equals = equals && !(null == this.subjectAttributes && null != record.getSubjectAttributes())))
      {
         return equals;
      }

      if(null != this.subject && !(equals = equals && this.subject.equalsIgnoreCase(record.getSubject())))
      {
         return equals;
      }
      else if(!(equals = equals && !(null == this.subject && null != record.getSubject())))
      {
         return equals;
      }

      return equals;
   }

   public String getSubject()
   {
      return subject;
   }

   public void setSubject(String subject)
   {
      this.subject = subject;
   }

   public Map<String, String> getSubjectAttributes()
   {
      return subjectAttributes;
   }

   public String getSubjectAttributeValue(String attributeName)
   {
      return this.subjectAttributes.get(attributeName);
   }

   public void setSubjectAttributes(Map<String, String> subjectAttributes)
   {
      this.subjectAttributes = subjectAttributes;
   }

   public void put(String attributeName, String attributeValue)
   {
      this.subjectAttributes.put(attributeName, attributeValue);
   }
}
