All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.vgregion.mobile.hriv.utils.KivwsUnitMapper Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2010 Västra Götalandsregionen
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 */

package se.vgregion.mobile.hriv.utils;

import se.vgregion.mobile.hriv.domain.KivwsAttributes;
import se.vgregion.mobile.hriv.domain.OfficeHours;
import se.vgregion.mobile.hriv.domain.Unit;
import se.vgregion.mobile.hriv.domain.WeekdayTime;
import se.vgregion.mobile.hriv.kivws.Function;
import se.vgregion.mobile.hriv.kivws.String2ArrayOfAnyTypeMap.Entry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Mapp kivws response object to Unit.
 * 
 * @author david bennehult
 * 
 */
public class KivwsUnitMapper {

  private Map> ldapAttributes = new HashMap>();

  public Unit mapFromContext(se.vgregion.mobile.hriv.kivws.Unit kivwsUnit) {
    Unit unit = new Unit();
    ldapAttributes = new HashMap>();
    List attributes = kivwsUnit.getAttributes().getValue().getEntry();

    for (Entry entry : attributes) {
      ldapAttributes.put(entry.getKey(), entry.getValue().getAnyType());
    }
    populateGeoCoordinates(unit);
    // HsaIdentity.
    unit.setHsaIdentity(getSingleValue(KivwsAttributes.HSA_IDENTITY));
    // Unit name.
    populateUnitName(unit);
    // Unit locale (l attribute).
    unit.setLocale(getSingleValue(KivwsAttributes.L));
    // HsaSurgeryHours.
    List visitingHours = getMultiValue(KivwsAttributes.HSA_SURGERY_HOURS);
    unit.setHsaSurgeryHours(createTimeInterval(visitingHours));
    // HsaDropInHours.
    List hsaDropInHours = getMultiValue(KivwsAttributes.HSA_DROPIN_HOURS);
    unit.setHsaDropInHours(createTimeInterval(hsaDropInHours));
    // HsaManagementCodeText (Drivs av).
    unit.setHsaManagementCodeText(getSingleValue(KivwsAttributes.HSA_MANAGEMENT_CODE_TEXT));
    // HsaPublicTelephoneNumber.
    unit.setHsaPublicTelephoneNumber(getSingleValue(KivwsAttributes.HSA_PUBLIC_TELEPHONE_NUMBER));
    // HsaTelephoneTime.
    List telephoneTime = getMultiValue(KivwsAttributes.HSA_TELEPHONE_TIME);
    unit.setHsaTelephoneTime(createTimeInterval(telephoneTime));
    // HsaVisitingRuleAge
    unit.setHsaVisitingRuleAge(getSingleValue(KivwsAttributes.HSA_VISITING_RULE_AGE));
    // HsaLabeledUri.
    unit.setLabeleduri(getSingleValue(KivwsAttributes.LABELED_URI));
    // Description
    unit.setDescription(getSingleValue(KivwsAttributes.DESCRIPTION));
    
    return unit;
  }
  
  private void populateUnitName(Unit unit) {
	    // Name
	    String ou = getSingleValue(KivwsAttributes.OU);
	    String unitName;
	    if (ou!=null && ou.length()>0) {
	    	
	      unitName = ou.replace("\\,", ",");
	      unit.setName(unitName.trim());
	    } else {
	      String cn = getSingleValue(KivwsAttributes.CN);
	      // change \, to ,
	      unitName = cn.replace("\\,", ",");
	      unit.setName(unitName.trim());
	    }
	  }

  private String getSingleValue(String key) {
    String returnValue = "";
    if (ldapAttributes.containsKey(key)) {
      returnValue = (String) ldapAttributes.get(key).get(0);
    }
    return returnValue;
  }

  private List getMultiValue(String key) {
    List result = new ArrayList();
    List values = ldapAttributes.get(key);
    if (ldapAttributes.containsKey(key)) {
      for (Object object : values) {
        result.add((String) object);
      }
    }
    return result;
  }

  private void populateGeoCoordinates(Unit unit) {
    // Coordinates
    if ((String) getSingleValue(KivwsAttributes.HSA_GEOGRAPHICAL_COORDINATES) != null) {
      String hsaGeographicalCoordinates = (String) getSingleValue(KivwsAttributes.HSA_GEOGRAPHICAL_COORDINATES);
      // Parse and set in RT90 format
      int[] rt90Coords = GeoUtil.parseRT90HsaString(hsaGeographicalCoordinates);
      if (rt90Coords != null) {
        // Convert to WGS84 and set on unit too
        CoordinateTransformerService gkp = new GaussKrugerProjection("2.5V");
        double[] wgs84Coords = gkp.getWGS84(rt90Coords[0], rt90Coords[1]);
        unit.setLatitude(wgs84Coords[0]);
        unit.setLongitude(wgs84Coords[1]);
      }
    }
  }

  private Map> createTimeInterval(List timeInterval) {
    Map> officeHours = new HashMap>();
    List createWeekdayTimeList = WeekdayTime.createWeekdayTimeList(timeInterval);
    for (WeekdayTime weekdayTime : createWeekdayTimeList) {
      int startDay = weekdayTime.getStartDay();
      int endDay = weekdayTime.getEndDay();
      int i = startDay;
      do {
        getListForKey(officeHours, WeekdayTime.getDayName(i)).add(new OfficeHours(weekdayTime.getStartTimeDisplayValue(), weekdayTime.getEndTimeDisplayValue()));
        // officeHours.put(WeekdayTime.getDayName(i), new OfficeHours(weekdayTime.getStartTimeDisplayValue(), weekdayTime.getEndTimeDisplayValue()));
        i++;
      } while (i <= endDay);

    }
    return officeHours;
  }

  private List getListForKey(Map> officeHours, String key) {
    List officeHoursList = officeHours.get(key);
    if (officeHoursList == null) {
      officeHoursList = new ArrayList();
      officeHours.put(key, officeHoursList);
    }
    return officeHoursList;
  }

public Unit mapFromContext(Function function2) {
	Unit unit = new Unit();
    ldapAttributes = new HashMap>();
    List attributes = function2.getAttributes().getValue().getEntry();

    for (Entry entry : attributes) {
      ldapAttributes.put(entry.getKey(), entry.getValue().getAnyType());
    }
    populateGeoCoordinates(unit);
    // HsaIdentity.
    unit.setHsaIdentity(getSingleValue(KivwsAttributes.HSA_IDENTITY));
    // Unit name.
   populateUnitName(unit);
    // Unit locale (l attribute).
    unit.setLocale(getSingleValue(KivwsAttributes.L));
    // HsaSurgeryHours.
    List visitingHours = getMultiValue(KivwsAttributes.HSA_SURGERY_HOURS);
    unit.setHsaSurgeryHours(createTimeInterval(visitingHours));
    // HsaDropInHours.
    List hsaDropInHours = getMultiValue(KivwsAttributes.HSA_DROPIN_HOURS);
    unit.setHsaDropInHours(createTimeInterval(hsaDropInHours));
    // HsaManagementCodeText (Drivs av).
    unit.setHsaManagementCodeText(getSingleValue(KivwsAttributes.HSA_MANAGEMENT_CODE_TEXT));
    // HsaPublicTelephoneNumber.
    unit.setHsaPublicTelephoneNumber(getSingleValue(KivwsAttributes.HSA_PUBLIC_TELEPHONE_NUMBER));
    // HsaTelephoneTime.
    List telephoneTime = getMultiValue(KivwsAttributes.HSA_TELEPHONE_TIME);
    unit.setHsaTelephoneTime(createTimeInterval(telephoneTime));
    // HsaVisitingRuleAge
    unit.setHsaVisitingRuleAge(getSingleValue(KivwsAttributes.HSA_VISITING_RULE_AGE));
    // HsaLabeledUri.
    unit.setLabeleduri(getSingleValue(KivwsAttributes.LABELED_URI));
    // Description
    unit.setDescription(getSingleValue(KivwsAttributes.DESCRIPTION));
    
    return unit;
  
	}
}