RiksdagenDateUtil.java

  1. /*
  2.  * Copyright 2010-2019 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.service.external.riksdagen.impl;

  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Date;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Locale;
  26. import java.util.Set;

  27. import com.hack23.cia.model.external.riksdagen.votering.impl.BallotContainer;
  28. import com.hack23.cia.model.external.riksdagen.votering.impl.VoteDataDto;

  29. /**
  30.  * The Class RiksdagenDateUtil.
  31.  */
  32. final class RiksdagenDateUtil {

  33.     /** The Constant YYYY_MM_DD. */
  34.     public static final String YYYY_MM_DD = "yyyy-MM-dd";

  35.     /** The Constant CONTAINS_ONE. */
  36.     private static final int CONTAINS_ONE = 1;

  37.    
  38.     /**
  39.      * Instantiates a new riksdagen date util.
  40.      */
  41.     public RiksdagenDateUtil() {
  42.         super();
  43.     }

  44.     /**
  45.      * Best guess vote date.
  46.      *
  47.      * @param ballotContainer
  48.      *            the ballot container
  49.      * @return the date
  50.      * @throws ParseException
  51.      *             the parse exception
  52.      */
  53.     private static Date bestGuessVoteDate(final BallotContainer ballotContainer) throws ParseException {
  54.         final com.hack23.cia.model.external.riksdagen.votering.impl.BallotDocumentElement ballotDocumentElement = ballotContainer.getBallotDocumentElement();
  55.         Date result;

  56.         final String createdDate=ballotContainer.getBallotDocumentElement().getCreatedDate();

  57.         if(createdDate!= null && createdDate.length()>= YYYY_MM_DD.length()) {
  58.             result=new SimpleDateFormat(YYYY_MM_DD,Locale.ENGLISH).parse(createdDate);
  59.         } else {
  60.             final String systemDate = ballotDocumentElement.getSystemDate();

  61.             if(systemDate!= null && systemDate.length()>= YYYY_MM_DD.length()) {
  62.                 result=new SimpleDateFormat(YYYY_MM_DD,Locale.ENGLISH).parse(systemDate);
  63.             } else {
  64.                 result=new SimpleDateFormat(YYYY_MM_DD,Locale.ENGLISH).parse(ballotDocumentElement.getMadePublicDate());
  65.             }
  66.         }
  67.         return result;
  68.     }

  69.     /**
  70.      * Check same date.
  71.      *
  72.      * @param voteList
  73.      *            the vote list
  74.      * @return the date
  75.      * @throws ParseException
  76.      *             the parse exception
  77.      */
  78.     private static Date checkSameDate(final List<VoteDataDto> voteList) throws ParseException {
  79.         final Set<String> set = new HashSet<>();
  80.         Date result=null;

  81.         for (final VoteDataDto voteData : voteList) {
  82.             final String voteDate = voteData.getVoteDate();
  83.             if (voteDate !=null && voteDate.length() >= YYYY_MM_DD.length()) {
  84.                 set.add(voteData.getVoteDate());
  85.             }
  86.         }

  87.         if (set.size() ==CONTAINS_ONE) {
  88.             final String dateString = set.iterator().next();
  89.             result=new SimpleDateFormat(YYYY_MM_DD,Locale.ENGLISH).parse(dateString);
  90.         }
  91.         return result;
  92.     }

  93.     /**
  94.      * Try to find valid vote date.
  95.      *
  96.      * @param ballotContainer
  97.      *            the ballot container
  98.      * @param voteDataList
  99.      *            the vote data list
  100.      * @return the date
  101.      * @throws ParseException
  102.      *             the parse exception
  103.      */
  104.     public Date tryToFindValidVoteDate(final BallotContainer ballotContainer, final List<VoteDataDto> voteDataList)
  105.                     throws ParseException {
  106.         Date ballotDate;
  107.         final Date sameDate = checkSameDate(voteDataList);

  108.         if (sameDate != null) {
  109.             ballotDate = sameDate;
  110.         } else {
  111.             ballotDate = bestGuessVoteDate(ballotContainer);
  112.         }
  113.         return ballotDate;
  114.     }

  115. }