OSDN Git Service

fix: add null value state
authorhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Tue, 22 Feb 2022 08:34:34 +0000 (17:34 +0900)
committerhizumiaoba <56146205+hizumiaoba@users.noreply.github.com>
Tue, 22 Feb 2022 08:34:34 +0000 (17:34 +0900)
generated/settings.json
src/com/ranfa/lib/songinfo/FetchFromAPI.java
src/com/ranfa/main/DelesteRandomSelector.java

index 8f75baa..f849674 100644 (file)
@@ -2,7 +2,7 @@
   "checkLibraryUpdates" : true,
   "checkVersion" : true,
   "windowHeight" : 360,
-  "songLimit" : 2,
+  "songLimit" : 5,
   "saveScoreLog" : true,
   "windowWidth" : 640
 }
\ No newline at end of file
index 5088c9a..106a182 100644 (file)
@@ -1,6 +1,7 @@
 package com.ranfa.lib.songinfo;
 
 import java.io.IOException;
+import java.text.Normalizer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -21,62 +22,70 @@ import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.ranfa.lib.concurrent.CountedThreadFactory;
 
+import HajimeAPI4J.api.HajimeAPI4J;
 import HajimeAPI4J.api.HajimeAPI4J.List_Params;
 import HajimeAPI4J.api.HajimeAPI4J.List_Type;
 import HajimeAPI4J.api.HajimeAPI4J.Music_Params;
 import HajimeAPI4J.api.HajimeAPI4J.Token;
 import HajimeAPI4J.api.HajimeAPIBuilder;
-import HajimeAPI4J.api.util.HajimeAPI4JImpl;
 import HajimeAPI4J.api.util.datatype.Member;
+import HajimeAPI4J.exception.NoSuchURIException;
 
 public class FetchFromAPI {
        
        private Logger logger = LoggerFactory.getLogger(FetchFromAPI.class);
        
        
-       private ExecutorService apiExecutor = Executors.newCachedThreadPool(new CountedThreadFactory(() -> "DRS", "HajimeAPIInquerier"));
+       private ExecutorService localDispatcher = Executors.newCachedThreadPool(new CountedThreadFactory(() -> "DRS", "LocalDispatcher"));
        private List<JsonNode> nodes;
        
        // constructor
        
        public FetchFromAPI(String... songnames) {
-               List<CompletableFuture<JsonNode>> listFutures = new ArrayList<>();
+               List<JsonNode> listFutures = new ArrayList<>();
                for(String songname : Arrays.asList(songnames)) {
                        Map<String, Object> data = fetchList(songname);
                        if(data.getOrDefault("error", "false").equals("true")) {
                                JsonNode errorNode = new ObjectMapper().valueToTree(data);
-                               CompletableFuture<JsonNode> error = CompletableFuture.supplyAsync(() -> errorNode, apiExecutor);
-                               listFutures.add(error);
+                               listFutures.add(errorNode);
                                continue;
                        }
                        int taxId = Integer.parseInt(data.get("song_id").toString());
-                       HajimeAPI4JImpl impl = HajimeAPIBuilder.createDefault(Token.MUSIC)
+                       HajimeAPI4J impl = HajimeAPIBuilder.createDefault(Token.MUSIC)
                                        .addParameter(Music_Params.ID   , String.valueOf(taxId))
                                        .build();
                        logger.info("fetch data : {}", taxId);
-                       listFutures.add(impl.getAsync(apiExecutor));
+                       try {
+                               listFutures.add(impl.get());
+                       } catch (NoSuchURIException | IOException | InterruptedException e) {
+                               logger.error("Exception while processing json.", e);
+                       }
                }
-               CompletableFuture.allOf(listFutures.toArray(new CompletableFuture[listFutures.size()])).join();
-               nodes = new ArrayList<>();
-               listFutures.stream().forEach(cf -> {
-                       nodes.add(cf.join());
-               });
+               nodes = listFutures;
        }
        
        private Map<String, Object> fetchList(String songname) {
-               HajimeAPI4JImpl api = HajimeAPIBuilder.createDefault(Token.LIST)
+               HajimeAPI4J api = HajimeAPIBuilder.createDefault(Token.LIST)
                                .addParameter(List_Params.TYPE, List_Type.MUSIC.toString())
                                .addParameter(List_Params.SEARCH, songname)
                                .build();
                List<Map<String, Object>> parse = Collections.emptyList();
                try {
-                       parse = new ObjectMapper().readValue( api.getAsync(apiExecutor).join().traverse(), new TypeReference<List<Map<String, Object>>>() {});
+                       parse = new ObjectMapper().readValue( api.get().traverse(), new TypeReference<List<Map<String, Object>>>() {});
                        TimeUnit.SECONDS.sleep(1);
-               } catch (IOException | InterruptedException e) {
+               } catch (IOException | InterruptedException | NoSuchURIException e) {
                        logger.error("Exception while processing json map");
                }
+               if(parse == null || parse.isEmpty()) {
+                       parse = new ArrayList<>(1);
+                       Map<String, Object> tmp = new HashMap<>();
+                       tmp.put("error", "true");
+                       parse.add(tmp);
+               }
                for(Map<String, Object> tmp : parse ) {
-                       if(tmp.get("name").toString().equals(songname))
+                       String normalizeApiName = Normalizer.normalize(tmp.get("name").toString(), Normalizer.Form.NFKD);
+                       String normalizeLocalName = Normalizer.normalize(songname, Normalizer.Form.NFKD);
+                       if(normalizeApiName.equalsIgnoreCase(normalizeLocalName))
                                return tmp;
                }
                HashMap<String, Object> altRes = new HashMap<>();
@@ -88,7 +97,20 @@ public class FetchFromAPI {
                List<Map<String, String>> resultList = new ArrayList<>();
                int nodeSize = nodes.size();
                try {
+                       TimeUnit.SECONDS.sleep(1);
                        for(JsonNode node : nodes) {
+                               if(node.get("error").asText().equals("true")) {
+                                       Map<String, String> tmp = new HashMap<>();
+                                       String errorStr = "Failed to get.";
+                                       tmp.put("songname", errorStr);
+                                       tmp.put("link", errorStr);
+                                       tmp.put("lyric", errorStr);
+                                       tmp.put("composer", errorStr);
+                                       tmp.put("arrange", errorStr);
+                                       tmp.put("member", errorStr);
+                                       resultList.add(tmp);
+                                       continue;
+                               }
                                LinkedHashMap<String, String> result = new LinkedHashMap<>();
                                result.put("songname", node.get("name").asText());
                                result.put("link", node.get("link").asText());
@@ -100,22 +122,23 @@ public class FetchFromAPI {
                                List<Map<String, Object>> lyricList = mapper.readValue(node.get("lyrics").traverse(), typeRef),
                                                composerList = mapper.readValue(node.get("composer").traverse(), typeRef),
                                                arrangeList = mapper.readValue(node.get("arrange").traverse(), typeRef);
-                               lyric = CompletableFuture.supplyAsync(() -> getArrayedNames(lyricList), apiExecutor).join();
-                               composer = CompletableFuture.supplyAsync(() -> getArrayedNames(composerList), apiExecutor).join();
-                               arrange = CompletableFuture.supplyAsync(() -> getArrayedNames(arrangeList), apiExecutor).join();
+                               lyric = CompletableFuture.supplyAsync(() -> getArrayedNames(lyricList), localDispatcher).join();
+                               composer = CompletableFuture.supplyAsync(() -> getArrayedNames(composerList), localDispatcher).join();
+                               arrange = CompletableFuture.supplyAsync(() -> getArrayedNames(arrangeList), localDispatcher).join();
                                result.put("lyric", lyric);
                                result.put("composer", composer);
                                result.put("arrange", arrange);
                                StringBuilder memberBuilder = new StringBuilder();
                                for(Member tmpMember : mapper.readValue(node.get("member").traverse(), new TypeReference<List<Member>>() {})) {
-                                       memberBuilder.append(tmpMember.getName()).append(",");
+                                       if(tmpMember.getProduction().equals("cg"))
+                                               memberBuilder.append(tmpMember.getName()).append(",");
                                }
                                memberBuilder.deleteCharAt(memberBuilder.length() - 1);
                                result.put("member", memberBuilder.toString());
                                logger.info("data fetch complete. : {}", result);
                                resultList.add(result);
                        }
-               } catch(IOException e) {
+               } catch(IOException | InterruptedException e) {
                        logger.warn("Exception while processing json", e);
                        String errorStr = "No data";
                        for(int i = 0; i < nodeSize; i++) {
index edf1afd..f2eb248 100644 (file)
@@ -10,6 +10,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.text.Normalizer;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -239,12 +240,12 @@ public class DelesteRandomSelector extends JFrame {
                if(Files.exists(Paths.get("generated/albumCycle.json"))) {
                    this.albumType = EstimateAlbumTypeCycle.getCurrentCycle();
                }
-       }).whenCompleteAsync((ret, ex) -> {
+       }, es).whenCompleteAsync((ret, ex) -> {
                if(ex != null) {
                        logger.error("Exception was thrown during concurrent process", ex);
                        throw new IllegalStateException(ex);
                }
-       });
+       }, es);
        getWholeDataFuture.thenAcceptAsync(list -> this.logger.info("Scraping data size:" + list.size()), es);
        getFromJsonFuture.thenAcceptAsync(list -> this.logger.info("Currently database size:" + list.size()), es);
        // easter phase
@@ -307,12 +308,12 @@ public class DelesteRandomSelector extends JFrame {
        labelDifficulty.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
        panelWestMain.add(labelDifficulty, "2, 2, center, center");
        
-       comboDifficultySelect = new JComboBox();
+       comboDifficultySelect = new JComboBox<>();
        comboDifficultySelect.setModel(new DefaultComboBoxModel(new String[] {Messages.MSGNonSelected.toString(), "DEBUT", "REGULAR", "PRO", "MASTER", "MASTER+", "ⓁMASTER+", "LIGHT", "TRICK", "PIANO", "FORTE", "WITCH"}));
        comboDifficultySelect.setFont(new Font("Dialog", Font.BOLD, 12));
        panelWestMain.add(comboDifficultySelect, "2, 4, fill, fill");
        
-       comboAttribute = new JComboBox();
+       comboAttribute = new JComboBox<>();
        comboAttribute.setModel(new DefaultComboBoxModel(new String[] {Messages.MSGNonSelected.toString(), "全タイプ", "キュート", "クール", "パッション"}));
        comboAttribute.setFont(new Font("UD デジタル 教科書体 NP-B", Font.BOLD, 13));
        panelWestMain.add(comboAttribute, "2, 6, fill, top");
@@ -557,7 +558,9 @@ public class DelesteRandomSelector extends JFrame {
                                Song firstSong = toolIntegrateList.get(0);
                                Map<String, String> fetchMap = new HashMap<>();
                                for(Map<String, String> tmpMap : listToolMapData) {
-                                       if(tmpMap.get("songname").equals(firstSong.getName())) {
+                                       String normalizeApiName = Normalizer.normalize(tmpMap.get("songname").toString(), Normalizer.Form.NFKD);
+                                       String normalizeLocalName = Normalizer.normalize(firstSong.getName(), Normalizer.Form.NFKD);
+                                       if(normalizeApiName.equals(normalizeLocalName)) {
                                                fetchMap = tmpMap;
                                                break;
                                        }
@@ -732,7 +735,9 @@ public class DelesteRandomSelector extends JFrame {
                                logger.info("prevSong: {}", prevSong);
                                Map<String, String> fetchMap = new HashMap<>();
                                for(Map<String, String> tmpMap : listToolMapData) {
-                                       if(tmpMap.get("songname").equals(prevSong.getName())) {
+                                       String normalizeApiName = Normalizer.normalize(tmpMap.get("songname").toString(), Normalizer.Form.NFKD);
+                                       String normalizeLocalName = Normalizer.normalize(prevSong.getName(), Normalizer.Form.NFKD);
+                                       if(normalizeApiName.equals(normalizeLocalName)) {
                                                fetchMap = tmpMap;
                                                break;
                                        }
@@ -759,7 +764,7 @@ public class DelesteRandomSelector extends JFrame {
        labelLyricToolTitle.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 12));
        panelCenterTool.add(labelLyricToolTitle, "2, 26, center, default");
        
-       labelLyricToolTip = new JLabel("New label");
+       labelLyricToolTip = new JLabel("Please wait...");
        labelLyricToolTip.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 12));
        panelCenterTool.add(labelLyricToolTip, "10, 26, center, default");
        
@@ -767,7 +772,7 @@ public class DelesteRandomSelector extends JFrame {
        labelComposerToolTitle.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 12));
        panelCenterTool.add(labelComposerToolTitle, "2, 30, center, default");
        
-       labelComposerToolTip = new JLabel("New label");
+       labelComposerToolTip = new JLabel("Please wait...");
        labelComposerToolTip.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 12));
        panelCenterTool.add(labelComposerToolTip, "10, 30, center, default");
        
@@ -775,7 +780,7 @@ public class DelesteRandomSelector extends JFrame {
        labelArrangeToolTitle.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 12));
        panelCenterTool.add(labelArrangeToolTitle, "2, 34, center, default");
        
-       labelArrangeToolTip = new JLabel("New label");
+       labelArrangeToolTip = new JLabel("Please wait...");
        labelArrangeToolTip.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 12));
        panelCenterTool.add(labelArrangeToolTip, "10, 34, center, default");
        btnPrevSongTool.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 12));
@@ -791,7 +796,9 @@ public class DelesteRandomSelector extends JFrame {
                                logger.info("nextSong: {}", nextSong);
                                Map<String, String> fetchMap = new HashMap<>();
                                for(Map<String, String> tmpMap : listToolMapData) {
-                                       if(tmpMap.get("songname").equals(nextSong.getName())) {
+                                       String normalizeApiName = Normalizer.normalize(tmpMap.get("songname").toString(), Normalizer.Form.NFKD);
+                                       String normalizeLocalName = Normalizer.normalize(nextSong.getName(), Normalizer.Form.NFKD);
+                                       if(normalizeApiName.equals(normalizeLocalName)) {
                                                fetchMap = tmpMap;
                                                break;
                                        }
@@ -832,7 +839,9 @@ public class DelesteRandomSelector extends JFrame {
                        Song currentSong = toolIntegrateList.get(currentIndex);
                        Map<String, String> fetchMap = new HashMap<>();
                        for(Map<String, String> tmpMap : listToolMapData) {
-                               if(tmpMap.get("songname").equals(currentSong.getName())) {
+                               String normalizeApiName = Normalizer.normalize(tmpMap.get("songname").toString(), Normalizer.Form.NFKD);
+                               String normalizeLocalName = Normalizer.normalize(currentSong.getName(), Normalizer.Form.NFKD);
+                               if(normalizeApiName.equals(normalizeLocalName)) {
                                        fetchMap = tmpMap;
                                        break;
                                }