CommonUtils.java 6.29 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3
package pwc.taxtech.atms.common;

import java.io.IOException;
eddie.woo's avatar
eddie.woo committed
4
import java.util.*;
eddie.woo's avatar
eddie.woo committed
5 6 7 8 9 10
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.nutz.lang.Lang;
import org.springframework.core.io.ClassPathResource;

11
import pwc.taxtech.atms.dpo.EnterpriseAccountSetOrgDto;
eddie.woo's avatar
eddie.woo committed
12 13

public class CommonUtils {
eddie.woo's avatar
eddie.woo committed
14
    public static final int BATCH_NUM = 500;
eddie.woo's avatar
eddie.woo committed
15 16 17
    public static final int BATCH_NUM_1000 = 1000;
    public static final int BATCH_NUM_2000 = 2000;

eddie.woo's avatar
eddie.woo committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    public static String getUUID() {
        return UUID.randomUUID().toString().toUpperCase();
    }
    
    public static <T> T copyProperties(Object sourceObject, T targetObject) {
        Lang.copyProperties(sourceObject, targetObject);
        return targetObject;
    }
    
    /**
     * Determines whether [is date time overlapp] [the specified start].
     * @param organiztionAccountSetList
     * @return
     */
    public static boolean isOrganizationDateTimeOverlap(List<EnterpriseAccountSetOrgDto> organiztionAccountSetList) {
        
        if (organiztionAccountSetList == null || organiztionAccountSetList.isEmpty()) {
            return false;
        }
        
        for (int i = organiztionAccountSetList.size() - 1; i >= 0; i--)
        {
            organiztionAccountSetList.get(i).setOverlapList(new ArrayList<EnterpriseAccountSetOrgDto>());
            for (int j = i - 1; j >= 0; j--)
            {
                if (isDateTimeOverlap(organiztionAccountSetList.get(i).getEffectiveDate(),
                        organiztionAccountSetList.get(i).getExpiredDate(),
                        organiztionAccountSetList.get(j).getEffectiveDate(),
                        organiztionAccountSetList.get(j).getExpiredDate()
                    ))
                {
                    organiztionAccountSetList.get(i).getOverlapList().add(organiztionAccountSetList.get(j));
                }
            }
        }
        if (organiztionAccountSetList.stream().anyMatch(sa -> !sa.getOverlapList().isEmpty())) {
            return true;
        }
        return false;
    }
    
    public static boolean validateOnlyOncePerYear(List<EnterpriseAccountSetOrgDto> organiztionAccountSetList) {
        
61 62
        List<String> accountSetIdList = organiztionAccountSetList.stream()
                .map(sa -> sa.getEnterpriseAccountSetId()).distinct().collect(Collectors.toList());
eddie.woo's avatar
eddie.woo committed
63
        
64
        for(String accountSetId: accountSetIdList) {
eddie.woo's avatar
eddie.woo committed
65
            List<EnterpriseAccountSetOrgDto> sameAccountSetList = organiztionAccountSetList.stream()
66
                    .filter(sa -> sa.getEnterpriseAccountSetId().equals(accountSetId)).collect(Collectors.toList());
eddie.woo's avatar
eddie.woo committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            for(int i = 0; i < sameAccountSetList.size() -1; i++) {
                for(int j = i+1; j<sameAccountSetList.size(); j++) {
                    EnterpriseAccountSetOrgDto first = sameAccountSetList.get(i);
                    EnterpriseAccountSetOrgDto second = sameAccountSetList.get(j);
                    
                    //DateTime firstE = new DateTime
                    if(first.getExpiredDate()!=null && second.getEffectiveDate()!=null) {
                        Calendar firstExpired = Calendar.getInstance();
                        firstExpired.setTime(first.getExpiredDate());
                        Calendar secondEffective = Calendar.getInstance();
                        secondEffective.setTime(second.getEffectiveDate());
                        if(firstExpired.get(Calendar.YEAR) == secondEffective.get(Calendar.YEAR)) {
                            return false;
                        }
                    }
                    if(first.getEffectiveDate()!=null && second.getExpiredDate()!=null) {
                        Calendar firstEffective = Calendar.getInstance();
                        firstEffective.setTime(first.getEffectiveDate());
                        Calendar secondExpired = Calendar.getInstance();
                        secondExpired.setTime(second.getExpiredDate());
                        if(firstEffective.get(Calendar.YEAR) == secondExpired.get(Calendar.YEAR)) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
    
    /**
     * Determines whether [is date time overlapp] [the specified start].
     * @param start - The start
     * @param end - The end
     * @param compareStart - The compare start
     * @param compareEnd - The compare end
     * @return true - there is overlap; false - there is no overlap
     */
    public static boolean isDateTimeOverlap(Date start, Date end, Date compareStart, Date compareEnd) {
        
        //1/1/0001 12:00:00 AM  (Equals Date.MinValue)
        //9999/12/31 23:59:59 PM (Equals Date.MaxValue)
        
        Calendar calMin = Calendar.getInstance();
        calMin.set(0001, 1, 1, 0, 0, 0);
        Calendar calMax = Calendar.getInstance();
        calMax.set(9999, 12, 32, 23, 59, 59);

        start = start == null ? calMin.getTime() : start;
        end = end == null ? calMin.getTime() : end;
        compareStart = compareStart == null ? calMin.getTime() : compareStart;
        compareEnd = compareEnd == null ? calMin.getTime() : compareEnd;
        
        if(start.compareTo(compareEnd)<=0 && end.compareTo(compareStart)>=0) {
            return true;
        }
        
        return false;
    }
    
    
    public static String readClasspathFileToString(String path) {
        String text = null;;
        try {
            ClassPathResource classPathResource = new ClassPathResource(path);
            text = IOUtils.toString(classPathResource.getInputStream(), "UTF-8");
        } catch (IOException e) {
          throw Lang.wrapThrow(e);
        }
        return text;
    }
eddie.woo's avatar
eddie.woo committed
138 139 140

    public static <T> List<List<T>> subListWithLen(List<T> source, int len) {
        if (source == null || source.size() == 0 || len < 1) {
eddie.woo's avatar
eddie.woo committed
141
            return Collections.emptyList();
eddie.woo's avatar
eddie.woo committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155
        }
        List<List<T>> result = new ArrayList<>();
        int count = (source.size() + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<T> value;
            if ((i + 1) * len < source.size()) {
                value = source.subList(i * len, (i + 1) * len);
            } else {
                value = source.subList(i * len, source.size());
            }
            result.add(value);
        }
        return result;
    }
156 157


eddie.woo's avatar
eddie.woo committed
158
}