Bladeren bron

DateSortLinkedMultiValueMap framework

Andrew Grant 3 maanden geleden
bovenliggende
commit
cc7eae529c

+ 1 - 1
src/main/java/scot/carricksoftware/grantswriter/services/censusentry/CensusEntryServiceImpl.java

@@ -30,7 +30,7 @@ public class CensusEntryServiceImpl implements CensusEntryService {
 
     @Override
     public List<CensusEntry> findAllByPerson(Person person) {
-        logger.debug("PersonServiceImpl::findAll");
+        logger.debug("PersonServiceImpl::findAllByPerson");
         List<CensusEntry> result = new ArrayList<>();
         Iterable<CensusEntry> censusEntryIterable = censusEntryRepository.findAllByPerson(person);
         for (CensusEntry censusEntry : censusEntryIterable) {

+ 1 - 1
src/main/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/PersonSubSectionTimeLineWriterImpl.java

@@ -44,7 +44,7 @@ public class PersonSubSectionTimeLineWriterImpl implements PersonSubSectionTimeL
         latexSubSectionHeader.write("Timeline");
         List<CensusEntry>  censusEntryList = censusEntryService.findAllByPerson(person);
         timelineData.clear();
-        timelineData.add(censusEntryList);
+       timelineData.add(censusEntryList);
 
         writeTimeLine.write(timelineData.getTimeline());
     }

+ 8 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/DateSortLinkedMultiValueMap.java

@@ -7,8 +7,16 @@ package scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.
 
 import org.springframework.util.LinkedMultiValueMap;
 
+import java.util.List;
+
 public interface DateSortLinkedMultiValueMap {
 
     @SuppressWarnings("SameReturnValue")
     LinkedMultiValueMap<String, String> sort (@SuppressWarnings("unused") LinkedMultiValueMap<String, String> map);
+
+    @SuppressWarnings("unused")
+    void swapEntries(List<String> keyList, int i, int j);
+
+    @SuppressWarnings("unused")
+    int compareDates(String a, String b);
 }

+ 63 - 1
src/main/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/DateSortLinkedMultiValueMapImpl.java

@@ -5,14 +5,76 @@
 
 package scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 import org.springframework.util.LinkedMultiValueMap;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
 @Component
 public class DateSortLinkedMultiValueMapImpl implements DateSortLinkedMultiValueMap {
 
+
+    private static final Logger logger = LogManager.getLogger(DateSortLinkedMultiValueMapImpl.class);
+
     @Override
     public LinkedMultiValueMap<String, String> sort(LinkedMultiValueMap<String, String> map) {
-        return null;
+
+        logger.debug("LinkedMultiValueMapImpl::sort");
+        Set<String> keys = map.keySet();
+        List<String> keyList = new ArrayList<>(keys);
+        if (keyList.size() > 1) {
+            sortKeyList(keyList);
+        }
+        LinkedMultiValueMap<String, String> newMap = new LinkedMultiValueMap<>();
+        for (String key : keyList) {
+            List<String> values = map.get(key);
+            assert values != null;
+            newMap.put(key, values);
+        }
+        return newMap;
+    }
+
+    @SuppressWarnings({"CommentedOutCode", "EmptyMethod", "unused"})
+    private void sortKeyList(List<String> keyList) {
+        //       int n = keyList.size();
+        //      boolean swapped;
+//        do {
+//            swapped = false;
+//            for (int i = 0; i < n - 1; i++) {
+//                if (compareTo(keyList.get(i), keyList.get(i + 1)) > 0) {
+//                    swapEntries(keyList, i + 1, i);
+//                    swapped = true;
+//                }
+//            }
+//        } while (swapped);
+
+    }
+
+    @Override
+    public void swapEntries(List<String> keyList, int i, int j) {
+        String temp = keyList.get(i);
+        keyList.set(i, keyList.get(j));
+        keyList.set(j, temp);
+    }
+
+    @Override
+    public int compareDates(String a, String b) {
+        String[] aParts = a.split("/");
+        String[] bParts = b.split("/");
+        if (Integer.parseInt(aParts[0]) > Integer.parseInt(bParts[0])) {
+            return 1;
+        }
+        if (Integer.parseInt(aParts[1]) > Integer.parseInt(bParts[1])) {
+            return 1;
+        }
+        if (Integer.parseInt(aParts[2]) > Integer.parseInt(bParts[2])) {
+            return 1;
+        }
+        return 0;
     }
 }
+

+ 2 - 2
src/main/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/WriteTimeLineImpl.java

@@ -41,8 +41,8 @@ public class WriteTimeLineImpl implements WriteTimeLine {
         logger.info("PersonSubSectionTimeLineWriterImp::write");
 
         latexLongTableStart.write("l l");
-        map = dateSortLinkedMultiValueMap.sort(map);
-        writeTheData(map);
+        LinkedMultiValueMap<String, String> newMap = dateSortLinkedMultiValueMap.sort(map);
+        writeTheData(newMap);
         latexLongTabLeEnd.write();
     }
 

+ 3 - 1
src/test/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/DateSortLinkedMultiValueMapTest.java

@@ -8,6 +8,8 @@ package scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
 class DateSortLinkedMultiValueMapTest {
 
     @SuppressWarnings("unused")
@@ -20,6 +22,6 @@ class DateSortLinkedMultiValueMapTest {
 
     @Test
     void constructorTest() {
-        underTest = new DateSortLinkedMultiValueMapImpl();
+        assertNotNull(underTest);
     }
 }