From 5c999d39c0c0fc56120de73e0390a760f7069e12 Mon Sep 17 00:00:00 2001
From: Lars Johansson <lars.johansson@ess.eu>
Date: Tue, 2 Nov 2021 13:31:45 +0100
Subject: [PATCH] CD.591: Prepare report for cables with FBS tags no longer
 available in CHESS

Report with cables with FBS tags not longer available in CHESS (Excel).
---
 .../openepics/cable/jaxb/ReportResource.java  | 10 +++
 .../cable/webservice/ReportResourceImpl.java  | 81 +++++++++++++++++++
 2 files changed, 91 insertions(+)

diff --git a/cabledb-jaxb/src/main/java/org/openepics/cable/jaxb/ReportResource.java b/cabledb-jaxb/src/main/java/org/openepics/cable/jaxb/ReportResource.java
index def301fe5..80048a52b 100644
--- a/cabledb-jaxb/src/main/java/org/openepics/cable/jaxb/ReportResource.java
+++ b/cabledb-jaxb/src/main/java/org/openepics/cable/jaxb/ReportResource.java
@@ -45,4 +45,14 @@ public interface ReportResource {
     @Produces(MediaType.APPLICATION_OCTET_STREAM)
     public Response reportCablesWithEssNamesWithoutFbsTags();
 
+    /**
+     * Unofficial report with cables for which at least one FBS tag is without mapping in CHESS.
+     *
+     * @return report with cables (Excel)
+     */
+    @GET
+    @Path("/cable/chess/cables_withfbstagsnochessmapping")
+    @Produces(MediaType.APPLICATION_OCTET_STREAM)
+    public Response reportCablesFbsTagNoChessMapping();
+
 }
diff --git a/cabledb/src/main/java/org/openepics/cable/webservice/ReportResourceImpl.java b/cabledb/src/main/java/org/openepics/cable/webservice/ReportResourceImpl.java
index b64c3cd0c..49423fef0 100644
--- a/cabledb/src/main/java/org/openepics/cable/webservice/ReportResourceImpl.java
+++ b/cabledb/src/main/java/org/openepics/cable/webservice/ReportResourceImpl.java
@@ -3,6 +3,7 @@ package org.openepics.cable.webservice;
 import java.io.InputStream;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -187,6 +188,8 @@ public class ReportResourceImpl implements ReportResource {
 
     @Override
     public Response reportCablesWithEssNamesWithoutFbsTags() {
+        // Unofficial report with cables for which ESS name but not FBS tag is used
+
         final List<Cable> cablesToExport = cableService.getCablesWithEssNamesWithoutFbsTags();
         final CableSaver cableSaver = new CableSaver(cablesToExport);
         final InputStream inputStream = cableSaver.save();
@@ -207,4 +210,82 @@ public class ReportResourceImpl implements ReportResource {
         }
     }
 
+    @Override
+    public Response reportCablesFbsTagNoChessMapping() {
+        // Unofficial report with cables for which at least one FBS tag is without mapping in CHESS
+
+        // find fbs elements and mappings cable name to fbs element
+        final List<FbsElement> fbsElements = fbsService.getAllFbsElements();
+        Map<String, FbsElement> mappingsFbsTagToFbsElement = null;
+        if (!fbsElements.isEmpty()) {
+            mappingsFbsTagToFbsElement =
+                    FbsUtil.readMappingsTagToFbsElement(fbsElements);
+
+            LOGGER.log(Level.INFO, "count mappings fbs tag: "
+                    + (mappingsFbsTagToFbsElement != null ? mappingsFbsTagToFbsElement.size() : "null"));
+        }
+
+        // check if fbs elements and mappings cable to fbs element available
+
+        final List<Cable> cablesToExport = new ArrayList<>();
+        String fbsTag = null;
+        String fromFbsTag = null;
+        String fromEnclosureFbsTag = null;
+        String toFbsTag = null;
+        String toEnclosureFbsTag = null;
+
+        // loop through cables
+        for (Cable cable : cableService.getCables()) {
+            // not consider deleted cables
+            if (CableStatus.DELETED.equals(cable.getStatus())) {
+                continue;
+            }
+
+            // cable
+            //     name - fbs tag
+            //     from endpoint
+            //         ess name - fbs tag
+            //         enclosure ess name - enclosure fbs tag
+            //     to endpoint
+            //         ess name - fbs tag
+            //         enclosure ess name - enclosure fbs tag
+
+            fbsTag = cable.getFbsTag();
+            fromFbsTag = cable.getEndpointA().getDeviceFbsTag();
+            fromEnclosureFbsTag = cable.getEndpointA().getRackFbsTag();
+            toFbsTag = cable.getEndpointB().getDeviceFbsTag();
+            toEnclosureFbsTag = cable.getEndpointB().getRackFbsTag();
+
+            if (!StringUtils.isEmpty(fbsTag) && mappingsFbsTagToFbsElement.get(fbsTag) == null) {
+                cablesToExport.add(cable);
+            } else if (!StringUtils.isEmpty(fromFbsTag) && mappingsFbsTagToFbsElement.get(fromFbsTag) == null) {
+                cablesToExport.add(cable);
+            } else if (!StringUtils.isEmpty(fromEnclosureFbsTag) && mappingsFbsTagToFbsElement.get(fromEnclosureFbsTag) == null) {
+                cablesToExport.add(cable);
+            } else if (!StringUtils.isEmpty(toFbsTag) && mappingsFbsTagToFbsElement.get(toFbsTag) == null) {
+                cablesToExport.add(cable);
+            } else if (!StringUtils.isEmpty(toEnclosureFbsTag) && mappingsFbsTagToFbsElement.get(toEnclosureFbsTag) == null) {
+                cablesToExport.add(cable);
+            }
+        }
+
+        final CableSaver cableSaver = new CableSaver(cablesToExport);
+        final InputStream inputStream = cableSaver.save();
+
+        try {
+            byte[] bytes = IOUtils.toByteArray(inputStream);
+
+            return Response.ok()
+                    .entity(bytes)
+                    .header("Content-Disposition", "attachment; filename=\"cdb_cables.xlsx\"")
+                    .type(MediaType.APPLICATION_OCTET_STREAM)
+                    .build();
+        } catch (Exception e) {
+            LOGGER.log(Level.INFO,
+                    "Report for cables with FBS tag(s) without mapping in CHESS "
+                    + "could not be made available, message: " + e.getMessage());
+            return null;
+        }
+    }
+
 }
-- 
GitLab