/*
 * This file is part of CoAnSys project.
 * Copyright (c) 2012-2013 ICM-UW
 * 
 * CoAnSys is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CoAnSys 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with CoAnSys. If not, see <http://www.gnu.org/licenses/>.
 */

package pl.edu.icm.coansys.disambiguation.work.comparator;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pl.edu.icm.coansys.commons.java.DocumentWrapperUtils;
import pl.edu.icm.coansys.models.DocumentProtos.DocumentWrapper;

@Service("workYearComparator")
public class WorkYearComparator {

    private WorkYearComparatorConfiguration config;
    
    @Autowired
    public WorkYearComparator(WorkYearComparatorConfiguration config) {
        this.config = config;
    }
    
    public boolean sameYears(DocumentWrapper doc1, DocumentWrapper doc2) {
        String doc1year = DocumentWrapperUtils.getPublicationYear(doc1);
        String doc2year = DocumentWrapperUtils.getPublicationYear(doc2);

        // if one of the years is not known then treat these years as equal
        if (StringUtils.isBlank(doc1year) || StringUtils.isBlank(doc2year)) {
            return true;
        }
        
        Integer year1 = Integer.parseInt(doc1year);
        Integer year2 = Integer.parseInt(doc2year);
        
        return Math.abs(year1-year2)<=config.getPublicationYearMaxDistance();
        
    }
}

@@ -793,52 +793,8 @@
 r;%0A%0A
-import org.apache.commons.lang.StringUtils;%0A
 impo
@@ -1093,20 +1093,17 @@
 ator %7B%0A%0A
-    
+%09
 private 
@@ -1142,25 +1142,18 @@
 config;%0A
-    %0A    
+%0A%09
 @Autowir
@@ -1155,20 +1155,17 @@
 towired%0A
-    
+%09
 public W
@@ -1224,24 +1224,18 @@
 nfig) %7B%0A
-        
+%09%09
 this.con
@@ -1252,23 +1252,13 @@
 ig;%0A
-    %7D%0A    %0A    
+%09%7D%0A%0A%09
 publ
@@ -1324,24 +1324,18 @@
 doc2) %7B%0A
-        
+%09%09
 String d
@@ -1391,24 +1391,18 @@
 (doc1);%0A
-        
+%09%09
 String d
@@ -1463,214 +1463,19 @@
 );%0A%0A
-        // if one of the years is not known then treat these years as equal%0A        if (StringUtils.isBlank(doc1year) %7C%7C StringUtils.isBlank(doc2year)) %7B%0A            return true;%0A        %7D%0A        %0A        
+%09%09try %7B%0A%09%09%09
 Inte
@@ -1514,24 +1514,19 @@
 1year);%0A
-        
+%09%09%09
 Integer 
@@ -1565,25 +1565,12 @@
 r);%0A
-        %0A        
+%0A%09%09%09
 retu
@@ -1590,23 +1590,33 @@
 ear1
--
+ - 
 year2)
+ 
 %3C=
+ 
 config
+%0A%09%09%09%09%09
 .get
@@ -1649,21 +1649,230 @@
 ();%0A
-        %0A    
+%09%09%7D catch (NumberFormatException ex) %7B%0A%09%09%09// if one of the years can not be parsed to the integer (i.e either%0A%09%09%09// is not known or in the form 20113/02013 etc. )%0A%09%09%09// then treat these years as equal%0A%09%09%09return true;%0A%09%09%7D%0A%09
 %7D%0A%7D%0A

make WorkYearComparator immune to non integer years (e.g. 2012/2013)