extensions/net.sf.basedb.meludi/trunk/src/net/sf/basedb/meludi/servlet/ReferralGeneratorServlet.java

Code
Comments
Other
Rev Date Author Line
4706 19 Mar 18 olle 1 package net.sf.basedb.meludi.servlet;
4706 19 Mar 18 olle 2
4706 19 Mar 18 olle 3 import java.awt.image.BufferedImage;
4706 19 Mar 18 olle 4 import java.io.BufferedReader;
4706 19 Mar 18 olle 5 import java.io.ByteArrayOutputStream;
4706 19 Mar 18 olle 6 //import java.io.File;
4706 19 Mar 18 olle 7 import java.io.FileInputStream;
4706 19 Mar 18 olle 8 import java.io.FileOutputStream;
4706 19 Mar 18 olle 9 import java.io.FileWriter;
4706 19 Mar 18 olle 10 import java.io.IOException;
4706 19 Mar 18 olle 11 import java.io.InputStream;
4706 19 Mar 18 olle 12 import java.io.InputStreamReader;
4706 19 Mar 18 olle 13 import java.io.OutputStream;
4706 19 Mar 18 olle 14 import java.io.OutputStreamWriter;
5154 28 Nov 18 olle 15 import java.net.URL;
4706 19 Mar 18 olle 16 import java.text.SimpleDateFormat;
4706 19 Mar 18 olle 17 import java.util.ArrayList;
4724 29 Mar 18 olle 18 import java.util.Collection;
4724 29 Mar 18 olle 19 import java.util.Collections;
4724 29 Mar 18 olle 20 import java.util.Comparator;
4706 19 Mar 18 olle 21 import java.util.Date;
4706 19 Mar 18 olle 22 import java.util.HashMap;
4706 19 Mar 18 olle 23 import java.util.List;
4706 19 Mar 18 olle 24
4706 19 Mar 18 olle 25 import javax.servlet.ServletException;
4706 19 Mar 18 olle 26 import javax.servlet.http.HttpServlet;
4706 19 Mar 18 olle 27 import javax.servlet.http.HttpServletRequest;
4706 19 Mar 18 olle 28 import javax.servlet.http.HttpServletResponse;
4706 19 Mar 18 olle 29
4706 19 Mar 18 olle 30 import org.json.simple.JSONArray;
4706 19 Mar 18 olle 31 import org.json.simple.JSONObject;
4915 16 Jul 18 olle 32 import org.json.simple.parser.JSONParser;
4706 19 Mar 18 olle 33
4706 19 Mar 18 olle 34 import net.sf.basedb.core.Application;
4706 19 Mar 18 olle 35 import net.sf.basedb.core.DbControl;
4706 19 Mar 18 olle 36 import net.sf.basedb.core.Directory;
4706 19 Mar 18 olle 37 import net.sf.basedb.core.File;
4706 19 Mar 18 olle 38 import net.sf.basedb.core.ItemQuery;
4706 19 Mar 18 olle 39 import net.sf.basedb.core.Path;
5201 07 Jan 19 olle 40 import net.sf.basedb.core.Project;
4706 19 Mar 18 olle 41 import net.sf.basedb.core.SessionControl;
4706 19 Mar 18 olle 42 import net.sf.basedb.core.SimpleProgressReporter;
4706 19 Mar 18 olle 43 import net.sf.basedb.core.query.Hql;
4706 19 Mar 18 olle 44 import net.sf.basedb.core.query.Orders;
4865 20 Jun 18 olle 45 import net.sf.basedb.core.snapshot.SnapshotManager;
4706 19 Mar 18 olle 46 import net.sf.basedb.meludi.converter.DateToStringConverter;
5061 30 Oct 18 olle 47 import net.sf.basedb.meludi.counter.CounterService;
4706 19 Mar 18 olle 48 import net.sf.basedb.meludi.JsonUtil;
4706 19 Mar 18 olle 49 import net.sf.basedb.meludi.Meludi;
4865 20 Jun 18 olle 50 import net.sf.basedb.meludi.dao.Annotationtype;
4865 20 Jun 18 olle 51 import net.sf.basedb.meludi.dao.Case;
4909 12 Jul 18 olle 52 import net.sf.basedb.meludi.dao.FfpeBlock;
4706 19 Mar 18 olle 53 import net.sf.basedb.meludi.dao.MeludiRole;
4865 20 Jun 18 olle 54 import net.sf.basedb.meludi.dao.Patient;
4908 12 Jul 18 olle 55 import net.sf.basedb.meludi.dao.Subtype;
4706 19 Mar 18 olle 56 import net.sf.basedb.meludi.pdf.PdfUtil;
4706 19 Mar 18 olle 57 import net.sf.basedb.meludi.Site;
4706 19 Mar 18 olle 58 import net.sf.basedb.util.Values;
4706 19 Mar 18 olle 59 import net.sf.basedb.util.error.ThrowableUtil;
4706 19 Mar 18 olle 60
4706 19 Mar 18 olle 61 import com.itextpdf.text.Element;
4706 19 Mar 18 olle 62
4706 19 Mar 18 olle 63 public class ReferralGeneratorServlet 
4706 19 Mar 18 olle 64   extends HttpServlet 
4706 19 Mar 18 olle 65 {
4706 19 Mar 18 olle 66   private static final long serialVersionUID = 2550233926528990677L;
4724 29 Mar 18 olle 67   //private static final String REF_START_INFO_FILENAME = "scanb_referral_start_info.txt";
4724 29 Mar 18 olle 68   //private static final String REF_FORM_LOG_FILENAME = "refFormLogFile.txt";
4724 29 Mar 18 olle 69   //private static final String REF_FORM_BATCH_BASE_FILENAME = "scanb_referral_form_batch";
4724 29 Mar 18 olle 70   //private static final String REF_VARIANT_KIT_1_STANDARD = "kit1_standard";
4724 29 Mar 18 olle 71   //private static final String REF_VARIANT_KIT_2_NEOADJUVANT = "kit2_neoadjuvant";
4724 29 Mar 18 olle 72   //private static final String REFERRAL_GENERATION_PROGRESS_ID = "ref-gen-progress";
4724 29 Mar 18 olle 73
4724 29 Mar 18 olle 74   // Remember to add new form template type constants to initializeRefFormTemplNameStartHashMap()
4724 29 Mar 18 olle 75   //private static final String REF_FORM_TEMPLATE_PATIENT_INFO = "patient-info";
4724 29 Mar 18 olle 76   //private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY = "clinical-chemistry-biopsy";
4724 29 Mar 18 olle 77   //private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD = "clinical-chemistry-blood";
4724 29 Mar 18 olle 78   //private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD_LUNDCOPY = "clinical-chemistry-blood-lundcopy";
4724 29 Mar 18 olle 79   //private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BCBLOOD = "clinical-chemistry-bcblood";
4724 29 Mar 18 olle 80   //private static final String REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY = "middle-needle-biopsy";
4724 29 Mar 18 olle 81   //private static final String REF_FORM_TEMPLATE_CLIN_CHEM = "clinical-chemistry";
4724 29 Mar 18 olle 82   //private static final String REF_FORM_TEMPLATE_CLIN_PATHOLOGY = "clinical-pathology";
4724 29 Mar 18 olle 83   //private static final String REF_FORM_TEMPLATE_CONSENT_REPORT = "consent-report";
4724 29 Mar 18 olle 84
4724 29 Mar 18 olle 85   // Remember to add new template filename start constants to initializeRefFormTemplNameStartHashMap()
4724 29 Mar 18 olle 86   //private static final String TEMPLATE_FILENAME_START_PATIENT_INFO = "Patinfo";
4724 29 Mar 18 olle 87   //private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BIOPSY = "KlinKemBiopsy-SCAN-B-remiss";
4724 29 Mar 18 olle 88   //private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD = "KlinKem-SCAN-B-blood-biobanken_remiss";
4724 29 Mar 18 olle 89   //private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD_LUNDCOPY = "KlinKem-SCAN-B-blood-biobanken-lundkopia_remiss";
4724 29 Mar 18 olle 90   //private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BCBLOOD = "KlinKem-SCAN-B-bcblood-remiss";
4724 29 Mar 18 olle 91   //private static final String TEMPLATE_FILENAME_START_MIDDLE_NEEDLE_BIOPSY = "PathBiopsy-SCAN-B-remiss";
4724 29 Mar 18 olle 92   //private static final String TEMPLATE_FILENAME_START_CLIN_CHEM = "Klinkem-SCAN-B-remiss";
4724 29 Mar 18 olle 93   //private static final String TEMPLATE_FILENAME_START_CLIN_PATHOLOGY = "Path-SCAN-B-remiss";
4724 29 Mar 18 olle 94   //private static final String TEMPLATE_FILENAME_START_CONSENT_REPORT = "Consent-SCAN-B-remiss";
4724 29 Mar 18 olle 95
4724 29 Mar 18 olle 96   private static final String REF_START_INFO_FILENAME = "meludi_referral_start_info.txt";
4706 19 Mar 18 olle 97   private static final String REF_FORM_LOG_FILENAME = "refFormLogFile.txt";
5201 07 Jan 19 olle 98   //private static final String REF_FORM_BATCH_BASE_FILENAME = "meludi_referral_form_batch";
5201 07 Jan 19 olle 99   private static final String REF_FORM_BATCH_BASE_FILENAME = "referral_form_batch";
4706 19 Mar 18 olle 100   private static final String REF_VARIANT_KIT_1_STANDARD = "kit1_standard";
4706 19 Mar 18 olle 101   private static final String REF_VARIANT_KIT_2_NEOADJUVANT = "kit2_neoadjuvant";
4706 19 Mar 18 olle 102   private static final String REFERRAL_GENERATION_PROGRESS_ID = "ref-gen-progress";
4706 19 Mar 18 olle 103
4706 19 Mar 18 olle 104   // Remember to add new form template type constants to initializeRefFormTemplNameStartHashMap()
4706 19 Mar 18 olle 105   private static final String REF_FORM_TEMPLATE_PATIENT_INFO = "patient-info";
4706 19 Mar 18 olle 106   private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY = "clinical-chemistry-biopsy";
4706 19 Mar 18 olle 107   private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD = "clinical-chemistry-blood";
4706 19 Mar 18 olle 108   private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD_LUNDCOPY = "clinical-chemistry-blood-lundcopy";
4706 19 Mar 18 olle 109   private static final String REF_FORM_TEMPLATE_CLIN_CHEM_BCBLOOD = "clinical-chemistry-bcblood";
4706 19 Mar 18 olle 110   private static final String REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY = "middle-needle-biopsy";
4706 19 Mar 18 olle 111   private static final String REF_FORM_TEMPLATE_CLIN_CHEM = "clinical-chemistry";
4706 19 Mar 18 olle 112   private static final String REF_FORM_TEMPLATE_CLIN_PATHOLOGY = "clinical-pathology";
4706 19 Mar 18 olle 113   private static final String REF_FORM_TEMPLATE_CONSENT_REPORT = "consent-report";
4724 29 Mar 18 olle 114   private static final String REF_FORM_TEMPLATE_PAD_REFERRAL = "pad-referral";
4767 17 Apr 18 olle 115   private static final String REF_FORM_TEMPLATE_WHOLE_BLOOD = "whole-blood";
4767 17 Apr 18 olle 116   private static final String REF_FORM_TEMPLATE_FOLLOW_UP = "follow-up";
5700 05 Nov 19 olle 117   private static final String REF_FORM_TEMPLATE_FOLLOW_UP_TISSUE = "follow-up-tissue";
4833 07 Jun 18 olle 118   private static final String REF_FORM_TEMPLATE_FFPE_REFERRAL = "ffpe-referral";
4909 12 Jul 18 olle 119   private static final String REF_FORM_TEMPLATE_FFPE_SECTION_ORDER_FORM = "ffpe-section-order-form";
5702 06 Nov 19 olle 120   private static final String REF_FORM_TEMPLATE_NORDIC_TRIP_STICKER_FORM = "nordic-trip-sticker-form";
6783 25 Jul 22 olle 121   private static final String REF_FORM_TEMPLATE_LUCAS_STICKER_FORM = "lucas-sticker-form";
4706 19 Mar 18 olle 122
4706 19 Mar 18 olle 123   // Remember to add new template filename start constants to initializeRefFormTemplNameStartHashMap()
4951 30 Aug 18 olle 124   //private static final String TEMPLATE_FILENAME_START_PATIENT_INFO = "Patinfo";
5391 02 May 19 olle 125   /*
4951 30 Aug 18 olle 126   private static final String TEMPLATE_FILENAME_START_PATIENT_INFO = "SCAN-B-rec_Patinfo";
4724 29 Mar 18 olle 127   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BIOPSY = "SCAN-B-rec_Biopsi";
4706 19 Mar 18 olle 128   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD = "KlinKem-SCAN-B-blood-biobanken_remiss";
4724 29 Mar 18 olle 129   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD_LUNDCOPY = "KlinKem-blood-biobanken-lundkopia_remiss";
4706 19 Mar 18 olle 130   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BCBLOOD = "KlinKem-SCAN-B-bcblood-remiss";
4706 19 Mar 18 olle 131   private static final String TEMPLATE_FILENAME_START_MIDDLE_NEEDLE_BIOPSY = "PathBiopsy-SCAN-B-remiss";
4767 17 Apr 18 olle 132   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM = "Klinkem-SCAN-B-remiss";
4706 19 Mar 18 olle 133   private static final String TEMPLATE_FILENAME_START_CLIN_PATHOLOGY = "Path-SCAN-B-remiss";
4724 29 Mar 18 olle 134   //private static final String TEMPLATE_FILENAME_START_CONSENT_REPORT = "Consent-SCAN-B-remiss";
4724 29 Mar 18 olle 135   private static final String TEMPLATE_FILENAME_START_CONSENT_REPORT = "SCAN-B-rec_Samtyckesrapport";
4724 29 Mar 18 olle 136   private static final String TEMPLATE_FILENAME_START_PAD_REFERRAL = "SCAN-B-rec_PADremiss";
4767 17 Apr 18 olle 137   private static final String TEMPLATE_FILENAME_START_WHOLE_BLOOD = "SCAN-B-rec_BL-Helblod_CTC_ctDNA_serum";
4767 17 Apr 18 olle 138   private static final String TEMPLATE_FILENAME_START_FOLLOW_UP = "SCAN-B-rec_FU-CTC_ctDNA_serum";
4833 07 Jun 18 olle 139   private static final String TEMPLATE_FILENAME_START_FFPE_REFERRAL = "SCAN-B-rec_FFPE";
4909 12 Jul 18 olle 140   private static final String TEMPLATE_FILENAME_START_FFPE_SECTION_ORDER_FORM = "SCAN-B-rec_Snitt";
5391 02 May 19 olle 141   */
5391 02 May 19 olle 142   private static final String TEMPLATE_FILENAME_START_PATIENT_INFO = "Patinfo";
5391 02 May 19 olle 143   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BIOPSY = "Biopsi";
5391 02 May 19 olle 144   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD = "KlinKem-SCAN-B-blood-biobanken_remiss";
5391 02 May 19 olle 145   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD_LUNDCOPY = "KlinKem-blood-biobanken-lundkopia_remiss";
5391 02 May 19 olle 146   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM_BCBLOOD = "KlinKem-SCAN-B-bcblood-remiss";
5391 02 May 19 olle 147   private static final String TEMPLATE_FILENAME_START_MIDDLE_NEEDLE_BIOPSY = "PathBiopsy-SCAN-B-remiss";
5391 02 May 19 olle 148   private static final String TEMPLATE_FILENAME_START_CLIN_CHEM = "Klinkem-SCAN-B-remiss";
5391 02 May 19 olle 149   private static final String TEMPLATE_FILENAME_START_CLIN_PATHOLOGY = "Path-SCAN-B-remiss";
5391 02 May 19 olle 150   //private static final String TEMPLATE_FILENAME_START_CONSENT_REPORT = "Consent-SCAN-B-remiss";
5391 02 May 19 olle 151   private static final String TEMPLATE_FILENAME_START_CONSENT_REPORT = "Samtyckesrapport";
5391 02 May 19 olle 152   private static final String TEMPLATE_FILENAME_START_PAD_REFERRAL = "PADremiss";
5391 02 May 19 olle 153   private static final String TEMPLATE_FILENAME_START_WHOLE_BLOOD = "BL-Helblod_CTC_ctDNA_serum";
5391 02 May 19 olle 154   private static final String TEMPLATE_FILENAME_START_FOLLOW_UP = "FU-CTC_ctDNA_serum";
5700 05 Nov 19 olle 155   private static final String TEMPLATE_FILENAME_START_FOLLOW_UP_TISSUE = "_NA_";
5391 02 May 19 olle 156   private static final String TEMPLATE_FILENAME_START_FFPE_REFERRAL = "FFPE";
5391 02 May 19 olle 157   private static final String TEMPLATE_FILENAME_START_FFPE_SECTION_ORDER_FORM = "Snitt";
5702 06 Nov 19 olle 158   private static final String TEMPLATE_FILENAME_START_NORDIC_TRIP_STICKER_FORM = "_NA_";
6783 25 Jul 22 olle 159   private static final String TEMPLATE_FILENAME_START_LUCAS_STICKER_FORM = "Bronkoskopi";
4706 19 Mar 18 olle 160
4810 15 May 18 olle 161   private static final int NUM_BIOPSY_FORMS_DEFAULT = 2;
4774 20 Apr 18 olle 162   private static final int NUM_FOLLOW_UP_FORMS_DEFAULT = 8;
4774 20 Apr 18 olle 163
4706 19 Mar 18 olle 164   private static HashMap<String,String> refFormTemplNameStartHM;
5732 19 Nov 19 olle 165   private static HashMap<String,String> refFormTemplTypeIdentSuffixHM;
5695 04 Nov 19 olle 166   private static int SITE_PREFIX_LENGTH_DEFAULT = 2;
5695 04 Nov 19 olle 167   private int sitePrefixLength = SITE_PREFIX_LENGTH_DEFAULT;
4706 19 Mar 18 olle 168
4706 19 Mar 18 olle 169   public ReferralGeneratorServlet()
4706 19 Mar 18 olle 170   {}
4706 19 Mar 18 olle 171
4706 19 Mar 18 olle 172   @SuppressWarnings("unchecked")
4706 19 Mar 18 olle 173   @Override
4706 19 Mar 18 olle 174   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
4706 19 Mar 18 olle 175     throws ServletException, IOException 
4706 19 Mar 18 olle 176   {
4724 29 Mar 18 olle 177     String ID = req.getParameter("ID");
4706 19 Mar 18 olle 178     String cmd = req.getParameter("cmd");
4706 19 Mar 18 olle 179     JsonUtil.setJsonResponseHeaders(resp);
4724 29 Mar 18 olle 180     System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd);
4706 19 Mar 18 olle 181     
4706 19 Mar 18 olle 182     JSONObject json = new JSONObject();
4706 19 Mar 18 olle 183     json.put("status", "ok");
4706 19 Mar 18 olle 184     
4706 19 Mar 18 olle 185     JSONArray jsonMessages = new JSONArray();
4706 19 Mar 18 olle 186
5391 02 May 19 olle 187     //initializeRefFormTemplNameStartHashMap();
4706 19 Mar 18 olle 188
4724 29 Mar 18 olle 189     //final SessionControl sc = Meludi.getSessionControl(req);
5468 04 Jun 19 olle 190     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 191     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 192     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
4706 19 Mar 18 olle 193     DbControl dc = null;
5695 04 Nov 19 olle 194     // Get site prefix length
5695 04 Nov 19 olle 195     Boolean usesSitePrefix = Meludi.fetchUsesSitePrefix(sc.getActiveProjectId());
5695 04 Nov 19 olle 196     if (!usesSitePrefix)
5695 04 Nov 19 olle 197     {
5695 04 Nov 19 olle 198       sitePrefixLength = 0;
5695 04 Nov 19 olle 199     }
5391 02 May 19 olle 200     initializeRefFormTemplNameStartHashMap(sc);
5732 19 Nov 19 olle 201     initializeRefFormTemplTypeIdentSuffixHashMap(sc);
5732 19 Nov 19 olle 202
4706 19 Mar 18 olle 203     String progressId = "";
4724 29 Mar 18 olle 204     System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " After initialization");
4706 19 Mar 18 olle 205     try
4706 19 Mar 18 olle 206     {
4724 29 Mar 18 olle 207       if ("GetSites".equals(cmd))
4706 19 Mar 18 olle 208       {
4724 29 Mar 18 olle 209         json.put("sites", fetchJSONSites(Site.SORT_BY_NAME, sc.getActiveProjectId()));
4724 29 Mar 18 olle 210         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GetSites: json = " + json);
4724 29 Mar 18 olle 211       }
4724 29 Mar 18 olle 212       else if ("GetReferralStartInfo".equals(cmd))
4724 29 Mar 18 olle 213       {
5695 04 Nov 19 olle 214         dc = sc.newDbControl();
5695 04 Nov 19 olle 215         JSONObject jsonRefStartInfo = fetchReferralStartInfo(dc);
4908 12 Jul 18 olle 216         jsonRefStartInfo.put("sampleItemPrefix", Meludi.fetchSampleItemPrefix(sc.getActiveProjectId()));
4706 19 Mar 18 olle 217         json.put("refStartInfo", jsonRefStartInfo);
4706 19 Mar 18 olle 218       }
4911 12 Jul 18 olle 219       else if ("GetReferralCaseInfo".equals(cmd))
4911 12 Jul 18 olle 220       {
4911 12 Jul 18 olle 221         dc = sc.newDbControl();
4911 12 Jul 18 olle 222         String referralId = req.getParameter("itemname");
4911 12 Jul 18 olle 223         JSONObject caseInfo = fetchReferralCaseInfo(dc, referralId);
4911 12 Jul 18 olle 224         json.put("caseInfo", caseInfo);
4911 12 Jul 18 olle 225       }
4908 12 Jul 18 olle 226       else if ("UpdateUsedItemIdDigitsList".equals(cmd))
4706 19 Mar 18 olle 227       {
4908 12 Jul 18 olle 228         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " UpdateUsedItemIdDigitsList: Start");
5695 04 Nov 19 olle 229         dc = sc.newDbControl();
4908 12 Jul 18 olle 230         String startItemName = req.getParameter("startitemname");
5733 19 Nov 19 olle 231         int numSerialDigits = Meludi.fetchReferralNumDigits(sc.getActiveProjectId());
5733 19 Nov 19 olle 232         numSerialDigits = numSerialDigits - sitePrefixLength;
4908 12 Jul 18 olle 233         //String startitemIdDigits = Meludi.fetchRootDigits(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 234         String startitemIdDigits = Meludi.fetchReferralDigits(startItemName, sc.getActiveProjectId());
4909 12 Jul 18 olle 235         // FFPE section forms contain several items on one referral form set
4908 12 Jul 18 olle 236         int nofItems = Values.getInteger(req.getParameter("nofformsets"), 1);
4909 12 Jul 18 olle 237         if (startItemName.startsWith(Meludi.fetchFfpeBlockItemPrefix(sc.getActiveProjectId())))
4909 12 Jul 18 olle 238         {
5733 19 Nov 19 olle 239           numSerialDigits = Meludi.fetchFfpeBlockItemNumDigits(sc.getActiveProjectId());
4909 12 Jul 18 olle 240           nofItems = Values.getInteger(req.getParameter("nofFfpeBlocks"), 1);
4909 12 Jul 18 olle 241         }
4908 12 Jul 18 olle 242         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " UpdateUsedItemIdDigitsList: startitemIdDigits = " + startitemIdDigits + " nofItems = " + nofItems);
4706 19 Mar 18 olle 243
4908 12 Jul 18 olle 244         String itemIdDigitsPure = startitemIdDigits.substring(sitePrefixLength);
5733 19 Nov 19 olle 245         String nextitemIdDigitsPure = nextItemIdDigits(itemIdDigitsPure, numSerialDigits, nofItems);
5733 19 Nov 19 olle 246         //String nextitemIdDigitsPure = nextItemIdDigits(startitemIdDigits, numDigits, nofItems);
5733 19 Nov 19 olle 247         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " UpdateUsedItemIdDigitsList: itemIdDigitsPure = " + itemIdDigitsPure + " numSerialDigits = " + numSerialDigits + " nofItems = " + nofItems + " nextitemIdDigitsPure = " + nextitemIdDigitsPure);
4706 19 Mar 18 olle 248
4706 19 Mar 18 olle 249         // Get date string
4706 19 Mar 18 olle 250         DateToStringConverter d1 = new DateToStringConverter(new SimpleDateFormat("yyyy-MM-dd"));
4706 19 Mar 18 olle 251         String lastUpdated = d1.convert(new Date());
4908 12 Jul 18 olle 252         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " UpdateUsedItemIdDigitsList: lastUpdated = " + lastUpdated);
4706 19 Mar 18 olle 253
5695 04 Nov 19 olle 254         /*
4706 19 Mar 18 olle 255         String refStartInfoFilePath = fetchReferralStartInfoFilePath();
4908 12 Jul 18 olle 256         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " UpdateUsedItemIdDigitsList: refStartInfoFilePath = " + refStartInfoFilePath);
4706 19 Mar 18 olle 257         java.io.File file = new java.io.File(refStartInfoFilePath);
4706 19 Mar 18 olle 258         FileWriter fileWriter = null;
4706 19 Mar 18 olle 259         //
4706 19 Mar 18 olle 260         try
4706 19 Mar 18 olle 261         {
4706 19 Mar 18 olle 262           fileWriter = new FileWriter(file);
4706 19 Mar 18 olle 263           String separator = "\t";
4908 12 Jul 18 olle 264           fileWriter.write(nextitemIdDigitsPure + separator + lastUpdated + "\n");
4706 19 Mar 18 olle 265           //
4706 19 Mar 18 olle 266           fileWriter.flush();
4706 19 Mar 18 olle 267           fileWriter.close();
4706 19 Mar 18 olle 268         }
4706 19 Mar 18 olle 269         catch(IOException ex)
4706 19 Mar 18 olle 270         {
4706 19 Mar 18 olle 271           System.out.println(new Date() + " ReferralGeneratorServlet::doGet(): Could not create filewriter for refStartInfoFilePath = " + refStartInfoFilePath);
4706 19 Mar 18 olle 272         }
5695 04 Nov 19 olle 273         */
5695 04 Nov 19 olle 274         // Get referral form start info file
5695 04 Nov 19 olle 275         File refStartInfoFile = fetchReferralStartInfoFile(dc);
5695 04 Nov 19 olle 276         System.out.println("ReferralGeneratorServlet::doGet(): cmd = \" + cmd + \" refStartInfoFile = " + refStartInfoFile);
5695 04 Nov 19 olle 277         try
5695 04 Nov 19 olle 278         {
5695 04 Nov 19 olle 279 /*
5695 04 Nov 19 olle 280           // Read and store current file contents
5695 04 Nov 19 olle 281           List<String> lines = new ArrayList<String>();
5695 04 Nov 19 olle 282           InputStream in = refStartInfoFile.getDownloadStream(0);
5695 04 Nov 19 olle 283           InputStreamReader ipsr = new InputStreamReader(in, "UTF-8");
5695 04 Nov 19 olle 284           BufferedReader br = new BufferedReader(ipsr);
5695 04 Nov 19 olle 285           // Read data lines
5695 04 Nov 19 olle 286           String rawLine;
5695 04 Nov 19 olle 287           while ((rawLine = br.readLine()) != null)
5695 04 Nov 19 olle 288           {
5695 04 Nov 19 olle 289             lines.add(rawLine);
5695 04 Nov 19 olle 290           }
5695 04 Nov 19 olle 291           br.close();
5695 04 Nov 19 olle 292           ipsr.close();
5695 04 Nov 19 olle 293           in.close();
5695 04 Nov 19 olle 294           //
5695 04 Nov 19 olle 295 */
5695 04 Nov 19 olle 296           OutputStream out = refStartInfoFile.getUploadStream(false);
5695 04 Nov 19 olle 297           OutputStreamWriter writer = new OutputStreamWriter(out);
5695 04 Nov 19 olle 298 /*
5695 04 Nov 19 olle 299           // Write back old contents
5695 04 Nov 19 olle 300           for (int i = 0; i < lines.size(); i++)
5695 04 Nov 19 olle 301           {
5695 04 Nov 19 olle 302             String line = (String)lines.get(i);
5695 04 Nov 19 olle 303             writer.write(line + "\n");
5695 04 Nov 19 olle 304           }
5695 04 Nov 19 olle 305 */
5695 04 Nov 19 olle 306           // Write new content
5695 04 Nov 19 olle 307           String separator = "\t";
5695 04 Nov 19 olle 308           String line = nextitemIdDigitsPure + separator + lastUpdated + "\n";
5695 04 Nov 19 olle 309           System.out.println("ReferralGeneratorServlet::doGet(): cmd = \" + cmd + \" line = " + line);
5695 04 Nov 19 olle 310           //writer.write(line + "\n");
5695 04 Nov 19 olle 311           writer.write(line);
5695 04 Nov 19 olle 312           //
5695 04 Nov 19 olle 313           writer.close();
5695 04 Nov 19 olle 314           out.close();
5695 04 Nov 19 olle 315         }
5695 04 Nov 19 olle 316         catch(Exception e)
5695 04 Nov 19 olle 317         {
5695 04 Nov 19 olle 318           System.out.println(new Date() + " ReferralGeneratorServlet::doGet(): cmd = \" + cmd + \": Exception e: " + e);
5695 04 Nov 19 olle 319         }
5695 04 Nov 19 olle 320         dc.commit();
4908 12 Jul 18 olle 321         String message = "Start item ID list updated to start at " + nextitemIdDigitsPure;
4706 19 Mar 18 olle 322         jsonMessages.add(message);
4706 19 Mar 18 olle 323       }
4706 19 Mar 18 olle 324       else if ("GetReferralTemplateInfo".equals(cmd))
4706 19 Mar 18 olle 325       {
4706 19 Mar 18 olle 326         dc = sc.newDbControl();
4706 19 Mar 18 olle 327
4724 29 Mar 18 olle 328         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GetReferralTemplateInfo: Start");
4706 19 Mar 18 olle 329         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
4724 29 Mar 18 olle 330         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GetReferralTemplateInfo: After permission check");
4706 19 Mar 18 olle 331
4706 19 Mar 18 olle 332         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
4706 19 Mar 18 olle 333         req.setCharacterEncoding("UTF-8");
4706 19 Mar 18 olle 334         JSONArray jsonMultipleTemplFiles = new JSONArray();
4706 19 Mar 18 olle 335
4706 19 Mar 18 olle 336         // Get referral form parameters
4706 19 Mar 18 olle 337         String site = Values.getString(req.getParameter("site"), "");
4724 29 Mar 18 olle 338         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " site = " + site);
4706 19 Mar 18 olle 339         String referralVariant = Values.getString(req.getParameter("referralvariant"), REF_VARIANT_KIT_1_STANDARD);
4724 29 Mar 18 olle 340         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " site = " + site + " referralVariant = " + referralVariant);
4706 19 Mar 18 olle 341
4706 19 Mar 18 olle 342         //
4706 19 Mar 18 olle 343         // Form batch file 1 (Referral forms intended to be printed on referral form sheets)
4706 19 Mar 18 olle 344         //
4706 19 Mar 18 olle 345         if (referralVariant.equals(REF_VARIANT_KIT_2_NEOADJUVANT))
4706 19 Mar 18 olle 346         {
4706 19 Mar 18 olle 347           // Biopsy ClinChem Form 
4767 17 Apr 18 olle 348           //jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 349
4706 19 Mar 18 olle 350           // Middle Needle Biopsy Form 
4706 19 Mar 18 olle 351           jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 352         }
4767 17 Apr 18 olle 353         // Biopsy ClinChem Form 
4767 17 Apr 18 olle 354         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 355
4767 17 Apr 18 olle 356         // Whole Blood Form 
4767 17 Apr 18 olle 357         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_WHOLE_BLOOD, jsonMultipleTemplFiles);
4767 17 Apr 18 olle 358
4767 17 Apr 18 olle 359         // Follow Up Form 
4767 17 Apr 18 olle 360         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_FOLLOW_UP, jsonMultipleTemplFiles);
4767 17 Apr 18 olle 361
5700 05 Nov 19 olle 362         // Follow Up Tissue Form 
5700 05 Nov 19 olle 363         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_FOLLOW_UP_TISSUE, jsonMultipleTemplFiles);
5700 05 Nov 19 olle 364
4706 19 Mar 18 olle 365         // ClinChem Form 
4706 19 Mar 18 olle 366         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CLIN_CHEM, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 367
4706 19 Mar 18 olle 368         // Clin Pathology Form 
4706 19 Mar 18 olle 369         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CLIN_PATHOLOGY, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 370           
4706 19 Mar 18 olle 371         // BC Blood Form
4706 19 Mar 18 olle 372         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CLIN_CHEM_BCBLOOD, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 373
5702 06 Nov 19 olle 374         // Nordic Trip Sticker Form 
5702 06 Nov 19 olle 375         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_NORDIC_TRIP_STICKER_FORM, jsonMultipleTemplFiles);
5702 06 Nov 19 olle 376
6783 25 Jul 22 olle 377         // LUCAS Sticker Form 
6783 25 Jul 22 olle 378         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_LUCAS_STICKER_FORM, jsonMultipleTemplFiles);
6783 25 Jul 22 olle 379
4706 19 Mar 18 olle 380         //
4706 19 Mar 18 olle 381         // Form batch file 2 (Referral forms intended to be printed on regular paper)
4706 19 Mar 18 olle 382         //
4706 19 Mar 18 olle 383         // Consent Patient Info  Form
4706 19 Mar 18 olle 384         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_PATIENT_INFO, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 385
4706 19 Mar 18 olle 386         // Consent Report Form
4706 19 Mar 18 olle 387         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CONSENT_REPORT, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 388           
4706 19 Mar 18 olle 389         // Blood Form
4706 19 Mar 18 olle 390         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 391
4706 19 Mar 18 olle 392         // Blood Form (Lund copy)
4706 19 Mar 18 olle 393         jsonMultipleTemplFiles = fetchMultipleTemplateFiles(dc, site, REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD_LUNDCOPY, jsonMultipleTemplFiles);
4706 19 Mar 18 olle 394
4706 19 Mar 18 olle 395         json.put("multTemplates", jsonMultipleTemplFiles);
4706 19 Mar 18 olle 396       }
4706 19 Mar 18 olle 397       else if ("GenerateReferralForms".equals(cmd))
4706 19 Mar 18 olle 398       {
4706 19 Mar 18 olle 399         dc = sc.newDbControl();
4706 19 Mar 18 olle 400
4724 29 Mar 18 olle 401         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: Start");
4706 19 Mar 18 olle 402         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
4724 29 Mar 18 olle 403         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: After permission check");
4706 19 Mar 18 olle 404
4706 19 Mar 18 olle 405         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
4706 19 Mar 18 olle 406         req.setCharacterEncoding("UTF-8");
4706 19 Mar 18 olle 407
4706 19 Mar 18 olle 408         // Get referral form parameters
4706 19 Mar 18 olle 409         String referralVariant = Values.getString(req.getParameter("referralvariant"), REF_VARIANT_KIT_1_STANDARD);
4908 12 Jul 18 olle 410         String startItemName = Values.getString(req.getParameter("startitemname"), "");
4706 19 Mar 18 olle 411         int nofFormSets = Values.getInteger(req.getParameter("nofformsets"), 1);
4908 12 Jul 18 olle 412         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: referralVariant = " + referralVariant + " startItemName = " + startItemName + " nofFormSets = " + nofFormSets);
4908 12 Jul 18 olle 413 //System.out.println(new Date() + " doGet(): cmd = \"" + cmd + "\" startItemName = " + startItemName + " nofFormSets = " + nofFormSets);
4908 12 Jul 18 olle 414         //String sitePrefix = startItemName.substring(0,sitePrefixLength);
4908 12 Jul 18 olle 415         //String itemIdRootDigits = Meludi.fetchRootDigits(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 416         String itemIdRootDigits = Meludi.fetchReferralDigits(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 417         String sitePrefix = itemIdRootDigits.substring(0,sitePrefixLength);
4908 12 Jul 18 olle 418         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: startItemName = " + startItemName + " itemIdRootDigits = " + itemIdRootDigits + " sitePrefix = " + sitePrefix);
5733 19 Nov 19 olle 419         int refNumDigits = Meludi.fetchReferralNumDigits(sc.getActiveProjectId());
5733 19 Nov 19 olle 420         int numSerialDigits = refNumDigits - sitePrefixLength;
4706 19 Mar 18 olle 421
4706 19 Mar 18 olle 422         SimpleProgressReporter progress = new SimpleProgressReporter(null);
4724 29 Mar 18 olle 423         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: After creating simpleProgressReporter");
4706 19 Mar 18 olle 424         progressId = REFERRAL_GENERATION_PROGRESS_ID;
4706 19 Mar 18 olle 425         sc.setSessionSetting(progressId, progress);
4724 29 Mar 18 olle 426         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: After setting progress settings");
4706 19 Mar 18 olle 427         progress.display(0, "Generating referral forms...");
4724 29 Mar 18 olle 428         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: After displaying progress");
4706 19 Mar 18 olle 429         // Set estimated fractions of process time
4706 19 Mar 18 olle 430         float progressBatchFile1Fraction = 0.43f;
4706 19 Mar 18 olle 431         float progressBatchFile2Fraction = 0.57f;
4706 19 Mar 18 olle 432         if (referralVariant.equals(REF_VARIANT_KIT_2_NEOADJUVANT))
4706 19 Mar 18 olle 433         {
4706 19 Mar 18 olle 434           progressBatchFile1Fraction = 0.56f;
4706 19 Mar 18 olle 435           progressBatchFile2Fraction = 0.44f;
4706 19 Mar 18 olle 436         }
4706 19 Mar 18 olle 437         int progressOffset = 0;
4706 19 Mar 18 olle 438         int progressPct = 0;
4706 19 Mar 18 olle 439
4706 19 Mar 18 olle 440         // List of used form template filenames for log purposes 
4706 19 Mar 18 olle 441         List<String> formTemplateFileList = new ArrayList<String>();
4706 19 Mar 18 olle 442         // Get new PdfUtil instance
4706 19 Mar 18 olle 443         String title = "Title";
4706 19 Mar 18 olle 444         String creator = "Creator";
4724 29 Mar 18 olle 445         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: calling new PdfUtil(" + title + ", " + creator + ")");
4706 19 Mar 18 olle 446         PdfUtil pdfUtil = new PdfUtil(title, creator);
4724 29 Mar 18 olle 447         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: After creating pdfUtil");
4706 19 Mar 18 olle 448         // PdfUtil.open(OutputStream out) must be called before text or images are added (canvas is needed)
4706 19 Mar 18 olle 449
4706 19 Mar 18 olle 450         //
4706 19 Mar 18 olle 451         // Form batch file 1 (Referral forms intended to be printed on referral form sheets)
4706 19 Mar 18 olle 452         //
5201 07 Jan 19 olle 453         Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5201 07 Jan 19 olle 454         String projectName = activeProject.getName();
5201 07 Jan 19 olle 455         String pdfOutFilePath = fetchReferralFormBatchFilePath(1, projectName);
4724 29 Mar 18 olle 456         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: pdfOutFilePath = " + pdfOutFilePath);
4706 19 Mar 18 olle 457         java.io.File pdfOutFile = new java.io.File(pdfOutFilePath);
4706 19 Mar 18 olle 458         OutputStream pdfOutStream = new FileOutputStream(pdfOutFile);
4706 19 Mar 18 olle 459         pdfUtil.open(pdfOutStream);
4706 19 Mar 18 olle 460
4908 12 Jul 18 olle 461         //String itemPrefix = Meludi.fetchSampleItemPrefix(sc.getActiveProjectId());
4908 12 Jul 18 olle 462         String itemPrefix = Meludi.fetchReferralPrefix(sc.getActiveProjectId());
4908 12 Jul 18 olle 463         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: itemPrefix = " + itemPrefix);
4706 19 Mar 18 olle 464
4908 12 Jul 18 olle 465         //String itemIdDigits = startItemName;
4908 12 Jul 18 olle 466         String itemIdDigits = itemIdRootDigits;
4908 12 Jul 18 olle 467         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: itemIdDigits = " + itemIdDigits);
4706 19 Mar 18 olle 468         for (int i = 0; i < nofFormSets; i++)
4706 19 Mar 18 olle 469         {
5700 05 Nov 19 olle 470           System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " i = " + i + " nofFormSets = " + nofFormSets + " formTemplateFileList = " + formTemplateFileList);
4706 19 Mar 18 olle 471           progressPct = Math.round(progressBatchFile1Fraction*100*i/nofFormSets);
4706 19 Mar 18 olle 472           progress.display(progressPct, "Generating referral forms...");
4706 19 Mar 18 olle 473           if (i > 0)
4706 19 Mar 18 olle 474           {
5733 19 Nov 19 olle 475             itemIdDigits = nextItemIdDigits(itemIdDigits, numSerialDigits);
4706 19 Mar 18 olle 476           }
4908 12 Jul 18 olle 477           //String itemIdDigitsBiopsy = itemIdDigits + "C";
4908 12 Jul 18 olle 478           String itemId = itemPrefix + itemIdDigits;
4908 12 Jul 18 olle 479           System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: i = " + i + " itemId = " + itemId);
6783 25 Jul 22 olle 480           if (projectName.equals("LUCAS"))
6783 25 Jul 22 olle 481           {            
6783 25 Jul 22 olle 482             // LUCAS Sticker Form
6783 25 Jul 22 olle 483             pdfUtil = createLucasStickerForm(dc, pdfUtil, itemId, formTemplateFileList);
6783 25 Jul 22 olle 484             continue;
6783 25 Jul 22 olle 485           }
4908 12 Jul 18 olle 486           String itemIdDigitsBiopsy = itemId;
4706 19 Mar 18 olle 487
4706 19 Mar 18 olle 488           if (referralVariant.equals(REF_VARIANT_KIT_2_NEOADJUVANT))
4706 19 Mar 18 olle 489           {
4767 17 Apr 18 olle 490             // Biopsy Biopsy ClinChem Form 
4908 12 Jul 18 olle 491             //pdfUtil = createBiopsyClinChemForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 492
4706 19 Mar 18 olle 493             // Middle Needle Biopsy Form 
4908 12 Jul 18 olle 494             pdfUtil = createMiddleNeedleBiopsyForm(dc, pdfUtil, itemId, formTemplateFileList);            
4706 19 Mar 18 olle 495           }
4706 19 Mar 18 olle 496
4767 17 Apr 18 olle 497           // Biopsy ClinChem Form 
5700 05 Nov 19 olle 498           int numBiopsyForms = fetchNumBiopsyForms(dc);
4810 15 May 18 olle 499           for (int j = 0; j < numBiopsyForms; j++)
4810 15 May 18 olle 500           {
4908 12 Jul 18 olle 501             pdfUtil = createBiopsyClinChemForm(dc, pdfUtil, itemId, formTemplateFileList);
4810 15 May 18 olle 502           }
4767 17 Apr 18 olle 503
4767 17 Apr 18 olle 504           // Whole Blood Form 
4908 12 Jul 18 olle 505           pdfUtil = createWholeBloodForm(dc, pdfUtil, itemId, formTemplateFileList);
4767 17 Apr 18 olle 506
4774 20 Apr 18 olle 507           // Follow Up Form(s)
5700 05 Nov 19 olle 508           int numFollowUpForms = fetchNumFollowUpForms(dc);
4774 20 Apr 18 olle 509           for (int j = 0; j < numFollowUpForms; j++)
4774 20 Apr 18 olle 510           {
4908 12 Jul 18 olle 511             pdfUtil = createFollowUpForm(dc, pdfUtil, itemId, formTemplateFileList);
4774 20 Apr 18 olle 512           }
4767 17 Apr 18 olle 513
5700 05 Nov 19 olle 514           // Follow Up Tissue Form
5700 05 Nov 19 olle 515           pdfUtil = createFollowUpTissueForm(dc, pdfUtil, itemId, formTemplateFileList);
5700 05 Nov 19 olle 516           
4706 19 Mar 18 olle 517           // ClinChem Form 
4908 12 Jul 18 olle 518           pdfUtil = createClinChemForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 519
4706 19 Mar 18 olle 520           // Clin Pathology Form 
4908 12 Jul 18 olle 521           pdfUtil = createClinPathologyForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 522           
4706 19 Mar 18 olle 523           // BC Blood Form
4908 12 Jul 18 olle 524           pdfUtil = createClinChemBcBloodForm(dc, pdfUtil, itemId, formTemplateFileList);
5702 06 Nov 19 olle 525
5702 06 Nov 19 olle 526           // Nordic Trip Sticker Form
5702 06 Nov 19 olle 527           pdfUtil = createNordicTripStickerForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 528         }
4706 19 Mar 18 olle 529         pdfUtil.close();
4706 19 Mar 18 olle 530 //System.out.println(new Date() + " doGet(): cmd = \"" + cmd + "\" PDF output file = " + pdfOutFilePath + " created");
4706 19 Mar 18 olle 531
6783 25 Jul 22 olle 532         if (!projectName.equals("LUCAS"))
6783 25 Jul 22 olle 533         {            
6783 25 Jul 22 olle 534           //
6783 25 Jul 22 olle 535           // Form batch file 2 (Referral forms intended to be printed on regular paper)
6783 25 Jul 22 olle 536           //
6783 25 Jul 22 olle 537           pdfUtil = new PdfUtil(title, creator);
6783 25 Jul 22 olle 538           pdfOutFilePath = fetchReferralFormBatchFilePath(2, projectName);
6783 25 Jul 22 olle 539           System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: pdfOutFilePath = " + pdfOutFilePath);
6783 25 Jul 22 olle 540           pdfOutFile = new java.io.File(pdfOutFilePath);
6783 25 Jul 22 olle 541           pdfOutStream = new FileOutputStream(pdfOutFile);
6783 25 Jul 22 olle 542           pdfUtil.open(pdfOutStream);
4706 19 Mar 18 olle 543
6783 25 Jul 22 olle 544           //itemIdDigits = startItemName;
6783 25 Jul 22 olle 545           itemIdDigits = itemIdRootDigits;
4706 19 Mar 18 olle 546
6783 25 Jul 22 olle 547           progressOffset += progressPct;
6783 25 Jul 22 olle 548           for (int i = 0; i < nofFormSets; i++)
4706 19 Mar 18 olle 549           {
6783 25 Jul 22 olle 550             progressPct = progressOffset + Math.round(progressBatchFile2Fraction*100*i/nofFormSets);
6783 25 Jul 22 olle 551             if (progressPct > 100)
6783 25 Jul 22 olle 552             {
6783 25 Jul 22 olle 553               progressPct = 100;
6783 25 Jul 22 olle 554             }
6783 25 Jul 22 olle 555             progress.display(progressPct, "Generating referral forms...");
6783 25 Jul 22 olle 556             if (i > 0)
6783 25 Jul 22 olle 557             {
6783 25 Jul 22 olle 558               itemIdDigits = nextItemIdDigits(itemIdDigits, numSerialDigits);
6783 25 Jul 22 olle 559             }
6783 25 Jul 22 olle 560             //String itemIdDigitsBiopsy = itemIdDigits + "C";
6783 25 Jul 22 olle 561             String itemId = itemPrefix + itemIdDigits;
6783 25 Jul 22 olle 562             String itemIdDigitsBiopsy = itemId;
4706 19 Mar 18 olle 563
6783 25 Jul 22 olle 564             // Consent Patient Info  Form
6783 25 Jul 22 olle 565             //pdfUtil = createPatientInfoForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 566
6783 25 Jul 22 olle 567             // Consent Report Form
6783 25 Jul 22 olle 568             pdfUtil = createConsentReportForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 569           
6783 25 Jul 22 olle 570             // PAD Referral Form
6783 25 Jul 22 olle 571             pdfUtil = createPadReferralForm(dc, pdfUtil, itemId, formTemplateFileList);
4724 29 Mar 18 olle 572           
6783 25 Jul 22 olle 573             // Blood Form
6783 25 Jul 22 olle 574             pdfUtil = createClinChemBloodForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 575           
6783 25 Jul 22 olle 576             // Blood Form (Lund copy)
6783 25 Jul 22 olle 577             pdfUtil = createClinChemBloodLundCopyForm(dc, pdfUtil, itemId, formTemplateFileList);
6783 25 Jul 22 olle 578           }
6783 25 Jul 22 olle 579           pdfUtil.close();
4706 19 Mar 18 olle 580         }
6783 25 Jul 22 olle 581         System.out.println(new Date() + " doGet(): cmd = \"" + cmd + "\" PDF output file = " + pdfOutFilePath + " created");
4706 19 Mar 18 olle 582
4706 19 Mar 18 olle 583         progress.display(100, "");
4908 12 Jul 18 olle 584         updateReferralFormLogFile(dc, referralVariant, itemIdRootDigits, nofFormSets, formTemplateFileList);
4706 19 Mar 18 olle 585         dc.commit();
4908 12 Jul 18 olle 586         String endItemName = itemPrefix + itemIdDigits;
4908 12 Jul 18 olle 587         String message = "Referral forms created for item ID " + startItemName + "-" + endItemName;
4724 29 Mar 18 olle 588         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + " GenerateReferralForms: message = " + message);
4706 19 Mar 18 olle 589         jsonMessages.add(message);
4706 19 Mar 18 olle 590       }
4833 07 Jun 18 olle 591       else if ("GenerateFfpeReferralForm".equals(cmd))
4833 07 Jun 18 olle 592       {
4833 07 Jun 18 olle 593         dc = sc.newDbControl();
4833 07 Jun 18 olle 594
4833 07 Jun 18 olle 595         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
4833 07 Jun 18 olle 596
4833 07 Jun 18 olle 597         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
4833 07 Jun 18 olle 598         req.setCharacterEncoding("UTF-8");
4833 07 Jun 18 olle 599
4833 07 Jun 18 olle 600         // Get referral form parameters
4833 07 Jun 18 olle 601         String referralVariant = Values.getString(req.getParameter("referralvariant"), REF_VARIANT_KIT_1_STANDARD);
4908 12 Jul 18 olle 602         String startItemName = Values.getString(req.getParameter("startitemname"), "");
4833 07 Jun 18 olle 603         int nofFormSets = Values.getInteger(req.getParameter("nofformsets"), 1);
4833 07 Jun 18 olle 604         String pad = Values.getString(req.getParameter("pad"), "");
5028 15 Oct 18 olle 605         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + ": referralVariant = " + referralVariant + " startItemName = " + startItemName + " nofFormSets = " + nofFormSets + " pad = " + pad);
4908 12 Jul 18 olle 606         //String itemIdRootDigits = Meludi.fetchRootDigits(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 607         String itemIdRootDigits = Meludi.fetchReferralDigits(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 608         String sitePrefix = itemIdRootDigits.substring(0,sitePrefixLength);
5733 19 Nov 19 olle 609         int numDigits = Meludi.fetchFfpeBlockItemNumDigits(sc.getActiveProjectId());
4833 07 Jun 18 olle 610
4865 20 Jun 18 olle 611         // Get case item from referral ID in order to get patient item
4908 12 Jul 18 olle 612         String referralId = startItemName;
4865 20 Jun 18 olle 613         Case theCase = Case.findByReferralId(dc, referralId);
4865 20 Jun 18 olle 614         Patient patient = Patient.findByCase(dc, theCase);
4865 20 Jun 18 olle 615         patient.loadDefaultAnnotations(dc);
4865 20 Jun 18 olle 616         // Use stored annotation snapshots for performance reasons
4865 20 Jun 18 olle 617         SnapshotManager manager = new SnapshotManager();        
4865 20 Jun 18 olle 618         String personalNumber = (String) Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, manager, patient.getBioSource());
4865 20 Jun 18 olle 619         String familyName = (String) Annotationtype.FAMILY_NAME.getAnnotationValue(dc, manager, patient.getBioSource());
4865 20 Jun 18 olle 620         String allFirstNames = (String) Annotationtype.ALL_FIRST_NAMES.getAnnotationValue(dc, manager, patient.getBioSource());
4866 21 Jun 18 olle 621         // Add hyphen before last four digits in personal number
4866 21 Jun 18 olle 622         int pnrLen = personalNumber.length();
4866 21 Jun 18 olle 623         String pnrFirstPart = personalNumber.substring(0, pnrLen - 4);
4866 21 Jun 18 olle 624         String pnrLastPart = personalNumber.substring(pnrLen - 4);
4866 21 Jun 18 olle 625         personalNumber = pnrFirstPart + "-" + pnrLastPart;
4865 20 Jun 18 olle 626         // Generate referral form
4833 07 Jun 18 olle 627         SimpleProgressReporter progress = new SimpleProgressReporter(null);
4833 07 Jun 18 olle 628         progressId = REFERRAL_GENERATION_PROGRESS_ID;
4833 07 Jun 18 olle 629         sc.setSessionSetting(progressId, progress);
4833 07 Jun 18 olle 630         progress.display(0, "Generating referral forms...");
4833 07 Jun 18 olle 631         // Set estimated fractions of process time
4833 07 Jun 18 olle 632         float progressBatchFile1Fraction = 0.43f;
4833 07 Jun 18 olle 633         float progressBatchFile2Fraction = 0.57f;
4833 07 Jun 18 olle 634         int progressOffset = 0;
4833 07 Jun 18 olle 635         int progressPct = 0;
4833 07 Jun 18 olle 636
4833 07 Jun 18 olle 637         // List of used form template filenames for log purposes 
4833 07 Jun 18 olle 638         List<String> formTemplateFileList = new ArrayList<String>();
4833 07 Jun 18 olle 639         // Get new PdfUtil instance
4833 07 Jun 18 olle 640         String title = "Title";
4833 07 Jun 18 olle 641         String creator = "Creator";
4833 07 Jun 18 olle 642
4833 07 Jun 18 olle 643         //
4833 07 Jun 18 olle 644         // Form batch file 2 (Referral forms intended to be printed on regular paper)
4833 07 Jun 18 olle 645         //
4833 07 Jun 18 olle 646         PdfUtil pdfUtil = new PdfUtil(title, creator);
5201 07 Jan 19 olle 647         Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5201 07 Jan 19 olle 648         String projectName = activeProject.getName();
5201 07 Jan 19 olle 649         String pdfOutFilePath = fetchReferralFormBatchFilePath(2, projectName);
4833 07 Jun 18 olle 650         java.io.File pdfOutFile = new java.io.File(pdfOutFilePath);
4833 07 Jun 18 olle 651         OutputStream pdfOutStream = new FileOutputStream(pdfOutFile);
4833 07 Jun 18 olle 652         pdfUtil.open(pdfOutStream);
4833 07 Jun 18 olle 653
4908 12 Jul 18 olle 654         //String itemPrefix = Meludi.fetchSampleItemPrefix(sc.getActiveProjectId());
4908 12 Jul 18 olle 655         String itemPrefix = Meludi.fetchReferralPrefix(sc.getActiveProjectId());
4833 07 Jun 18 olle 656
4908 12 Jul 18 olle 657         String itemIdDigits = itemIdRootDigits;
4833 07 Jun 18 olle 658
4833 07 Jun 18 olle 659         progressOffset += progressPct;
4833 07 Jun 18 olle 660         for (int i = 0; i < nofFormSets; i++)
4833 07 Jun 18 olle 661         {
4833 07 Jun 18 olle 662           progressPct = progressOffset + Math.round(progressBatchFile2Fraction*100*i/nofFormSets);
4833 07 Jun 18 olle 663           if (progressPct > 100)
4833 07 Jun 18 olle 664           {
4833 07 Jun 18 olle 665             progressPct = 100;
4833 07 Jun 18 olle 666           }
4833 07 Jun 18 olle 667           progress.display(progressPct, "Generating referral forms...");
4833 07 Jun 18 olle 668           if (i > 0)
4833 07 Jun 18 olle 669           {
5733 19 Nov 19 olle 670             itemIdDigits = nextItemIdDigits(itemIdDigits, numDigits);
4833 07 Jun 18 olle 671           }
4908 12 Jul 18 olle 672           //String itemIdDigitsBiopsy = itemIdDigits + "C";
4908 12 Jul 18 olle 673           String itemId = itemPrefix + itemIdDigits;
4908 12 Jul 18 olle 674           String itemIdDigitsBiopsy = itemId;
4833 07 Jun 18 olle 675
4833 07 Jun 18 olle 676           // FFPE referral Form
4908 12 Jul 18 olle 677           pdfUtil = createFfpeForm(dc, pdfUtil, itemId, pad, personalNumber, familyName, allFirstNames, formTemplateFileList);
4833 07 Jun 18 olle 678         }
4833 07 Jun 18 olle 679         pdfUtil.close();
4833 07 Jun 18 olle 680
4833 07 Jun 18 olle 681         progress.display(100, "");
4908 12 Jul 18 olle 682         updateReferralFormLogFile(dc, referralVariant, itemIdRootDigits, nofFormSets, formTemplateFileList);
4833 07 Jun 18 olle 683         dc.commit();
4908 12 Jul 18 olle 684         //String endItemName = itemPrefix + itemIdDigits;
4908 12 Jul 18 olle 685         String message = "FFPE referral form created for referral ID " + startItemName;
4833 07 Jun 18 olle 686         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + ": message = " + message);
4833 07 Jun 18 olle 687         jsonMessages.add(message);
4833 07 Jun 18 olle 688       }
5158 30 Nov 18 olle 689       else if ("GenerateFfpeBlockOrderForm".equals(cmd))
5158 30 Nov 18 olle 690       {
5158 30 Nov 18 olle 691         dc = sc.newDbControl();
5158 30 Nov 18 olle 692
5158 30 Nov 18 olle 693         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
5158 30 Nov 18 olle 694
5158 30 Nov 18 olle 695         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
5158 30 Nov 18 olle 696         req.setCharacterEncoding("UTF-8");
5158 30 Nov 18 olle 697
5158 30 Nov 18 olle 698         // Get referral form parameters
5158 30 Nov 18 olle 699         String referralVariant = Values.getString(req.getParameter("referralvariant"), REF_VARIANT_KIT_1_STANDARD);
5158 30 Nov 18 olle 700         String jsonCaseListStr = Values.getString(req.getParameter("caselist"), "");
5158 30 Nov 18 olle 701         // Get list of extract source item names
5158 30 Nov 18 olle 702         List<String> caseIdList = new ArrayList<String>();
5158 30 Nov 18 olle 703         JSONArray jsonItems = (JSONArray)new JSONParser().parse(jsonCaseListStr);
5158 30 Nov 18 olle 704         
5158 30 Nov 18 olle 705         // Use stored annotation snapshots for performance reasons
5158 30 Nov 18 olle 706         SnapshotManager manager = new SnapshotManager();        
5158 30 Nov 18 olle 707         // Generate referral form
5158 30 Nov 18 olle 708         SimpleProgressReporter progress = new SimpleProgressReporter(null);
5158 30 Nov 18 olle 709         progressId = REFERRAL_GENERATION_PROGRESS_ID;
5158 30 Nov 18 olle 710         sc.setSessionSetting(progressId, progress);
5158 30 Nov 18 olle 711         progress.display(0, "Generating referral forms...");
5158 30 Nov 18 olle 712         // Set estimated fractions of process time
5158 30 Nov 18 olle 713         float progressBatchFile1Fraction = 0.43f;
5158 30 Nov 18 olle 714         float progressBatchFile2Fraction = 0.57f;
5158 30 Nov 18 olle 715         int progressOffset = 0;
5158 30 Nov 18 olle 716         int progressPct = 0;
5158 30 Nov 18 olle 717
5158 30 Nov 18 olle 718         // List of used form template filenames for log purposes 
5158 30 Nov 18 olle 719         List<String> formTemplateFileList = new ArrayList<String>();
5158 30 Nov 18 olle 720         // Get new PdfUtil instance
5158 30 Nov 18 olle 721         String title = "Title";
5158 30 Nov 18 olle 722         String creator = "Creator";
5158 30 Nov 18 olle 723
5158 30 Nov 18 olle 724         //
5158 30 Nov 18 olle 725         // Form batch file 2 (Referral forms intended to be printed on regular paper)
5158 30 Nov 18 olle 726         //
5158 30 Nov 18 olle 727         PdfUtil pdfUtil = new PdfUtil(title, creator);
5201 07 Jan 19 olle 728         Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5201 07 Jan 19 olle 729         String projectName = activeProject.getName();
5201 07 Jan 19 olle 730         String pdfOutFilePath = fetchReferralFormBatchFilePath(2, projectName);
5158 30 Nov 18 olle 731         java.io.File pdfOutFile = new java.io.File(pdfOutFilePath);
5158 30 Nov 18 olle 732         OutputStream pdfOutStream = new FileOutputStream(pdfOutFile);
5158 30 Nov 18 olle 733         pdfUtil.open(pdfOutStream);
5158 30 Nov 18 olle 734
5158 30 Nov 18 olle 735         int nofFormSets = jsonItems.size();
5158 30 Nov 18 olle 736         for (int i = 0; i < nofFormSets; ++i)
5158 30 Nov 18 olle 737         {
5158 30 Nov 18 olle 738           progressPct = progressOffset + Math.round(progressBatchFile2Fraction*100*i/nofFormSets);
5158 30 Nov 18 olle 739           if (progressPct > 100)
5158 30 Nov 18 olle 740           {
5158 30 Nov 18 olle 741             progressPct = 100;
5158 30 Nov 18 olle 742           }
5158 30 Nov 18 olle 743           progress.display(progressPct, "Generating referral forms...");
5158 30 Nov 18 olle 744
5158 30 Nov 18 olle 745           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
5158 30 Nov 18 olle 746           String itemName = (String)jsonItem.get("name");
5158 30 Nov 18 olle 747           caseIdList.add(itemName);
5158 30 Nov 18 olle 748
5158 30 Nov 18 olle 749           // Get case item from referral ID in order to get patient item
5158 30 Nov 18 olle 750           Case theCase = Case.findByName(dc, itemName);
5158 30 Nov 18 olle 751           String referralId = (String) Annotationtype.REFERRAL_ID.getAnnotationValue(dc, manager, theCase.getSample());
5158 30 Nov 18 olle 752           String pad = null;
5158 30 Nov 18 olle 753           Patient patient = Patient.findByCase(dc, theCase);
5158 30 Nov 18 olle 754           patient.loadDefaultAnnotations(dc);
5158 30 Nov 18 olle 755           String personalNumber = (String) Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, manager, patient.getBioSource());
5158 30 Nov 18 olle 756           String familyName = (String) Annotationtype.FAMILY_NAME.getAnnotationValue(dc, manager, patient.getBioSource());
5158 30 Nov 18 olle 757           String allFirstNames = (String) Annotationtype.ALL_FIRST_NAMES.getAnnotationValue(dc, manager, patient.getBioSource());
5158 30 Nov 18 olle 758           // Add hyphen before last four digits in personal number
5158 30 Nov 18 olle 759           int pnrLen = personalNumber.length();
5158 30 Nov 18 olle 760           String pnrFirstPart = personalNumber.substring(0, pnrLen - 4);
5158 30 Nov 18 olle 761           String pnrLastPart = personalNumber.substring(pnrLen - 4);
5158 30 Nov 18 olle 762           personalNumber = pnrFirstPart + "-" + pnrLastPart;
5158 30 Nov 18 olle 763
5158 30 Nov 18 olle 764           // FFPE referral Form
5158 30 Nov 18 olle 765           pdfUtil = createFfpeForm(dc, pdfUtil, referralId, pad, personalNumber, familyName, allFirstNames, formTemplateFileList);
5158 30 Nov 18 olle 766         }
5158 30 Nov 18 olle 767         progressPct = 100;
5158 30 Nov 18 olle 768         progress.display(progressPct, "Generating referral forms...");
5158 30 Nov 18 olle 769         pdfUtil.close();
5158 30 Nov 18 olle 770
5158 30 Nov 18 olle 771         progress.display(100, "");
5158 30 Nov 18 olle 772         dc.commit();
5158 30 Nov 18 olle 773         //CounterService.getInstance().setForceCount();
5158 30 Nov 18 olle 774         String message = "FFPE block order form[s] created for case[s] " + caseIdList;
5158 30 Nov 18 olle 775         jsonMessages.add(message);
5158 30 Nov 18 olle 776       }
5158 30 Nov 18 olle 777       else if ("RegisterFfpeBlockOrderFormsAsOk".equals(cmd))
5158 30 Nov 18 olle 778       {
5158 30 Nov 18 olle 779         dc = sc.newDbControl();
5158 30 Nov 18 olle 780
5158 30 Nov 18 olle 781         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
5158 30 Nov 18 olle 782
5158 30 Nov 18 olle 783         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
5158 30 Nov 18 olle 784         req.setCharacterEncoding("UTF-8");
5158 30 Nov 18 olle 785
5158 30 Nov 18 olle 786         // Get referral form parameters
5158 30 Nov 18 olle 787         String jsonCaseListStr = Values.getString(req.getParameter("caselist"), "");
5158 30 Nov 18 olle 788         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" jsonCaseListStr = " + jsonCaseListStr);
5158 30 Nov 18 olle 789         // Get list of extract source item names
5158 30 Nov 18 olle 790         List<String> caseIdList = new ArrayList<String>();
5158 30 Nov 18 olle 791         JSONArray jsonItems = (JSONArray)new JSONParser().parse(jsonCaseListStr);
5158 30 Nov 18 olle 792         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" jsonItems = " + jsonItems);
5158 30 Nov 18 olle 793         for (int i = 0; i < jsonItems.size(); ++i)
5158 30 Nov 18 olle 794         {
5158 30 Nov 18 olle 795           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
5158 30 Nov 18 olle 796           String itemName = (String)jsonItem.get("name");
5158 30 Nov 18 olle 797           caseIdList.add(itemName);
5158 30 Nov 18 olle 798           // Mark case as having been used for FFPE block order form
5158 30 Nov 18 olle 799           Case theCase = Case.findByName(dc, itemName);
5158 30 Nov 18 olle 800           // Update the case annotations
5243 17 Jan 19 olle 801           Date orderDate = new Date();
5243 17 Jan 19 olle 802           Annotationtype.FFPE_BLOCK_ORDER_DATE.setAnnotationValue(dc, theCase.getSample(), orderDate);
5158 30 Nov 18 olle 803         }
5158 30 Nov 18 olle 804         dc.commit();
5158 30 Nov 18 olle 805         CounterService.getInstance().setForceCount();
5158 30 Nov 18 olle 806         String message = "FFPE block order form OK for cases " + caseIdList;
5158 30 Nov 18 olle 807         jsonMessages.add(message);
5158 30 Nov 18 olle 808       }
4914 13 Jul 18 olle 809       else if ("GenerateFfpeSectionOrderFormSequential".equals(cmd))
4909 12 Jul 18 olle 810       {
4909 12 Jul 18 olle 811         dc = sc.newDbControl();
4909 12 Jul 18 olle 812
4909 12 Jul 18 olle 813         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
4909 12 Jul 18 olle 814
4909 12 Jul 18 olle 815         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
4909 12 Jul 18 olle 816         req.setCharacterEncoding("UTF-8");
4909 12 Jul 18 olle 817
4909 12 Jul 18 olle 818         // Get referral form parameters
4909 12 Jul 18 olle 819         String referralVariant = Values.getString(req.getParameter("referralvariant"), REF_VARIANT_KIT_1_STANDARD);
4909 12 Jul 18 olle 820         String startFfpeBlockName = Values.getString(req.getParameter("startitemname"), "");
4909 12 Jul 18 olle 821         int nofFfpeBlocks = Values.getInteger(req.getParameter("nofffpeblocks"), 1);
4909 12 Jul 18 olle 822         //String pad = Values.getString(req.getParameter("pad"), "");
4909 12 Jul 18 olle 823         String itemIdRootDigits = Meludi.fetchRootDigits(startFfpeBlockName, Subtype.FFPE_BLOCK, sc.getActiveProjectId());
5733 19 Nov 19 olle 824         int numDigits = Meludi.fetchFfpeBlockItemNumDigits(sc.getActiveProjectId());
4909 12 Jul 18 olle 825
4909 12 Jul 18 olle 826         // Get FFPE block item from FFPE block name
4909 12 Jul 18 olle 827         String ffpeBlockName = startFfpeBlockName;
4909 12 Jul 18 olle 828         FfpeBlock ffpeBlock = FfpeBlock.getByName(dc, ffpeBlockName);
4909 12 Jul 18 olle 829         // Use stored annotation snapshots for performance reasons
4909 12 Jul 18 olle 830         SnapshotManager manager = new SnapshotManager();        
4909 12 Jul 18 olle 831         String pad = (String) Annotationtype.PAD.getAnnotationValue(dc, manager, ffpeBlock.getSample());
4909 12 Jul 18 olle 832
4909 12 Jul 18 olle 833         // Generate referral form
4909 12 Jul 18 olle 834         SimpleProgressReporter progress = new SimpleProgressReporter(null);
4909 12 Jul 18 olle 835         progressId = REFERRAL_GENERATION_PROGRESS_ID;
4909 12 Jul 18 olle 836         sc.setSessionSetting(progressId, progress);
4909 12 Jul 18 olle 837         progress.display(0, "Generating referral forms...");
4909 12 Jul 18 olle 838         // Set estimated fractions of process time
4909 12 Jul 18 olle 839         float progressBatchFile1Fraction = 0.43f;
4909 12 Jul 18 olle 840         float progressBatchFile2Fraction = 0.57f;
4909 12 Jul 18 olle 841         int progressOffset = 0;
4909 12 Jul 18 olle 842         int progressPct = 0;
4909 12 Jul 18 olle 843
4909 12 Jul 18 olle 844         // List of used form template filenames for log purposes 
4909 12 Jul 18 olle 845         List<String> formTemplateFileList = new ArrayList<String>();
4909 12 Jul 18 olle 846         // Get new PdfUtil instance
4909 12 Jul 18 olle 847         String title = "Title";
4909 12 Jul 18 olle 848         String creator = "Creator";
4909 12 Jul 18 olle 849
4909 12 Jul 18 olle 850         //
4909 12 Jul 18 olle 851         // Form batch file 2 (Referral forms intended to be printed on regular paper)
4909 12 Jul 18 olle 852         //
4909 12 Jul 18 olle 853         PdfUtil pdfUtil = new PdfUtil(title, creator);
5201 07 Jan 19 olle 854         Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5201 07 Jan 19 olle 855         String projectName = activeProject.getName();
5201 07 Jan 19 olle 856         String pdfOutFilePath = fetchReferralFormBatchFilePath(2, projectName);
4909 12 Jul 18 olle 857         java.io.File pdfOutFile = new java.io.File(pdfOutFilePath);
4909 12 Jul 18 olle 858         OutputStream pdfOutStream = new FileOutputStream(pdfOutFile);
4909 12 Jul 18 olle 859         pdfUtil.open(pdfOutStream);
4909 12 Jul 18 olle 860
4909 12 Jul 18 olle 861         String ffpeBlockPrefix = Meludi.fetchFfpeBlockItemPrefix(sc.getActiveProjectId());
4909 12 Jul 18 olle 862
4909 12 Jul 18 olle 863         String ffpeBlockRootDigits = Meludi.fetchRootDigits(startFfpeBlockName, Subtype.FFPE_BLOCK, sc.getActiveProjectId());
4909 12 Jul 18 olle 864         String ffpeBlockDigits = ffpeBlockRootDigits;
4909 12 Jul 18 olle 865
4909 12 Jul 18 olle 866         List<String> ffpeBlockIdList = new ArrayList<String>();
4909 12 Jul 18 olle 867         for (int i = 0; i < nofFfpeBlocks; i++)
4909 12 Jul 18 olle 868         {
4909 12 Jul 18 olle 869           if (i > 0)
4909 12 Jul 18 olle 870           {
5733 19 Nov 19 olle 871             ffpeBlockDigits = nextItemIdDigits(ffpeBlockDigits, numDigits);
4909 12 Jul 18 olle 872           }
4909 12 Jul 18 olle 873           String ffpeBlockId = ffpeBlockPrefix + ffpeBlockDigits;
4909 12 Jul 18 olle 874
4909 12 Jul 18 olle 875           // Add to FFPE block ID list
4909 12 Jul 18 olle 876           ffpeBlockIdList.add(ffpeBlockId);
4909 12 Jul 18 olle 877         }
4909 12 Jul 18 olle 878         progressOffset += progressPct;
4909 12 Jul 18 olle 879 /*
4909 12 Jul 18 olle 880         progressPct = progressOffset + Math.round(progressBatchFile2Fraction*100*i/nofFfpeBlocks);
4909 12 Jul 18 olle 881         if (progressPct > 100)
4909 12 Jul 18 olle 882         {
4909 12 Jul 18 olle 883           progressPct = 100;
4909 12 Jul 18 olle 884         }
4909 12 Jul 18 olle 885 */
4909 12 Jul 18 olle 886         progressPct = 100;
4909 12 Jul 18 olle 887         progress.display(progressPct, "Generating referral forms...");
4909 12 Jul 18 olle 888         // FFPE section order Form
4909 12 Jul 18 olle 889         pdfUtil = createFfpeSectionOrderForm(dc, pdfUtil, ffpeBlockIdList, formTemplateFileList);
4909 12 Jul 18 olle 890         pdfUtil.close();
4909 12 Jul 18 olle 891
4909 12 Jul 18 olle 892         progress.display(100, "");
4909 12 Jul 18 olle 893         //updateReferralFormLogFile(dc, referralVariant, itemIdRootDigits, nofItems, formTemplateFileList);
4909 12 Jul 18 olle 894         dc.commit();
4909 12 Jul 18 olle 895         //String endFfpeBlockName = ffpeBlockPrefix + ffpeBlockIdDigits;
4909 12 Jul 18 olle 896         String message = "FFPE section order form[s] created for FFPE block ID " + ffpeBlockIdList;
4909 12 Jul 18 olle 897         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + ": message = " + message);
4909 12 Jul 18 olle 898         jsonMessages.add(message);
4909 12 Jul 18 olle 899       }
4915 16 Jul 18 olle 900       else if ("GenerateFfpeSectionOrderForm".equals(cmd))
4915 16 Jul 18 olle 901       {
4915 16 Jul 18 olle 902         dc = sc.newDbControl();
4915 16 Jul 18 olle 903
4915 16 Jul 18 olle 904         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
4915 16 Jul 18 olle 905
4915 16 Jul 18 olle 906         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
4915 16 Jul 18 olle 907         req.setCharacterEncoding("UTF-8");
4915 16 Jul 18 olle 908
4915 16 Jul 18 olle 909         // Get referral form parameters
4915 16 Jul 18 olle 910         String referralVariant = Values.getString(req.getParameter("referralvariant"), REF_VARIANT_KIT_1_STANDARD);
4915 16 Jul 18 olle 911         String jsonFfpeBlockListStr = Values.getString(req.getParameter("ffpeblocklist"), "");
4915 16 Jul 18 olle 912         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" jsonFfpeBlockListStr = " + jsonFfpeBlockListStr);
4915 16 Jul 18 olle 913         // Get list of extract source item names
4915 16 Jul 18 olle 914         List<String> ffpeBlockIdList = new ArrayList<String>();
4915 16 Jul 18 olle 915         JSONArray jsonItems = (JSONArray)new JSONParser().parse(jsonFfpeBlockListStr);
4915 16 Jul 18 olle 916         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" jsonItems = " + jsonItems);
4915 16 Jul 18 olle 917         for (int i = 0; i < jsonItems.size(); ++i)
4915 16 Jul 18 olle 918         {
4915 16 Jul 18 olle 919           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
4915 16 Jul 18 olle 920           String itemName = (String)jsonItem.get("name");
4915 16 Jul 18 olle 921           ffpeBlockIdList.add(itemName);
5127 21 Nov 18 olle 922 /*
5052 25 Oct 18 olle 923           // Mark FFPE block as having been used in FFPE section order form
5052 25 Oct 18 olle 924           FfpeBlock ffpeBlock = FfpeBlock.getByName(dc, itemName);
5052 25 Oct 18 olle 925           // Update the FFPE block annotations
5061 30 Oct 18 olle 926           Annotationtype.USED_FOR_FFPE_SECTION_ORDER_FORM.setAnnotationValue(dc, ffpeBlock.getSample(), Boolean.TRUE);
5127 21 Nov 18 olle 927 */
4915 16 Jul 18 olle 928         }
4915 16 Jul 18 olle 929         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" ffpeBlockIdList = " + ffpeBlockIdList);
4915 16 Jul 18 olle 930
4915 16 Jul 18 olle 931         // Use stored annotation snapshots for performance reasons
4915 16 Jul 18 olle 932         SnapshotManager manager = new SnapshotManager();        
4915 16 Jul 18 olle 933 /*
4915 16 Jul 18 olle 934         String pad = (String) Annotationtype.PAD.getAnnotationValue(dc, manager, ffpeBlock.getSample());
4915 16 Jul 18 olle 935 */
4915 16 Jul 18 olle 936         // Generate referral form
4915 16 Jul 18 olle 937         SimpleProgressReporter progress = new SimpleProgressReporter(null);
4915 16 Jul 18 olle 938         progressId = REFERRAL_GENERATION_PROGRESS_ID;
4915 16 Jul 18 olle 939         sc.setSessionSetting(progressId, progress);
4915 16 Jul 18 olle 940         progress.display(0, "Generating referral forms...");
4915 16 Jul 18 olle 941         // Set estimated fractions of process time
4915 16 Jul 18 olle 942         float progressBatchFile1Fraction = 0.43f;
4915 16 Jul 18 olle 943         float progressBatchFile2Fraction = 0.57f;
4915 16 Jul 18 olle 944         int progressOffset = 0;
4915 16 Jul 18 olle 945         int progressPct = 0;
4915 16 Jul 18 olle 946
4915 16 Jul 18 olle 947         // List of used form template filenames for log purposes 
4915 16 Jul 18 olle 948         List<String> formTemplateFileList = new ArrayList<String>();
4915 16 Jul 18 olle 949         // Get new PdfUtil instance
4915 16 Jul 18 olle 950         String title = "Title";
4915 16 Jul 18 olle 951         String creator = "Creator";
4915 16 Jul 18 olle 952
4915 16 Jul 18 olle 953         //
4915 16 Jul 18 olle 954         // Form batch file 2 (Referral forms intended to be printed on regular paper)
4915 16 Jul 18 olle 955         //
4915 16 Jul 18 olle 956         PdfUtil pdfUtil = new PdfUtil(title, creator);
5201 07 Jan 19 olle 957         Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5201 07 Jan 19 olle 958         String projectName = activeProject.getName();
5201 07 Jan 19 olle 959         String pdfOutFilePath = fetchReferralFormBatchFilePath(2, projectName);
4915 16 Jul 18 olle 960         java.io.File pdfOutFile = new java.io.File(pdfOutFilePath);
4915 16 Jul 18 olle 961         OutputStream pdfOutStream = new FileOutputStream(pdfOutFile);
4915 16 Jul 18 olle 962         pdfUtil.open(pdfOutStream);
4915 16 Jul 18 olle 963
4915 16 Jul 18 olle 964 /*
4915 16 Jul 18 olle 965         progressPct = progressOffset + Math.round(progressBatchFile2Fraction*100*i/nofFfpeBlocks);
4915 16 Jul 18 olle 966         if (progressPct > 100)
4915 16 Jul 18 olle 967         {
4915 16 Jul 18 olle 968           progressPct = 100;
4915 16 Jul 18 olle 969         }
4915 16 Jul 18 olle 970 */
4915 16 Jul 18 olle 971         progressPct = 100;
4915 16 Jul 18 olle 972         progress.display(progressPct, "Generating referral forms...");
4915 16 Jul 18 olle 973         // FFPE section order Form
4915 16 Jul 18 olle 974         pdfUtil = createFfpeSectionOrderForm(dc, pdfUtil, ffpeBlockIdList, formTemplateFileList);
4915 16 Jul 18 olle 975         pdfUtil.close();
4915 16 Jul 18 olle 976
4915 16 Jul 18 olle 977         progress.display(100, "");
4915 16 Jul 18 olle 978         //updateReferralFormLogFile(dc, referralVariant, itemIdRootDigits, nofItems, formTemplateFileList);
4915 16 Jul 18 olle 979         dc.commit();
5061 30 Oct 18 olle 980         CounterService.getInstance().setForceCount();
4915 16 Jul 18 olle 981         //String endFfpeBlockName = ffpeBlockPrefix + ffpeBlockIdDigits;
4915 16 Jul 18 olle 982         String message = "FFPE section order form[s] created for FFPE block ID " + ffpeBlockIdList;
4915 16 Jul 18 olle 983         System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + ": message = " + message);
4915 16 Jul 18 olle 984         jsonMessages.add(message);
4915 16 Jul 18 olle 985       }
5127 21 Nov 18 olle 986       else if ("RegisterFfpeSectionOrderFormsAsOk".equals(cmd))
5127 21 Nov 18 olle 987       {
5127 21 Nov 18 olle 988         dc = sc.newDbControl();
5127 21 Nov 18 olle 989
5127 21 Nov 18 olle 990         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.PATIENT_CURATOR, MeludiRole.ADMINISTRATOR);
5127 21 Nov 18 olle 991
5127 21 Nov 18 olle 992         // Set character encoding to UTF-8 to ensure that Swedish characters will be preserved
5127 21 Nov 18 olle 993         req.setCharacterEncoding("UTF-8");
5127 21 Nov 18 olle 994
5127 21 Nov 18 olle 995         // Get referral form parameters
5127 21 Nov 18 olle 996         String jsonFfpeBlockListStr = Values.getString(req.getParameter("ffpeblocklist"), "");
5127 21 Nov 18 olle 997         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" jsonFfpeBlockListStr = " + jsonFfpeBlockListStr);
5127 21 Nov 18 olle 998         // Get list of extract source item names
5127 21 Nov 18 olle 999         List<String> ffpeBlockIdList = new ArrayList<String>();
5127 21 Nov 18 olle 1000         JSONArray jsonItems = (JSONArray)new JSONParser().parse(jsonFfpeBlockListStr);
5127 21 Nov 18 olle 1001         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" jsonItems = " + jsonItems);
5127 21 Nov 18 olle 1002         for (int i = 0; i < jsonItems.size(); ++i)
5127 21 Nov 18 olle 1003         {
5127 21 Nov 18 olle 1004           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
5127 21 Nov 18 olle 1005           String itemName = (String)jsonItem.get("name");
5127 21 Nov 18 olle 1006           ffpeBlockIdList.add(itemName);
5127 21 Nov 18 olle 1007           // Mark FFPE block as having been used in FFPE section order form
5127 21 Nov 18 olle 1008           FfpeBlock ffpeBlock = FfpeBlock.getByName(dc, itemName);
5127 21 Nov 18 olle 1009           // Update the FFPE block annotations
5127 21 Nov 18 olle 1010           Annotationtype.USED_FOR_FFPE_SECTION_ORDER_FORM.setAnnotationValue(dc, ffpeBlock.getSample(), Boolean.TRUE);
5127 21 Nov 18 olle 1011         }
5127 21 Nov 18 olle 1012         dc.commit();
5127 21 Nov 18 olle 1013         CounterService.getInstance().setForceCount();
5127 21 Nov 18 olle 1014         String message = "FFPE section order form[s] OK for FFPE block ID " + ffpeBlockIdList;
5127 21 Nov 18 olle 1015         jsonMessages.add(message);
5127 21 Nov 18 olle 1016       }
4706 19 Mar 18 olle 1017       else if ("DownloadFormBatchFile".equals(cmd))
4706 19 Mar 18 olle 1018       {
4706 19 Mar 18 olle 1019         // No JSON output
4706 19 Mar 18 olle 1020         json = null;
4706 19 Mar 18 olle 1021         
4706 19 Mar 18 olle 1022         int batchType = Values.getInteger(req.getParameter("batchType"), 1);
4908 12 Jul 18 olle 1023         String startItemName = req.getParameter("startitemname");
4915 16 Jul 18 olle 1024         String endItemName = req.getParameter("enditemname");
5204 08 Jan 19 olle 1025         String altFilename = req.getParameter("altfilename");
4915 16 Jul 18 olle 1026         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + ": batchType = " + batchType + " startItemName = " + startItemName + " endItemName = " + endItemName);
4909 12 Jul 18 olle 1027         // FFPE section forms contain several items on one referral form set
5733 19 Nov 19 olle 1028         int refNumDigits = Meludi.fetchReferralNumDigits(sc.getActiveProjectId());
5733 19 Nov 19 olle 1029         int numSerialDigits = refNumDigits - sitePrefixLength;
4908 12 Jul 18 olle 1030         int nofItems = Values.getInteger(req.getParameter("nofformsets"), 1);
4915 16 Jul 18 olle 1031         //System.out.println("ReferralGeneratorServlet::doGet(): cmd = " + cmd + ": batchType = " + batchType + " startItemName = " + startItemName + " nofItems = " + nofItems);
4909 12 Jul 18 olle 1032         if (startItemName.startsWith(Meludi.fetchFfpeBlockItemPrefix(sc.getActiveProjectId())))
4909 12 Jul 18 olle 1033         {
5733 19 Nov 19 olle 1034           numSerialDigits = Meludi.fetchFfpeBlockItemNumDigits(sc.getActiveProjectId());
4909 12 Jul 18 olle 1035           nofItems = Values.getInteger(req.getParameter("nofFfpeBlocks"), 1);
4909 12 Jul 18 olle 1036         }
4908 12 Jul 18 olle 1037         String itemPrefix = Meludi.fetchReferralPrefix(sc.getActiveProjectId());
4908 12 Jul 18 olle 1038         String startItemIdDigits = Meludi.fetchReferralDigits(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 1039         Subtype mainSubtype = Meludi.fetchMainSubtypeFromItemName(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 1040         if (mainSubtype != null)
4908 12 Jul 18 olle 1041         {
4908 12 Jul 18 olle 1042           itemPrefix = Meludi.fetchItemPrefix(mainSubtype, sc.getActiveProjectId());
4908 12 Jul 18 olle 1043           startItemIdDigits = Meludi.fetchRootDigits(startItemName, sc.getActiveProjectId());
4908 12 Jul 18 olle 1044         }
4915 16 Jul 18 olle 1045         if (endItemName == null || endItemName.equals("undefined"))
4915 16 Jul 18 olle 1046         {
5733 19 Nov 19 olle 1047           String endItemIdDigits = nextItemIdDigits(startItemIdDigits, numSerialDigits, (nofItems-1));
4915 16 Jul 18 olle 1048           endItemName = itemPrefix + endItemIdDigits;
4915 16 Jul 18 olle 1049         }
5201 07 Jan 19 olle 1050         dc = sc.newDbControl();
5201 07 Jan 19 olle 1051         Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5201 07 Jan 19 olle 1052         String projectName = activeProject.getName();
5201 07 Jan 19 olle 1053         String pdfOutFilePath = fetchReferralFormBatchFilePath(batchType, projectName);
5204 08 Jan 19 olle 1054         String filename = fetchReferralFormBatchFileName(batchType, startItemName, endItemName, projectName, altFilename);
4706 19 Mar 18 olle 1055         // Add time stamp to download filename
4706 19 Mar 18 olle 1056         DateToStringConverter d2 = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd_HHmm"));
4706 19 Mar 18 olle 1057         String timeStampStr = d2.convert(new Date());
4706 19 Mar 18 olle 1058         String fileExt = "";
4706 19 Mar 18 olle 1059         int fileExtIndex = filename.lastIndexOf(".");
4706 19 Mar 18 olle 1060         if (fileExtIndex >= 0)
4706 19 Mar 18 olle 1061         {
4706 19 Mar 18 olle 1062           // Get file extension
4706 19 Mar 18 olle 1063           fileExt = filename.substring(fileExtIndex);
4706 19 Mar 18 olle 1064           // Remove file extension
4706 19 Mar 18 olle 1065           filename = filename.substring(0, fileExtIndex);
4706 19 Mar 18 olle 1066         }
4706 19 Mar 18 olle 1067         filename += "_" + timeStampStr + fileExt;
4706 19 Mar 18 olle 1068         
4706 19 Mar 18 olle 1069         resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
4706 19 Mar 18 olle 1070         //resp.setContentType("text/plain");
4706 19 Mar 18 olle 1071         resp.setContentType("application/pdf");
4706 19 Mar 18 olle 1072         resp.setCharacterEncoding("UTF-8");
4706 19 Mar 18 olle 1073
4706 19 Mar 18 olle 1074         // Copy contents from form set PDF file to download buffer
4706 19 Mar 18 olle 1075         java.io.File file = null;
4706 19 Mar 18 olle 1076         try
4706 19 Mar 18 olle 1077         {
4706 19 Mar 18 olle 1078           file = new java.io.File(pdfOutFilePath);
4706 19 Mar 18 olle 1079         }
4706 19 Mar 18 olle 1080         catch (NullPointerException ex)
4706 19 Mar 18 olle 1081         {
4706 19 Mar 18 olle 1082           System.out.println(new Date() + " ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" NullPointerException when trying to get file for  = \"" + pdfOutFilePath + "\" e: " + ex);
4706 19 Mar 18 olle 1083           return;
4706 19 Mar 18 olle 1084         }
4706 19 Mar 18 olle 1085         try
4706 19 Mar 18 olle 1086         {
4706 19 Mar 18 olle 1087           InputStream in = new FileInputStream(file);
4706 19 Mar 18 olle 1088           // For binary data, get OutputStream from HttpServletResponse instead of PrintWriter
4706 19 Mar 18 olle 1089           OutputStream out = resp.getOutputStream();
4706 19 Mar 18 olle 1090
4706 19 Mar 18 olle 1091           // Buffer for portion of data from connection
4706 19 Mar 18 olle 1092           byte[] buffer = new byte[4096];
4706 19 Mar 18 olle 1093           int n = -1;
4706 19 Mar 18 olle 1094           while ((n = in.read(buffer)) != -1)
4706 19 Mar 18 olle 1095           {
4706 19 Mar 18 olle 1096             // Write n bytes from the byte array buffer (with offset 0) to the output stream
4706 19 Mar 18 olle 1097             out.write(buffer, 0, n);
4706 19 Mar 18 olle 1098           }
4706 19 Mar 18 olle 1099           out.flush();
4706 19 Mar 18 olle 1100           out.close();
4706 19 Mar 18 olle 1101           in.close();
4706 19 Mar 18 olle 1102         }
4706 19 Mar 18 olle 1103         catch (Exception e)
4706 19 Mar 18 olle 1104         {
4706 19 Mar 18 olle 1105           System.out.println(new Date() + " ReferralGeneratorServlet::doGet(): cmd = \"" + cmd + "\" Exception when trying to copy file \"" + pdfOutFilePath + "\" to \"" + filename + "\". e: " + e);
4706 19 Mar 18 olle 1106         }
4706 19 Mar 18 olle 1107       }
4706 19 Mar 18 olle 1108       json.put("messages", jsonMessages);
4706 19 Mar 18 olle 1109     }
4706 19 Mar 18 olle 1110     catch (Throwable t)
4706 19 Mar 18 olle 1111     {
4706 19 Mar 18 olle 1112       t.printStackTrace();
4706 19 Mar 18 olle 1113       if (json != null)
4706 19 Mar 18 olle 1114       {
4706 19 Mar 18 olle 1115         json.clear();
4706 19 Mar 18 olle 1116         json.put("status", "error");
4706 19 Mar 18 olle 1117         json.put("message", t.getMessage());
4706 19 Mar 18 olle 1118         json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
4706 19 Mar 18 olle 1119       }
4706 19 Mar 18 olle 1120     }
4706 19 Mar 18 olle 1121     finally
4706 19 Mar 18 olle 1122     {
4706 19 Mar 18 olle 1123       if (dc != null) dc.close();
4706 19 Mar 18 olle 1124       if (json != null)
4706 19 Mar 18 olle 1125       {
4706 19 Mar 18 olle 1126         json.writeJSONString(resp.getWriter());
4706 19 Mar 18 olle 1127       }
4706 19 Mar 18 olle 1128     }
4706 19 Mar 18 olle 1129     
4706 19 Mar 18 olle 1130   }
4706 19 Mar 18 olle 1131
4724 29 Mar 18 olle 1132   /**
4724 29 Mar 18 olle 1133    *  Returns a JSONArray with all sites 
4724 29 Mar 18 olle 1134    * 
4724 29 Mar 18 olle 1135    *  @return String A JSONObject with site prefix as key and site JSON data as value.
4724 29 Mar 18 olle 1136    */
4724 29 Mar 18 olle 1137   @SuppressWarnings("unchecked")
4724 29 Mar 18 olle 1138   public static JSONArray fetchJSONSites(Comparator<Site> comparator, int activeProjectId)
4724 29 Mar 18 olle 1139   {
4724 29 Mar 18 olle 1140     Collection<Site> sites = Site.getAllSites();
4724 29 Mar 18 olle 1141     if (comparator != null)
4724 29 Mar 18 olle 1142     {
4724 29 Mar 18 olle 1143       List<Site> list = new ArrayList<Site>(sites);
4724 29 Mar 18 olle 1144       Collections.sort(list, comparator);
4724 29 Mar 18 olle 1145       sites = list;
4724 29 Mar 18 olle 1146     }
4724 29 Mar 18 olle 1147
4724 29 Mar 18 olle 1148     JSONArray jsonSites = new JSONArray();
4724 29 Mar 18 olle 1149     HashMap<String,String> siteKeySitePrefixHashMap = Meludi.fetchSiteKeySitePrefixHashMap(activeProjectId);
4724 29 Mar 18 olle 1150     for (Site s : sites)
4724 29 Mar 18 olle 1151     {
4724 29 Mar 18 olle 1152       // Exchange MeLuDI site key for site prefix in returned JSON data
4724 29 Mar 18 olle 1153       // Note that class Site currently returns the site key as JSON key 'prefix' 
4724 29 Mar 18 olle 1154       String siteKey = s.getPrefix();
4724 29 Mar 18 olle 1155       String sitePrefix = siteKeySitePrefixHashMap.get(siteKey);
4724 29 Mar 18 olle 1156       JSONObject jsonSite = s.asJSONObject();
4724 29 Mar 18 olle 1157       jsonSite.put("prefix", sitePrefix);
4724 29 Mar 18 olle 1158       jsonSites.add(jsonSite);
4724 29 Mar 18 olle 1159     }
4724 29 Mar 18 olle 1160     return jsonSites;
4724 29 Mar 18 olle 1161   }
4724 29 Mar 18 olle 1162
4724 29 Mar 18 olle 1163
5700 05 Nov 19 olle 1164   /**
5700 05 Nov 19 olle 1165    * Returns number of biopsy forms.
5700 05 Nov 19 olle 1166    * 
5700 05 Nov 19 olle 1167    * @return int Number of biopsy forms.
5700 05 Nov 19 olle 1168    */
5700 05 Nov 19 olle 1169   private int fetchNumBiopsyForms(DbControl dc)
5700 05 Nov 19 olle 1170   {
5700 05 Nov 19 olle 1171     int numBiopsyForms = NUM_BIOPSY_FORMS_DEFAULT;
5700 05 Nov 19 olle 1172
5700 05 Nov 19 olle 1173     SessionControl sc = dc.getSessionControl();
5700 05 Nov 19 olle 1174     Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5700 05 Nov 19 olle 1175     String projectName = activeProject.getName();
5700 05 Nov 19 olle 1176     if (projectName.equals("NordicTrip"))
5700 05 Nov 19 olle 1177     {
5700 05 Nov 19 olle 1178       numBiopsyForms = 1;
5700 05 Nov 19 olle 1179     }
5700 05 Nov 19 olle 1180     return numBiopsyForms;
5700 05 Nov 19 olle 1181   }
5700 05 Nov 19 olle 1182
5700 05 Nov 19 olle 1183   /**
5700 05 Nov 19 olle 1184    * Returns number of follow-up forms.
5700 05 Nov 19 olle 1185    * 
5700 05 Nov 19 olle 1186    * @return int Number of follow-up forms.
5700 05 Nov 19 olle 1187    */
5700 05 Nov 19 olle 1188   private int fetchNumFollowUpForms(DbControl dc)
5700 05 Nov 19 olle 1189   {
5700 05 Nov 19 olle 1190     int numFollowUpForms = NUM_FOLLOW_UP_FORMS_DEFAULT;
5700 05 Nov 19 olle 1191     
5700 05 Nov 19 olle 1192     SessionControl sc = dc.getSessionControl();
5700 05 Nov 19 olle 1193     Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5700 05 Nov 19 olle 1194     String projectName = activeProject.getName();
5700 05 Nov 19 olle 1195     if (projectName.equals("NordicTrip"))
5700 05 Nov 19 olle 1196     {
5700 05 Nov 19 olle 1197       numFollowUpForms = 1;
5700 05 Nov 19 olle 1198     }
5700 05 Nov 19 olle 1199     return numFollowUpForms;
5700 05 Nov 19 olle 1200   }
5700 05 Nov 19 olle 1201
5391 02 May 19 olle 1202   private void initializeRefFormTemplNameStartHashMap(SessionControl sc)
4706 19 Mar 18 olle 1203   {
4724 29 Mar 18 olle 1204     System.out.println("ReferralGeneratorServlet::initializeRefFormTemplNameStartHashMap(): Start");
5391 02 May 19 olle 1205     DbControl dc = sc.newDbControl();
5391 02 May 19 olle 1206     Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5391 02 May 19 olle 1207     String projectName = activeProject.getName();
5391 02 May 19 olle 1208     String prefix = projectName + "_";
4706 19 Mar 18 olle 1209     refFormTemplNameStartHM = new HashMap<>();
5700 05 Nov 19 olle 1210     if (projectName.equals("NordicTrip"))
5700 05 Nov 19 olle 1211     {
5700 05 Nov 19 olle 1212       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_PATIENT_INFO, prefix + TEMPLATE_FILENAME_START_PATIENT_INFO);
5700 05 Nov 19 olle 1213       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY, prefix + "BL_Tissue_referral");
5700 05 Nov 19 olle 1214       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD);
5700 05 Nov 19 olle 1215       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD_LUNDCOPY, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD_LUNDCOPY);
5700 05 Nov 19 olle 1216       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BCBLOOD, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM_BCBLOOD);
5700 05 Nov 19 olle 1217       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY, prefix + TEMPLATE_FILENAME_START_MIDDLE_NEEDLE_BIOPSY);
5700 05 Nov 19 olle 1218       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM);
5700 05 Nov 19 olle 1219       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_PATHOLOGY, prefix + TEMPLATE_FILENAME_START_CLIN_PATHOLOGY);
5700 05 Nov 19 olle 1220       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CONSENT_REPORT, prefix + "consent_form");
5700 05 Nov 19 olle 1221       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_PAD_REFERRAL, prefix + TEMPLATE_FILENAME_START_PAD_REFERRAL);
5700 05 Nov 19 olle 1222       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_WHOLE_BLOOD, prefix + "referral_blood_BL");
5700 05 Nov 19 olle 1223       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FOLLOW_UP, prefix + "referral_blood_FU");
5700 05 Nov 19 olle 1224       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FOLLOW_UP_TISSUE, prefix + "FU");
5700 05 Nov 19 olle 1225       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FFPE_REFERRAL, prefix + TEMPLATE_FILENAME_START_FFPE_REFERRAL);
5700 05 Nov 19 olle 1226       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FFPE_SECTION_ORDER_FORM, prefix + TEMPLATE_FILENAME_START_FFPE_SECTION_ORDER_FORM);
5702 06 Nov 19 olle 1227       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_NORDIC_TRIP_STICKER_FORM, prefix + "NT");
5700 05 Nov 19 olle 1228     }
6783 25 Jul 22 olle 1229     else if (projectName.equals("LUCAS"))
6783 25 Jul 22 olle 1230     {
6783 25 Jul 22 olle 1231       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_LUCAS_STICKER_FORM, prefix + "Bronkoskopi");
6783 25 Jul 22 olle 1232     }
5700 05 Nov 19 olle 1233     else
5700 05 Nov 19 olle 1234     {
5700 05 Nov 19 olle 1235       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_PATIENT_INFO, prefix + TEMPLATE_FILENAME_START_PATIENT_INFO);
5700 05 Nov 19 olle 1236       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM_BIOPSY);
5700 05 Nov 19 olle 1237       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD);
5700 05 Nov 19 olle 1238       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD_LUNDCOPY, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM_BLOOD_LUNDCOPY);
5700 05 Nov 19 olle 1239       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM_BCBLOOD, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM_BCBLOOD);
5700 05 Nov 19 olle 1240       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY, prefix + TEMPLATE_FILENAME_START_MIDDLE_NEEDLE_BIOPSY);
5700 05 Nov 19 olle 1241       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_CHEM, prefix + TEMPLATE_FILENAME_START_CLIN_CHEM);
5700 05 Nov 19 olle 1242       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CLIN_PATHOLOGY, prefix + TEMPLATE_FILENAME_START_CLIN_PATHOLOGY);
5700 05 Nov 19 olle 1243       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_CONSENT_REPORT, prefix + TEMPLATE_FILENAME_START_CONSENT_REPORT);
5700 05 Nov 19 olle 1244       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_PAD_REFERRAL, prefix + TEMPLATE_FILENAME_START_PAD_REFERRAL);
5700 05 Nov 19 olle 1245       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_WHOLE_BLOOD, prefix + TEMPLATE_FILENAME_START_WHOLE_BLOOD);
5700 05 Nov 19 olle 1246       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FOLLOW_UP, prefix + TEMPLATE_FILENAME_START_FOLLOW_UP);
5700 05 Nov 19 olle 1247       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FOLLOW_UP_TISSUE, prefix + TEMPLATE_FILENAME_START_FOLLOW_UP_TISSUE);
5700 05 Nov 19 olle 1248       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FFPE_REFERRAL, prefix + TEMPLATE_FILENAME_START_FFPE_REFERRAL);
5700 05 Nov 19 olle 1249       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_FFPE_SECTION_ORDER_FORM, prefix + TEMPLATE_FILENAME_START_FFPE_SECTION_ORDER_FORM);
5702 06 Nov 19 olle 1250       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_NORDIC_TRIP_STICKER_FORM, prefix + TEMPLATE_FILENAME_START_NORDIC_TRIP_STICKER_FORM);
6783 25 Jul 22 olle 1251       refFormTemplNameStartHM.put(REF_FORM_TEMPLATE_LUCAS_STICKER_FORM, prefix + TEMPLATE_FILENAME_START_LUCAS_STICKER_FORM);
5700 05 Nov 19 olle 1252     }
4724 29 Mar 18 olle 1253     System.out.println("ReferralGeneratorServlet::initializeRefFormTemplNameStartHashMap(): End");
5695 04 Nov 19 olle 1254     dc.close();
4706 19 Mar 18 olle 1255   }
4706 19 Mar 18 olle 1256
5732 19 Nov 19 olle 1257   private void initializeRefFormTemplTypeIdentSuffixHashMap(SessionControl sc)
5732 19 Nov 19 olle 1258   {
5732 19 Nov 19 olle 1259     System.out.println("ReferralGeneratorServlet::initializeRefFormTemplTypeIdentSuffixHashMap(): Start");
5732 19 Nov 19 olle 1260     DbControl dc = sc.newDbControl();
5732 19 Nov 19 olle 1261     Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5732 19 Nov 19 olle 1262     String projectName = activeProject.getName();
5732 19 Nov 19 olle 1263     refFormTemplTypeIdentSuffixHM = new HashMap<>();
5732 19 Nov 19 olle 1264     if (projectName.equals("NordicTrip"))
5732 19 Nov 19 olle 1265     {
5732 19 Nov 19 olle 1266       refFormTemplTypeIdentSuffixHM.put("B0B", "a");
5732 19 Nov 19 olle 1267       refFormTemplTypeIdentSuffixHM.put("B0C", "b");
5732 19 Nov 19 olle 1268       refFormTemplTypeIdentSuffixHM.put("T0A", "c");
5732 19 Nov 19 olle 1269       refFormTemplTypeIdentSuffixHM.put("T0B", "d");
5732 19 Nov 19 olle 1270       refFormTemplTypeIdentSuffixHM.put("B1A", "e");
5732 19 Nov 19 olle 1271       refFormTemplTypeIdentSuffixHM.put("B1B", "f");
5732 19 Nov 19 olle 1272       refFormTemplTypeIdentSuffixHM.put("T1A", "g");
5732 19 Nov 19 olle 1273       refFormTemplTypeIdentSuffixHM.put("T1B", "h");
5732 19 Nov 19 olle 1274       refFormTemplTypeIdentSuffixHM.put("B2", "i");
5732 19 Nov 19 olle 1275       refFormTemplTypeIdentSuffixHM.put("T3A", "j");
5732 19 Nov 19 olle 1276       refFormTemplTypeIdentSuffixHM.put("T3B", "k");
5938 16 May 20 olle 1277       refFormTemplTypeIdentSuffixHM.put("T3C", "m");
5938 16 May 20 olle 1278       refFormTemplTypeIdentSuffixHM.put("B4", "n");
5938 16 May 20 olle 1279       refFormTemplTypeIdentSuffixHM.put("B5", "p");
5938 16 May 20 olle 1280       refFormTemplTypeIdentSuffixHM.put("B6", "q");
5938 16 May 20 olle 1281       refFormTemplTypeIdentSuffixHM.put("T6", "r");
5732 19 Nov 19 olle 1282     }
5732 19 Nov 19 olle 1283     System.out.println("ReferralGeneratorServlet::initializeRefFormTemplTypeIdentSuffixHashMap(): End");
5732 19 Nov 19 olle 1284     dc.close();
5732 19 Nov 19 olle 1285   }
5732 19 Nov 19 olle 1286
4706 19 Mar 18 olle 1287   /**
4706 19 Mar 18 olle 1288    * Returns path to be used for referral start info file.
4706 19 Mar 18 olle 1289    * 
4706 19 Mar 18 olle 1290    * @return String Path to be used for report file.
4706 19 Mar 18 olle 1291    */
4706 19 Mar 18 olle 1292   private String fetchReferralStartInfoFilePath()
4706 19 Mar 18 olle 1293   {
4706 19 Mar 18 olle 1294     String refStartInfoFilePath = null;
4706 19 Mar 18 olle 1295     String refStartInfoFileName = REF_START_INFO_FILENAME;
4706 19 Mar 18 olle 1296     if (refStartInfoFileName != null)
4706 19 Mar 18 olle 1297     {
4706 19 Mar 18 olle 1298       // Place referral start info file in BASE user files directory
4706 19 Mar 18 olle 1299       java.io.File userFilesDir = Application.getUserFilesDirectory();
4706 19 Mar 18 olle 1300       String fileDir = userFilesDir.getAbsolutePath();
4706 19 Mar 18 olle 1301       refStartInfoFilePath = fileDir + "/" + refStartInfoFileName;
4706 19 Mar 18 olle 1302     }
4706 19 Mar 18 olle 1303     return refStartInfoFilePath;
4706 19 Mar 18 olle 1304   }
4706 19 Mar 18 olle 1305
5695 04 Nov 19 olle 1306   /**
5695 04 Nov 19 olle 1307    * Returns BASE file item for referral start info file.
5695 04 Nov 19 olle 1308    * 
5695 04 Nov 19 olle 1309    * @param dc DbControl The DbControl to use.
5695 04 Nov 19 olle 1310    * @return File BASE file item for referral start info file.
5695 04 Nov 19 olle 1311    */
5695 04 Nov 19 olle 1312   private File fetchReferralStartInfoFile(DbControl dc)
5695 04 Nov 19 olle 1313   {
5695 04 Nov 19 olle 1314     // Place referral start info file in form templates directory in BASE file system
5695 04 Nov 19 olle 1315     //Directory refFormStartInfoFileDir = Directory.getNew(dc, new Path(Meludi.REFERRAL_FORM_TEMPLATES_DIR, Path.Type.DIRECTORY));
5695 04 Nov 19 olle 1316     SessionControl sc = dc.getSessionControl();
5695 04 Nov 19 olle 1317     String refFormTemplTopDirPath = Meludi.fetchReferralFormTemplatesDir(sc.getActiveProjectId());
5695 04 Nov 19 olle 1318     Directory refFormStartInfoFileDir = Directory.getNew(dc, new Path(refFormTemplTopDirPath, Path.Type.DIRECTORY));
5695 04 Nov 19 olle 1319     File refStartInfoFile = File.getFile(dc, refFormStartInfoFileDir, REF_START_INFO_FILENAME, true);
5695 04 Nov 19 olle 1320     System.out.println("ReferralGeneratorServlet::fetchReferralStartInfoFile(): refFormTemplTopDirPath = " + refFormTemplTopDirPath + " refStartInfoFile = " + refStartInfoFile);
5695 04 Nov 19 olle 1321     if (!refStartInfoFile.isInDatabase())
5695 04 Nov 19 olle 1322     {
5695 04 Nov 19 olle 1323       dc.saveItem(refStartInfoFile);
5695 04 Nov 19 olle 1324     }
5695 04 Nov 19 olle 1325     return refStartInfoFile;
5695 04 Nov 19 olle 1326   }
5695 04 Nov 19 olle 1327
4706 19 Mar 18 olle 1328   @SuppressWarnings("unchecked")
4911 12 Jul 18 olle 1329   private JSONObject fetchReferralCaseInfo(DbControl dc, String referralId)
4911 12 Jul 18 olle 1330     throws IOException
4911 12 Jul 18 olle 1331   {
4911 12 Jul 18 olle 1332     JSONObject jsonCase = null;
4911 12 Jul 18 olle 1333     // Get case item for referral ID
4911 12 Jul 18 olle 1334     Case theCase = Case.findByReferralId(dc, referralId);
4911 12 Jul 18 olle 1335     if (theCase != null)
4911 12 Jul 18 olle 1336     {
4911 12 Jul 18 olle 1337       // Get PAD pre-entry from case annotation
4911 12 Jul 18 olle 1338       theCase.loadAnnotations(dc, "referralId", Annotationtype.REFERRAL_ID, null);
4911 12 Jul 18 olle 1339       jsonCase = theCase.asJSONObject();
4911 12 Jul 18 olle 1340     }
4911 12 Jul 18 olle 1341     System.out.println("ReferralGeneratorServlet::fetchReferralCaseInfo(): referralId = " + referralId + " jsonCase = " + jsonCase);
4911 12 Jul 18 olle 1342     return jsonCase;
4911 12 Jul 18 olle 1343   }
4911 12 Jul 18 olle 1344
4911 12 Jul 18 olle 1345   @SuppressWarnings("unchecked")
5695 04 Nov 19 olle 1346   private JSONObject fetchReferralStartInfo(DbControl dc)
4706 19 Mar 18 olle 1347     throws IOException
4706 19 Mar 18 olle 1348   {
5695 04 Nov 19 olle 1349 /*
4706 19 Mar 18 olle 1350     String refStartInfoFilePath = fetchReferralStartInfoFilePath();
4724 29 Mar 18 olle 1351     System.out.println("ReferralGeneratorServlet::fetchReferralStartInfo(): refStartInfoFilePath = " + refStartInfoFilePath);
5695 04 Nov 19 olle 1352 */
4706 19 Mar 18 olle 1353     // Copy contents from referral start info file to JSON object
4706 19 Mar 18 olle 1354     JSONObject jsonRefStartInfo = new JSONObject();
5695 04 Nov 19 olle 1355 /*
4706 19 Mar 18 olle 1356     java.io.File file = null;
4706 19 Mar 18 olle 1357     file = new java.io.File(refStartInfoFilePath);
4706 19 Mar 18 olle 1358     if (!file.exists())
4706 19 Mar 18 olle 1359     {
4706 19 Mar 18 olle 1360       createReferralStartInfoFile();
4706 19 Mar 18 olle 1361     }
4706 19 Mar 18 olle 1362     java.io.FileReader fileReader = null;
4706 19 Mar 18 olle 1363     try
4706 19 Mar 18 olle 1364     {
4706 19 Mar 18 olle 1365       fileReader = new java.io.FileReader(file);
4706 19 Mar 18 olle 1366     }
4706 19 Mar 18 olle 1367     catch (IOException ex)
4706 19 Mar 18 olle 1368     {
4706 19 Mar 18 olle 1369       System.out.println(new Date() + " ReferralGeneratorServlet::fetchReferralStartInfo(): IOException when trying to get file reader for  = \"" + refStartInfoFilePath + "\" e: " + ex);
4706 19 Mar 18 olle 1370     }
4706 19 Mar 18 olle 1371     java.io.BufferedReader bufferedReader = new java.io.BufferedReader(fileReader);
4706 19 Mar 18 olle 1372     StringBuffer stringBuffer = new StringBuffer();
4706 19 Mar 18 olle 1373     String line;
4706 19 Mar 18 olle 1374     // Get data from single line, or last, if more than one line 
4706 19 Mar 18 olle 1375     while ((line = bufferedReader.readLine()) != null)
4706 19 Mar 18 olle 1376     {
4724 29 Mar 18 olle 1377       System.out.println("ReferralGeneratorServlet::fetchReferralStartInfo(): line = \"" + line + "\"");
4706 19 Mar 18 olle 1378       String[] columns = line.split("\t", -1);
4706 19 Mar 18 olle 1379       if (columns != null && columns.length > 0)
4706 19 Mar 18 olle 1380       {
4908 12 Jul 18 olle 1381         // Get next item id to use without site prefix
4908 12 Jul 18 olle 1382         String startItemIdDigitsPure = columns[0];
4706 19 Mar 18 olle 1383         String lastUpdated = "";
4706 19 Mar 18 olle 1384         if (columns.length > 1)
4706 19 Mar 18 olle 1385         {
4908 12 Jul 18 olle 1386           // Get date the next item id info was last updated
4706 19 Mar 18 olle 1387           lastUpdated = columns[1];
4706 19 Mar 18 olle 1388         }
4908 12 Jul 18 olle 1389         System.out.println("ReferralGeneratorServlet::fetchReferralStartInfo(): startItemIdDigitsPure = " + startItemIdDigitsPure + " lastUpdated = " + lastUpdated);
4908 12 Jul 18 olle 1390         jsonRefStartInfo.put("startItemIdDigitsPure", startItemIdDigitsPure);
4706 19 Mar 18 olle 1391         jsonRefStartInfo.put("lastUpdated", lastUpdated);
4706 19 Mar 18 olle 1392       }
4706 19 Mar 18 olle 1393     }
4706 19 Mar 18 olle 1394     fileReader.close();
5695 04 Nov 19 olle 1395 */
5695 04 Nov 19 olle 1396     // Get referral form start info file
5695 04 Nov 19 olle 1397     File refStartInfoFile = fetchReferralStartInfoFile(dc);
5695 04 Nov 19 olle 1398     try
5695 04 Nov 19 olle 1399     {
5695 04 Nov 19 olle 1400       // Read current file contents
5695 04 Nov 19 olle 1401       InputStream in = refStartInfoFile.getDownloadStream(0);
5695 04 Nov 19 olle 1402       InputStreamReader ipsr = new InputStreamReader(in, "UTF-8");
5695 04 Nov 19 olle 1403       BufferedReader br = new BufferedReader(ipsr);
5695 04 Nov 19 olle 1404       String line;
5695 04 Nov 19 olle 1405       // Get data from single line, or last, if more than one line 
5695 04 Nov 19 olle 1406       while ((line = br.readLine()) != null)
5695 04 Nov 19 olle 1407       {
5695 04 Nov 19 olle 1408         System.out.println("ReferralGeneratorServlet::fetchReferralStartInfo(): line = \"" + line + "\"");
5695 04 Nov 19 olle 1409         String[] columns = line.split("\t", -1);
5695 04 Nov 19 olle 1410         if (columns != null && columns.length > 0)
5695 04 Nov 19 olle 1411         {
5695 04 Nov 19 olle 1412           // Get next item id to use without site prefix
5695 04 Nov 19 olle 1413           String startItemIdDigitsPure = columns[0];
5695 04 Nov 19 olle 1414           String lastUpdated = "";
5695 04 Nov 19 olle 1415           if (columns.length > 1)
5695 04 Nov 19 olle 1416           {
5695 04 Nov 19 olle 1417             // Get date the next item id info was last updated
5695 04 Nov 19 olle 1418             lastUpdated = columns[1];
5695 04 Nov 19 olle 1419           }
5695 04 Nov 19 olle 1420           System.out.println("ReferralGeneratorServlet::fetchReferralStartInfo(): startItemIdDigitsPure = " + startItemIdDigitsPure + " lastUpdated = " + lastUpdated);
5695 04 Nov 19 olle 1421           jsonRefStartInfo.put("startItemIdDigitsPure", startItemIdDigitsPure);
5695 04 Nov 19 olle 1422           jsonRefStartInfo.put("lastUpdated", lastUpdated);
5695 04 Nov 19 olle 1423         }
5695 04 Nov 19 olle 1424       }
5695 04 Nov 19 olle 1425       br.close();
5695 04 Nov 19 olle 1426       ipsr.close();
5695 04 Nov 19 olle 1427       in.close();
5695 04 Nov 19 olle 1428       //
5695 04 Nov 19 olle 1429     }
5695 04 Nov 19 olle 1430     catch(Exception e)
5695 04 Nov 19 olle 1431     {
5695 04 Nov 19 olle 1432       System.out.println(new Date() + " ReferralGeneratorServlet::fetchReferralStartInfo(): Exception e: " + e);
5695 04 Nov 19 olle 1433     }
4706 19 Mar 18 olle 1434     return jsonRefStartInfo;
4706 19 Mar 18 olle 1435   }
4706 19 Mar 18 olle 1436
4706 19 Mar 18 olle 1437   private void createReferralStartInfoFile()
4706 19 Mar 18 olle 1438     throws IOException
4706 19 Mar 18 olle 1439   {
4908 12 Jul 18 olle 1440     // Set next pure item ID to use to 16000
4908 12 Jul 18 olle 1441     String nextItemIdDigitsPure = "00001";
4706 19 Mar 18 olle 1442
4706 19 Mar 18 olle 1443     // Get date string
4706 19 Mar 18 olle 1444     DateToStringConverter d1 = new DateToStringConverter(new SimpleDateFormat("yyyy-MM-dd"));
4706 19 Mar 18 olle 1445     String lastUpdated = d1.convert(new Date());
4706 19 Mar 18 olle 1446
4706 19 Mar 18 olle 1447     String refStartInfoFilePath = fetchReferralStartInfoFilePath();
4706 19 Mar 18 olle 1448     java.io.File file = new java.io.File(refStartInfoFilePath);
4706 19 Mar 18 olle 1449     FileWriter fileWriter = null;
4706 19 Mar 18 olle 1450     //
4706 19 Mar 18 olle 1451     try
4706 19 Mar 18 olle 1452     {
4706 19 Mar 18 olle 1453       fileWriter = new FileWriter(file);
4706 19 Mar 18 olle 1454       String separator = "\t";
4908 12 Jul 18 olle 1455       fileWriter.write(nextItemIdDigitsPure + separator + lastUpdated + "\n");
4706 19 Mar 18 olle 1456       //
4706 19 Mar 18 olle 1457       fileWriter.flush();
4706 19 Mar 18 olle 1458       fileWriter.close();
4706 19 Mar 18 olle 1459     }
4706 19 Mar 18 olle 1460     catch(IOException ex)
4706 19 Mar 18 olle 1461     {
4706 19 Mar 18 olle 1462       System.out.println(new Date() + " ReferralGeneratorServlet::createReferralStartInfoFile(): Could not create filewriter for refStartInfoFilePath = " + refStartInfoFilePath);
4706 19 Mar 18 olle 1463     }
4706 19 Mar 18 olle 1464   }
4706 19 Mar 18 olle 1465
4706 19 Mar 18 olle 1466   /**
4706 19 Mar 18 olle 1467    * Returns path to be used for referral form batch file.
4706 19 Mar 18 olle 1468    * 
4706 19 Mar 18 olle 1469    * @param batchType int The type number of the form batch.
5204 08 Jan 19 olle 1470    * @param projectName String Name of active project.
4706 19 Mar 18 olle 1471    * @return String Path to be used for referral form batch file.
4706 19 Mar 18 olle 1472    */
5201 07 Jan 19 olle 1473   private String fetchReferralFormBatchFilePath(int batchType, String projectName)
4706 19 Mar 18 olle 1474   {
4706 19 Mar 18 olle 1475     String refFormBatchFilePath = null;
5201 07 Jan 19 olle 1476     String refFormBatchFileName = fetchReferralFormBatchFileName(batchType, projectName);
4706 19 Mar 18 olle 1477     if (refFormBatchFileName != null)
4706 19 Mar 18 olle 1478     {
4706 19 Mar 18 olle 1479       // Place referral form batch file in BASE user files directory
4706 19 Mar 18 olle 1480       java.io.File userFilesDir = Application.getUserFilesDirectory();
4706 19 Mar 18 olle 1481       String fileDir = userFilesDir.getAbsolutePath();
4706 19 Mar 18 olle 1482       refFormBatchFilePath = fileDir + "/" + refFormBatchFileName;
4706 19 Mar 18 olle 1483     }
4706 19 Mar 18 olle 1484     return refFormBatchFilePath;
4706 19 Mar 18 olle 1485   }
4706 19 Mar 18 olle 1486
4706 19 Mar 18 olle 1487   /**
4706 19 Mar 18 olle 1488    * Returns name to be used for referral form batch file.
4706 19 Mar 18 olle 1489    * 
4706 19 Mar 18 olle 1490    * @param batchType int The type number of the form batch.
5204 08 Jan 19 olle 1491    * @param projectName String Name of active project.
4706 19 Mar 18 olle 1492    * @return String name to be used for report file.
4706 19 Mar 18 olle 1493    */
5201 07 Jan 19 olle 1494   private String fetchReferralFormBatchFileName(int batchType, String projectName)
4706 19 Mar 18 olle 1495   {
5204 08 Jan 19 olle 1496     String refFormBatchFileName = fetchReferralFormBatchFileName(batchType, null, null, projectName, null);
4706 19 Mar 18 olle 1497     return refFormBatchFileName;
4706 19 Mar 18 olle 1498   }
4706 19 Mar 18 olle 1499
4706 19 Mar 18 olle 1500   /**
4706 19 Mar 18 olle 1501    * Returns name to be used for referral form batch file.
4706 19 Mar 18 olle 1502    * 
4706 19 Mar 18 olle 1503    * @param batchType int The type number of the form batch.
4908 12 Jul 18 olle 1504    * @param startItemIdDigits String The first item ID number in the form batch.
4908 12 Jul 18 olle 1505    * @param endItemIdDigits String The last item ID number in the form batch.
5204 08 Jan 19 olle 1506    * @param projectName String Name of active project.
5204 08 Jan 19 olle 1507    * @param altFilename String A string to use instead of standard batch base filename and batch type.
4706 19 Mar 18 olle 1508    * @return String name to be used for report file.
4706 19 Mar 18 olle 1509    */
5204 08 Jan 19 olle 1510   private String fetchReferralFormBatchFileName(int batchType, String startItemIdDigits, String endItemIdDigits, String projectName, String altFilename)
4706 19 Mar 18 olle 1511   {
4908 12 Jul 18 olle 1512     String itemIdDigitsSpec = "";
4908 12 Jul 18 olle 1513     if (startItemIdDigits != null && !startItemIdDigits.equals(""))
4706 19 Mar 18 olle 1514     {
4908 12 Jul 18 olle 1515       itemIdDigitsSpec = "_" + startItemIdDigits;
4908 12 Jul 18 olle 1516       if (endItemIdDigits != null && !endItemIdDigits.equals(""))
4706 19 Mar 18 olle 1517       {
4908 12 Jul 18 olle 1518         itemIdDigitsSpec += "-" + endItemIdDigits;
4706 19 Mar 18 olle 1519       }
4706 19 Mar 18 olle 1520     }
5201 07 Jan 19 olle 1521     //String refFormBatchFileName = REF_FORM_BATCH_BASE_FILENAME + "_" + batchType + itemIdDigitsSpec + ".pdf";
5201 07 Jan 19 olle 1522     String refFormBatchFileName = projectName + "_" + REF_FORM_BATCH_BASE_FILENAME + "_" + batchType + itemIdDigitsSpec + ".pdf";
5204 08 Jan 19 olle 1523     if (altFilename != null && !altFilename.equals(""))
5204 08 Jan 19 olle 1524     {
5204 08 Jan 19 olle 1525       refFormBatchFileName = projectName + "_" + altFilename + itemIdDigitsSpec + ".pdf";
5204 08 Jan 19 olle 1526     }
4706 19 Mar 18 olle 1527     return refFormBatchFileName;
4706 19 Mar 18 olle 1528   }
4706 19 Mar 18 olle 1529
4706 19 Mar 18 olle 1530   /**
4706 19 Mar 18 olle 1531    * Returns start of referral form template filename.
4706 19 Mar 18 olle 1532    * 
4706 19 Mar 18 olle 1533    * @param formTemplateName String The String key of the form template file.
4706 19 Mar 18 olle 1534    * @return String Start of the referral form template filename.
4706 19 Mar 18 olle 1535    */
4706 19 Mar 18 olle 1536   private String fetchReferralFormTemplateFilenameStart(String formTemplateName)
4706 19 Mar 18 olle 1537   {
4706 19 Mar 18 olle 1538     String refFormTemplateFilename = refFormTemplNameStartHM.get(formTemplateName);
4706 19 Mar 18 olle 1539     return refFormTemplateFilename;
4706 19 Mar 18 olle 1540   }
4706 19 Mar 18 olle 1541
4706 19 Mar 18 olle 1542   /**
4706 19 Mar 18 olle 1543    * Returns JSONArray with list of multiple BASE file items for given referral type.
4706 19 Mar 18 olle 1544    * 
4706 19 Mar 18 olle 1545    * @param dc DbControl The DbControl to use.
4724 29 Mar 18 olle 1546    * @param sitePrefix String The site ID prefix to create referral form for.
4706 19 Mar 18 olle 1547    * @param formTemplateName String Name of the form template.
4706 19 Mar 18 olle 1548    * @param jsonMultipleTemplFiles JSONArray JSONArray with list of multiple template files to add to.
4706 19 Mar 18 olle 1549    * @return JSONArray JSONArray with list of multiple BASE file items for given referral type.
4706 19 Mar 18 olle 1550    */
4706 19 Mar 18 olle 1551   @SuppressWarnings("unchecked")
4724 29 Mar 18 olle 1552   private JSONArray fetchMultipleTemplateFiles(DbControl dc, String sitePrefix, String formTemplateName, JSONArray jsonMultipleTemplFiles)
4706 19 Mar 18 olle 1553   {
4908 12 Jul 18 olle 1554     // Get dummy item ID
4908 12 Jul 18 olle 1555     String itemIdDigits = sitePrefix + "00000";
4706 19 Mar 18 olle 1556     // Get PDF templates for template type
4908 12 Jul 18 olle 1557     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, formTemplateName);
4706 19 Mar 18 olle 1558     if (pdfFiles.size() > 1)
4706 19 Mar 18 olle 1559     {
4706 19 Mar 18 olle 1560       for (File pdfFile: pdfFiles)
4706 19 Mar 18 olle 1561       {
4706 19 Mar 18 olle 1562         JSONObject jsonMultipleTemplFile = new JSONObject();
4706 19 Mar 18 olle 1563         jsonMultipleTemplFile.put("type", formTemplateName);
4706 19 Mar 18 olle 1564         jsonMultipleTemplFile.put("file", pdfFile.getName());
4706 19 Mar 18 olle 1565         jsonMultipleTemplFiles.add(jsonMultipleTemplFile);
4706 19 Mar 18 olle 1566       }
4706 19 Mar 18 olle 1567     }
4706 19 Mar 18 olle 1568     return jsonMultipleTemplFiles;
4706 19 Mar 18 olle 1569   }
4706 19 Mar 18 olle 1570
4706 19 Mar 18 olle 1571   /**
4706 19 Mar 18 olle 1572    * Returns list of BASE file items for referral form template file.
4706 19 Mar 18 olle 1573    * 
4706 19 Mar 18 olle 1574    * @param dc DbControl The DbControl to use.
4908 12 Jul 18 olle 1575    * @param itemIdDigits String The item ID to create referral form for.
4706 19 Mar 18 olle 1576    * @param formTemplateName String Name of the form template.
4706 19 Mar 18 olle 1577    * @return List<File> List of BASE file items for referral form template file.
4706 19 Mar 18 olle 1578    */
4908 12 Jul 18 olle 1579   private List<File> fetchReferralFormTemplateFileList(DbControl dc, String itemIdDigits, String formTemplateName)
4706 19 Mar 18 olle 1580   {    
4706 19 Mar 18 olle 1581     // Get PDF template
4706 19 Mar 18 olle 1582     //String refFormTemplTopDirPath = Meludi.REFERRAL_FORM_TEMPLATES_DIR;
4706 19 Mar 18 olle 1583     SessionControl sc = dc.getSessionControl();
4706 19 Mar 18 olle 1584     String refFormTemplTopDirPath = Meludi.fetchReferralFormTemplatesDir(sc.getActiveProjectId());
4908 12 Jul 18 olle 1585     System.out.println("ReferralGeneratorServlet::fetchReferralFormTemplateFileList(): refFormTemplTopDirPath = " + refFormTemplTopDirPath + " itemIdDigits = " + itemIdDigits + " formTemplateName = " + formTemplateName);
4808 15 May 18 olle 1586     Boolean userSiteSpecificReferralTemplates = Meludi.fetchUsesSiteSpecificReferralTemplates(sc.getActiveProjectId());
4808 15 May 18 olle 1587     String refFormTemplDirPath = refFormTemplTopDirPath;
4908 12 Jul 18 olle 1588     String upperCaseAnglifiedSiteName = "";
4808 15 May 18 olle 1589     if (userSiteSpecificReferralTemplates)
4808 15 May 18 olle 1590     {
4908 12 Jul 18 olle 1591       String sitePrefix = itemIdDigits.substring(0,sitePrefixLength);
4808 15 May 18 olle 1592       refFormTemplDirPath = refFormTemplTopDirPath + "/" + sitePrefix;
4908 12 Jul 18 olle 1593       upperCaseAnglifiedSiteName = fetchUpperCaseAnglifiedSiteName(sitePrefix);
4908 12 Jul 18 olle 1594       System.out.println("ReferralGeneratorServlet::fetchReferralFormTemplateFileList(): upperCaseAnglifiedSiteName = " + upperCaseAnglifiedSiteName);
4808 15 May 18 olle 1595     }
4724 29 Mar 18 olle 1596     System.out.println("ReferralGeneratorServlet::fetchReferralFormTemplateFileList(): refFormTemplDirPath = " + refFormTemplDirPath);
4706 19 Mar 18 olle 1597     String refFormTemplFilenameStart = fetchReferralFormTemplateFilenameStart(formTemplateName);
4724 29 Mar 18 olle 1598     System.out.println("ReferralGeneratorServlet::fetchReferralFormTemplateFileList(): refFormTemplFilenameStart = " + refFormTemplFilenameStart);
4706 19 Mar 18 olle 1599     Directory refFormTemplDir = Directory.getNew(dc, new Path(refFormTemplDirPath, Path.Type.DIRECTORY));
4706 19 Mar 18 olle 1600     ItemQuery<File> fileQuery = refFormTemplDir.getFiles();
4706 19 Mar 18 olle 1601     fileQuery.order(Orders.asc(Hql.property("name")));
4706 19 Mar 18 olle 1602     List<File> fileList = fileQuery.list(dc);
4706 19 Mar 18 olle 1603     List<File> pdfFiles = new ArrayList<File>();
4706 19 Mar 18 olle 1604     for (int i = 0; i < fileList.size(); i++)
4706 19 Mar 18 olle 1605     {
4706 19 Mar 18 olle 1606       File file = (File)fileList.get(i);
4706 19 Mar 18 olle 1607       if (file.getName().startsWith(refFormTemplFilenameStart))
4706 19 Mar 18 olle 1608       {
4706 19 Mar 18 olle 1609         pdfFiles.add(file);
4706 19 Mar 18 olle 1610       }
4706 19 Mar 18 olle 1611       else if (file.getName().startsWith(upperCaseAnglifiedSiteName + "-" + refFormTemplFilenameStart))
4706 19 Mar 18 olle 1612       {
4706 19 Mar 18 olle 1613         pdfFiles.add(file);
4706 19 Mar 18 olle 1614       }
4706 19 Mar 18 olle 1615       else if (file.getName().startsWith(upperCaseAnglifiedSiteName + "_" + refFormTemplFilenameStart))
4706 19 Mar 18 olle 1616       {
4706 19 Mar 18 olle 1617         pdfFiles.add(file);
4706 19 Mar 18 olle 1618       }
4706 19 Mar 18 olle 1619     }
4724 29 Mar 18 olle 1620     System.out.println("ReferralGeneratorServlet::fetchReferralFormTemplateFileList(): pdfFiles = " + pdfFiles);
4706 19 Mar 18 olle 1621     return pdfFiles;
4706 19 Mar 18 olle 1622   }
4706 19 Mar 18 olle 1623
4706 19 Mar 18 olle 1624   private String fetchUpperCaseAnglifiedSiteName(String prefix)
4706 19 Mar 18 olle 1625   {
4706 19 Mar 18 olle 1626     JSONArray jsonSites = ReportTableUtil.getJSONSites(Site.SORT_BY_NAME);
4706 19 Mar 18 olle 1627     JSONObject jsonSite = null;
4706 19 Mar 18 olle 1628     for (int i = 0; i < jsonSites.size(); i++)
4706 19 Mar 18 olle 1629     {
4706 19 Mar 18 olle 1630       JSONObject theJsonSite = (JSONObject)jsonSites.get(i);
4706 19 Mar 18 olle 1631       String sitePrefix = (String)theJsonSite.get("prefix");
4706 19 Mar 18 olle 1632       if (sitePrefix.equals(prefix))
4706 19 Mar 18 olle 1633       {
4706 19 Mar 18 olle 1634         jsonSite = theJsonSite;
4706 19 Mar 18 olle 1635         break;
4706 19 Mar 18 olle 1636       }
4706 19 Mar 18 olle 1637     }
4706 19 Mar 18 olle 1638     String upperCaseSiteName = null;
4706 19 Mar 18 olle 1639     if (jsonSite != null)
4706 19 Mar 18 olle 1640     {
4706 19 Mar 18 olle 1641       String siteName = (String)jsonSite.get("name");
4706 19 Mar 18 olle 1642       String anglifiedSiteName = anglifyString(siteName);
4706 19 Mar 18 olle 1643       upperCaseSiteName = anglifiedSiteName.toUpperCase();
4706 19 Mar 18 olle 1644     }
4706 19 Mar 18 olle 1645     return upperCaseSiteName;
4706 19 Mar 18 olle 1646   }
4706 19 Mar 18 olle 1647   
4706 19 Mar 18 olle 1648   /**
4706 19 Mar 18 olle 1649    * Replaces national Swedish characters "Ã…,Ä,Ö,Ã¥,ä,ö" with
4706 19 Mar 18 olle 1650    * the root characters "A,A,O,a,a,o", respectively.
4706 19 Mar 18 olle 1651    * 
4706 19 Mar 18 olle 1652    * @param text String The Swedish text that should be modified to only use English characters.
4706 19 Mar 18 olle 1653    * @return Sting The input Swedish text modified to only use English characters.
4706 19 Mar 18 olle 1654    */
4706 19 Mar 18 olle 1655   private String anglifyString(String text)
4706 19 Mar 18 olle 1656   {
4706 19 Mar 18 olle 1657     if (text == null)
4706 19 Mar 18 olle 1658     {
4706 19 Mar 18 olle 1659       return null;
4706 19 Mar 18 olle 1660     }
4706 19 Mar 18 olle 1661     String anglifiedString = "";
4706 19 Mar 18 olle 1662     for (int i = 0; i < text.length(); i++)
4706 19 Mar 18 olle 1663     {
4706 19 Mar 18 olle 1664       String chr = text.substring(i,i+1);
4706 19 Mar 18 olle 1665       if (chr.equals("Ã…"))
4706 19 Mar 18 olle 1666       {
4706 19 Mar 18 olle 1667         chr = "A";
4706 19 Mar 18 olle 1668       }
4706 19 Mar 18 olle 1669       else if (chr.equals("Ä"))
4706 19 Mar 18 olle 1670       {
4706 19 Mar 18 olle 1671         chr = "A";
4706 19 Mar 18 olle 1672       }
4706 19 Mar 18 olle 1673       else if (chr.equals("Ö"))
4706 19 Mar 18 olle 1674       {
4706 19 Mar 18 olle 1675         chr = "O";
4706 19 Mar 18 olle 1676       }
4706 19 Mar 18 olle 1677       if (chr.equals("Ã¥"))
4706 19 Mar 18 olle 1678       {
4706 19 Mar 18 olle 1679         chr = "a";
4706 19 Mar 18 olle 1680       }
4706 19 Mar 18 olle 1681       else if (chr.equals("ä"))
4706 19 Mar 18 olle 1682       {
4706 19 Mar 18 olle 1683         chr = "a";
4706 19 Mar 18 olle 1684       }
4706 19 Mar 18 olle 1685       else if (chr.equals("ö"))
4706 19 Mar 18 olle 1686       {
4706 19 Mar 18 olle 1687         chr = "o";
4706 19 Mar 18 olle 1688       }
4706 19 Mar 18 olle 1689       anglifiedString += chr;
4706 19 Mar 18 olle 1690     }
4706 19 Mar 18 olle 1691     return anglifiedString;
4706 19 Mar 18 olle 1692   }
4706 19 Mar 18 olle 1693
4706 19 Mar 18 olle 1694   /**
4706 19 Mar 18 olle 1695    * Update BASE file item for referral form log file.
4706 19 Mar 18 olle 1696    * 
4706 19 Mar 18 olle 1697    * @param dc DbControl The DbControl to use.
4706 19 Mar 18 olle 1698    * @param referralVariant String The referral variant to be created.
4908 12 Jul 18 olle 1699    * @param startItemIdDigits String The first item id in the created form set batch.
4706 19 Mar 18 olle 1700    * @param nofFormSets int The number of referral form sets created.
4706 19 Mar 18 olle 1701    * @param formTemplateFileList List<String> List of used form template files.
4706 19 Mar 18 olle 1702    */
4908 12 Jul 18 olle 1703   private void updateReferralFormLogFile(DbControl dc, String referralVariant, String startItemIdDigits, int nofFormSets, List<String> formTemplateFileList)
4706 19 Mar 18 olle 1704   {    
4908 12 Jul 18 olle 1705     System.out.println("ReferralGeneratorServlet::updateReferralFormLogFile(): referralVariant = " + referralVariant + " startItemIdDigits = " + startItemIdDigits + " nofFormSets = " + nofFormSets + " formTemplateFileList = " + formTemplateFileList);
4706 19 Mar 18 olle 1706     DateToStringConverter d3 = new DateToStringConverter(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
4908 12 Jul 18 olle 1707     String itemPrefix = Meludi.fetchReferralPrefix(dc.getSessionControl().getActiveProjectId());
5733 19 Nov 19 olle 1708     int refNumDigits = Meludi.fetchReferralNumDigits(dc.getSessionControl().getActiveProjectId());
5733 19 Nov 19 olle 1709     int numSerialDigits = refNumDigits - sitePrefixLength;
4706 19 Mar 18 olle 1710     String timeStampStr = d3.convert(new Date());
5733 19 Nov 19 olle 1711     String endItemIdDigits = nextItemIdDigits(startItemIdDigits, numSerialDigits, (nofFormSets-1));
4908 12 Jul 18 olle 1712     String startItemId = itemPrefix + startItemIdDigits;
4908 12 Jul 18 olle 1713     String endItemId = itemPrefix + endItemIdDigits;
4706 19 Mar 18 olle 1714     // Get comma-separated list of used form template filenames
4706 19 Mar 18 olle 1715     String formTemplateFiles = "";
4706 19 Mar 18 olle 1716     for (int i = 0; i < formTemplateFileList.size(); i++)
4706 19 Mar 18 olle 1717     {
4706 19 Mar 18 olle 1718       String formTemplateFilename = (String)formTemplateFileList.get(i);
4706 19 Mar 18 olle 1719       if (i > 0)
4706 19 Mar 18 olle 1720       {
4706 19 Mar 18 olle 1721         formTemplateFiles += ",";
4706 19 Mar 18 olle 1722       }
4706 19 Mar 18 olle 1723       formTemplateFiles += formTemplateFilename;
4706 19 Mar 18 olle 1724     }
4706 19 Mar 18 olle 1725     // Get referral form log file
4706 19 Mar 18 olle 1726     File logFile = fetchReferralFormLogFile(dc);
4706 19 Mar 18 olle 1727     try
4706 19 Mar 18 olle 1728     {
4706 19 Mar 18 olle 1729       // Read and store current file contents
4706 19 Mar 18 olle 1730       List<String> lines = new ArrayList<String>();
4706 19 Mar 18 olle 1731       InputStream in = logFile.getDownloadStream(0);
4706 19 Mar 18 olle 1732       InputStreamReader ipsr = new InputStreamReader(in, "UTF-8");
4706 19 Mar 18 olle 1733       BufferedReader br = new BufferedReader(ipsr);
4706 19 Mar 18 olle 1734       // Read data lines
4706 19 Mar 18 olle 1735       String rawLine;
4706 19 Mar 18 olle 1736       while ((rawLine = br.readLine()) != null)
4706 19 Mar 18 olle 1737       {
4706 19 Mar 18 olle 1738         lines.add(rawLine);
4706 19 Mar 18 olle 1739       }
4706 19 Mar 18 olle 1740       br.close();
4706 19 Mar 18 olle 1741       ipsr.close();
4706 19 Mar 18 olle 1742       in.close();
4706 19 Mar 18 olle 1743       //
4706 19 Mar 18 olle 1744       OutputStream out = logFile.getUploadStream(false);
4706 19 Mar 18 olle 1745       OutputStreamWriter writer = new OutputStreamWriter(out);
4706 19 Mar 18 olle 1746       // Write back old contents
4706 19 Mar 18 olle 1747       for (int i = 0; i < lines.size(); i++)
4706 19 Mar 18 olle 1748       {
4706 19 Mar 18 olle 1749         String line = (String)lines.get(i);
4706 19 Mar 18 olle 1750         writer.write(line + "\n");
4706 19 Mar 18 olle 1751       }
4706 19 Mar 18 olle 1752       // Add new content
4706 19 Mar 18 olle 1753       String separator = "\t";
4908 12 Jul 18 olle 1754       //String itemIdDigitsRange = startItemIdDigits + "-" + endItemIdDigits;
4908 12 Jul 18 olle 1755       String itemIdDigitsRange = startItemId + "-" + endItemId;
4908 12 Jul 18 olle 1756       String line = timeStampStr + separator + referralVariant + separator + itemIdDigitsRange + separator + formTemplateFiles;
4724 29 Mar 18 olle 1757       System.out.println("ReferralGeneratorServlet::updateReferralFormLogFile(): line = " + line);
4706 19 Mar 18 olle 1758       writer.write(line + "\n");
4706 19 Mar 18 olle 1759       //
4706 19 Mar 18 olle 1760       writer.close();
4706 19 Mar 18 olle 1761       out.close();
4706 19 Mar 18 olle 1762     }
4706 19 Mar 18 olle 1763     catch(Exception e)
4706 19 Mar 18 olle 1764     {
4706 19 Mar 18 olle 1765       System.out.println(new Date() + " updateReferralFormLogFile(): Exception e: " + e);
4706 19 Mar 18 olle 1766     }
4706 19 Mar 18 olle 1767   }
4706 19 Mar 18 olle 1768
4706 19 Mar 18 olle 1769   /**
4706 19 Mar 18 olle 1770    * Returns BASE file item for referral form log file.
4706 19 Mar 18 olle 1771    * 
4706 19 Mar 18 olle 1772    * @param dc DbControl The DbControl to use.
4706 19 Mar 18 olle 1773    * @return File BASE file item for referral form log file.
4706 19 Mar 18 olle 1774    */
4706 19 Mar 18 olle 1775   private File fetchReferralFormLogFile(DbControl dc)
4706 19 Mar 18 olle 1776   {
4706 19 Mar 18 olle 1777     // Place referral form log file in form templates directory in BASE file system
4706 19 Mar 18 olle 1778     //Directory refFormLogFileDir = Directory.getNew(dc, new Path(Meludi.REFERRAL_FORM_TEMPLATES_DIR, Path.Type.DIRECTORY));
4706 19 Mar 18 olle 1779     SessionControl sc = dc.getSessionControl();
4706 19 Mar 18 olle 1780     String refFormTemplTopDirPath = Meludi.fetchReferralFormTemplatesDir(sc.getActiveProjectId());
4706 19 Mar 18 olle 1781     Directory refFormLogFileDir = Directory.getNew(dc, new Path(refFormTemplTopDirPath, Path.Type.DIRECTORY));
4706 19 Mar 18 olle 1782     File logFile = File.getFile(dc, refFormLogFileDir, REF_FORM_LOG_FILENAME, true);
4706 19 Mar 18 olle 1783     if (!logFile.isInDatabase())
4706 19 Mar 18 olle 1784     {
4706 19 Mar 18 olle 1785       dc.saveItem(logFile);
4706 19 Mar 18 olle 1786     }
4706 19 Mar 18 olle 1787     return logFile;
4706 19 Mar 18 olle 1788   }
4706 19 Mar 18 olle 1789
5733 19 Nov 19 olle 1790   /**
5733 19 Nov 19 olle 1791    * Increments an input item id with or without site prefix one unit.
5733 19 Nov 19 olle 1792    * 
5733 19 Nov 19 olle 1793    * @param itemIdDigits String The item id with or without site prefix.
5733 19 Nov 19 olle 1794    * @param numSerialDigits int The number of digits in serial number part of the item id, excluding optional site prefix.
5733 19 Nov 19 olle 1795    * @return String The item id with given increment.
5733 19 Nov 19 olle 1796    */
5733 19 Nov 19 olle 1797   private String nextItemIdDigits(String itemIdDigits, int numSerialDigits)
4706 19 Mar 18 olle 1798   {
5733 19 Nov 19 olle 1799     String nextItemIdDigits = nextItemIdDigits(itemIdDigits, numSerialDigits, 1);
4908 12 Jul 18 olle 1800     return nextItemIdDigits;
4706 19 Mar 18 olle 1801   }
4706 19 Mar 18 olle 1802
4706 19 Mar 18 olle 1803   /**
4908 12 Jul 18 olle 1804    * Increments an input item id with or without site prefix
4706 19 Mar 18 olle 1805    * a given increment.
4706 19 Mar 18 olle 1806    * 
4908 12 Jul 18 olle 1807    * @param itemIdDigits String The item id with or without site prefix.
5733 19 Nov 19 olle 1808    * @param numSerialDigits int The number of digits in serial number part of the item id, excluding optional site prefix.
4908 12 Jul 18 olle 1809    * @param incr Integer Increment of item id.
4908 12 Jul 18 olle 1810    * @return String The item id with given increment.
4706 19 Mar 18 olle 1811    */
5733 19 Nov 19 olle 1812   private String nextItemIdDigits(String itemIdDigits, int numSerialDigits, Integer incr)
4706 19 Mar 18 olle 1813   {
5733 19 Nov 19 olle 1814 System.out.println(new Date() + " nextItemIdDigits(): Start: itemIdDigits = \"" + itemIdDigits + "\" numSerialDigits = " + numSerialDigits + " incr = " + incr + ".");
4908 12 Jul 18 olle 1815     String nextItemIdDigits = null;
4724 29 Mar 18 olle 1816     String sitePrefix = "";
4908 12 Jul 18 olle 1817     String itemIdDigitsPure = "";
4908 12 Jul 18 olle 1818     if (itemIdDigits != null)
4706 19 Mar 18 olle 1819     {
5733 19 Nov 19 olle 1820       if (itemIdDigits.length() > numSerialDigits)
4706 19 Mar 18 olle 1821       {        
4908 12 Jul 18 olle 1822         sitePrefix = itemIdDigits.substring(0,sitePrefixLength);
4908 12 Jul 18 olle 1823         itemIdDigitsPure = itemIdDigits.substring(sitePrefixLength);
4706 19 Mar 18 olle 1824       }
5733 19 Nov 19 olle 1825       else
4706 19 Mar 18 olle 1826       {        
4908 12 Jul 18 olle 1827         itemIdDigitsPure = itemIdDigits;
4706 19 Mar 18 olle 1828       }
5733 19 Nov 19 olle 1829 System.out.println(new Date() + " nextItemIdDigits(): sitePrefix = \"" + sitePrefix + "\" itemIdDigitsPure = \"" + itemIdDigitsPure + "\".");
4908 12 Jul 18 olle 1830       String itemIdDigitsSuffix = "";
5733 19 Nov 19 olle 1831       if (itemIdDigitsPure.length() > numSerialDigits)
4706 19 Mar 18 olle 1832       {
4908 12 Jul 18 olle 1833         // Extract item ID suffix
4908 12 Jul 18 olle 1834         itemIdDigitsSuffix = itemIdDigitsPure.substring(itemIdDigitsPure.length() - 1, itemIdDigitsPure.length());
4908 12 Jul 18 olle 1835         itemIdDigitsPure = itemIdDigitsPure.substring(0, itemIdDigitsPure.length() - 1);
4706 19 Mar 18 olle 1836       }
5733 19 Nov 19 olle 1837 System.out.println(new Date() + " nextItemIdDigits(): itemIdDigitsSuffix = \"" + itemIdDigitsSuffix + "\" itemIdDigitsPure = \"" + itemIdDigitsPure + "\".");
4706 19 Mar 18 olle 1838       // Remove initial zeroes
4908 12 Jul 18 olle 1839       while (itemIdDigitsPure.indexOf("0") == 0)
4706 19 Mar 18 olle 1840       {
4706 19 Mar 18 olle 1841         // Remove initial zero
4908 12 Jul 18 olle 1842         itemIdDigitsPure = itemIdDigitsPure.substring(1);        
4706 19 Mar 18 olle 1843       }
4908 12 Jul 18 olle 1844       String nextItemIdDigitsPure = "";
4908 12 Jul 18 olle 1845       if (itemIdDigitsPure.length() > 0)
4706 19 Mar 18 olle 1846       {
4908 12 Jul 18 olle 1847         int itemIdDigitsPureInt = Integer.parseInt(itemIdDigitsPure);
4908 12 Jul 18 olle 1848         int nextItemIdDigitsPureInt = itemIdDigitsPureInt + incr;
4908 12 Jul 18 olle 1849         nextItemIdDigitsPure = "" + nextItemIdDigitsPureInt;
4706 19 Mar 18 olle 1850       }
4706 19 Mar 18 olle 1851       // Add initial zeroes
5733 19 Nov 19 olle 1852       while (nextItemIdDigitsPure.length() < numSerialDigits)
4706 19 Mar 18 olle 1853       {
4706 19 Mar 18 olle 1854         // Add initial zero
4908 12 Jul 18 olle 1855         nextItemIdDigitsPure = "0" + nextItemIdDigitsPure;        
4706 19 Mar 18 olle 1856       }
4908 12 Jul 18 olle 1857       // Add item ID prefix and optional suffix
4908 12 Jul 18 olle 1858       nextItemIdDigits = sitePrefix + nextItemIdDigitsPure + itemIdDigitsSuffix;
5733 19 Nov 19 olle 1859 System.out.println(new Date() + " nextItemIdDigits(): siteProefix = \"" + sitePrefix + "\" nextItemIdDigitsPure = \"" + nextItemIdDigitsPure + "\" itemIdDigitsSuffix = \"" + itemIdDigitsSuffix + "\" nextItemIdDigits = \"" + nextItemIdDigits + "\".");
4706 19 Mar 18 olle 1860     }
4908 12 Jul 18 olle 1861     return nextItemIdDigits;
4706 19 Mar 18 olle 1862   }
4706 19 Mar 18 olle 1863
4908 12 Jul 18 olle 1864   private PdfUtil createFfpeForm(DbControl dc, PdfUtil pdfUtil, String itemId, String pad, String personalNumber, String familyName, String allFirstNames, List<String> formTemplateFileList)
4833 07 Jun 18 olle 1865     throws IOException
4833 07 Jun 18 olle 1866   {    
4833 07 Jun 18 olle 1867     // Get PDF templates of same type
4908 12 Jul 18 olle 1868     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 1869     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 1870     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, REF_FORM_TEMPLATE_FFPE_REFERRAL);
4833 07 Jun 18 olle 1871     for (File pdfFile: pdfFiles)
4833 07 Jun 18 olle 1872     {
4908 12 Jul 18 olle 1873       pdfUtil = createFfpeForm(dc, pdfUtil, pdfFile, itemId, pad, personalNumber, familyName, allFirstNames, formTemplateFileList);
4833 07 Jun 18 olle 1874     }
4833 07 Jun 18 olle 1875
4833 07 Jun 18 olle 1876     return pdfUtil;
4833 07 Jun 18 olle 1877   }
4833 07 Jun 18 olle 1878
4908 12 Jul 18 olle 1879   private PdfUtil createFfpeForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, String pad, String personalNumber, String familyName, String allFirstNames, List<String> formTemplateFileList)
4833 07 Jun 18 olle 1880     throws IOException
5028 15 Oct 18 olle 1881   {
4833 07 Jun 18 olle 1882     if (pdfFile == null)
4833 07 Jun 18 olle 1883     {
4833 07 Jun 18 olle 1884       return pdfUtil;
4833 07 Jun 18 olle 1885     }
4833 07 Jun 18 olle 1886     if (!formTemplateFileList.contains(pdfFile.getName()))
4833 07 Jun 18 olle 1887     {
4833 07 Jun 18 olle 1888       formTemplateFileList.add(pdfFile.getName());
4833 07 Jun 18 olle 1889     }
4833 07 Jun 18 olle 1890     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4833 07 Jun 18 olle 1891     pdfUtil.newPage();
4833 07 Jun 18 olle 1892     // Import PDF document template
4833 07 Jun 18 olle 1893     float xOrig = 0.0f;
4833 07 Jun 18 olle 1894     float yOrig = 0.0f;
4833 07 Jun 18 olle 1895     float scaleX = 1.0f;
4833 07 Jun 18 olle 1896     float scaleY = 1.0f;
4833 07 Jun 18 olle 1897     int pageNo = 1;
4833 07 Jun 18 olle 1898     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4871 26 Jun 18 olle 1899
4908 12 Jul 18 olle 1900     // Item ID below bar code in upper right section
5028 15 Oct 18 olle 1901     //String text = itemId;
5028 15 Oct 18 olle 1902     String text = personalNumber;
4833 07 Jun 18 olle 1903     //float textFontSize = 11.9f;
4833 07 Jun 18 olle 1904     float textFontSize = 10.0f;
4833 07 Jun 18 olle 1905     int align = Element.ALIGN_LEFT;
4833 07 Jun 18 olle 1906     float xTextUpperRight = 357.0f;
4871 26 Jun 18 olle 1907     float yTextUpperRight = 627.0f;
4833 07 Jun 18 olle 1908     pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
4833 07 Jun 18 olle 1909     //
4833 07 Jun 18 olle 1910     //pdfUtil.drawGrid();
4833 07 Jun 18 olle 1911
4833 07 Jun 18 olle 1912     // Barcode at upper right
4833 07 Jun 18 olle 1913     float xBarcodeUpperRight = xTextUpperRight;
4833 07 Jun 18 olle 1914     float yBarcodeUpperRight = yTextUpperRight + 10.0f;
4833 07 Jun 18 olle 1915     //float width = 94.0f;
4833 07 Jun 18 olle 1916     float width = 105.8f;
4833 07 Jun 18 olle 1917     float height = 20.0f;
4833 07 Jun 18 olle 1918     align = Element.ALIGN_LEFT;
5028 15 Oct 18 olle 1919     pdfUtil.addBarcode(text, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
4833 07 Jun 18 olle 1920
4833 07 Jun 18 olle 1921     // Barcode at upper left
4833 07 Jun 18 olle 1922     float xBarcodeUpperLeft = 52.0f;
4833 07 Jun 18 olle 1923     float yBarcodeUpperLeft = 705.0f;
4833 07 Jun 18 olle 1924     align = Element.ALIGN_LEFT;
5028 15 Oct 18 olle 1925     pdfUtil.addBarcode(text, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
4833 07 Jun 18 olle 1926     textFontSize = 10.0f;
4833 07 Jun 18 olle 1927     float xTextUpperLeft = xBarcodeUpperLeft;
4833 07 Jun 18 olle 1928     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4833 07 Jun 18 olle 1929     pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
4833 07 Jun 18 olle 1930
5028 15 Oct 18 olle 1931 /*
4833 07 Jun 18 olle 1932     // PAD number below bar code/text in upper right section
4833 07 Jun 18 olle 1933     textFontSize = 10.0f;
4833 07 Jun 18 olle 1934     align = Element.ALIGN_LEFT;
4833 07 Jun 18 olle 1935     //float xTextUpperRight = 357.0f;
4833 07 Jun 18 olle 1936     //float yTextUpperRight = 642.0f;
4833 07 Jun 18 olle 1937     yTextUpperRight = 600.0f;
4833 07 Jun 18 olle 1938     pdfUtil.addBoldText(pad, textFontSize, align, xTextUpperRight, yTextUpperRight);
5028 15 Oct 18 olle 1939 */
4833 07 Jun 18 olle 1940
5116 20 Nov 18 olle 1941 /*
4865 20 Jun 18 olle 1942     // Personal number at top above bar code/text in upper right section
4865 20 Jun 18 olle 1943     textFontSize = 10.0f;
4865 20 Jun 18 olle 1944     align = Element.ALIGN_LEFT;
4865 20 Jun 18 olle 1945     //float xTextUpperRight = 357.0f;
4865 20 Jun 18 olle 1946     //float yTextUpperRight = 642.0f;
4865 20 Jun 18 olle 1947     //yTextUpperRight = 742.0f;
4871 26 Jun 18 olle 1948     yTextUpperRight = 727.0f;
4866 21 Jun 18 olle 1949     pdfUtil.addBoldText(personalNumber, textFontSize, align, xTextUpperRight, yTextUpperRight);
5116 20 Nov 18 olle 1950 */
4865 20 Jun 18 olle 1951
5006 04 Oct 18 olle 1952 /*
4865 20 Jun 18 olle 1953     // Family name above bar code/text in upper right section
4865 20 Jun 18 olle 1954     textFontSize = 10.0f;
4865 20 Jun 18 olle 1955     align = Element.ALIGN_LEFT;
4865 20 Jun 18 olle 1956     //float xTextUpperRight = 357.0f;
4865 20 Jun 18 olle 1957     //float yTextUpperRight = 642.0f;
4865 20 Jun 18 olle 1958     //xTextUpperRight = 250.0f;
4865 20 Jun 18 olle 1959     xTextUpperRight = 265.0f;
4871 26 Jun 18 olle 1960     yTextUpperRight = 698.0f;
4871 26 Jun 18 olle 1961     pdfUtil.addBoldText(familyName, textFontSize, align, xTextUpperRight, yTextUpperRight);
4865 20 Jun 18 olle 1962
4865 20 Jun 18 olle 1963     // All first names above bar code/text in upper right section
4865 20 Jun 18 olle 1964     textFontSize = 10.0f;
4865 20 Jun 18 olle 1965     align = Element.ALIGN_LEFT;
4865 20 Jun 18 olle 1966     //float xTextUpperRight = 357.0f;
4865 20 Jun 18 olle 1967     //float yTextUpperRight = 642.0f;
4865 20 Jun 18 olle 1968     //xTextUpperRight = 450.0f;
4865 20 Jun 18 olle 1969     xTextUpperRight = 390.0f;
4865 20 Jun 18 olle 1970     //yTextUpperRight = 700.0f;
4871 26 Jun 18 olle 1971     pdfUtil.addBoldText(allFirstNames, textFontSize, align, xTextUpperRight, yTextUpperRight);
5006 04 Oct 18 olle 1972 */
5006 04 Oct 18 olle 1973     // Combined family and all first names above bar code/text in upper right section
5006 04 Oct 18 olle 1974     String combinedNames = familyName + ", " + allFirstNames;
5006 04 Oct 18 olle 1975     textFontSize = 10.0f;
5006 04 Oct 18 olle 1976     align = Element.ALIGN_LEFT;
5006 04 Oct 18 olle 1977     //float xTextUpperRight = 357.0f;
5006 04 Oct 18 olle 1978     //float yTextUpperRight = 642.0f;
5006 04 Oct 18 olle 1979     //xTextUpperRight = 250.0f;
5006 04 Oct 18 olle 1980     xTextUpperRight = 265.0f;
5006 04 Oct 18 olle 1981     yTextUpperRight = 698.0f;
5006 04 Oct 18 olle 1982     pdfUtil.addBoldText(combinedNames, textFontSize, align, xTextUpperRight, yTextUpperRight);
4865 20 Jun 18 olle 1983
4833 07 Jun 18 olle 1984     // Add blank extra page
4833 07 Jun 18 olle 1985     //pdfUtil.newBlankPage();
4833 07 Jun 18 olle 1986
4833 07 Jun 18 olle 1987     return pdfUtil;
4833 07 Jun 18 olle 1988   }
4833 07 Jun 18 olle 1989
4909 12 Jul 18 olle 1990   private PdfUtil createFfpeSectionOrderForm(DbControl dc, PdfUtil pdfUtil, List<String> itemIdList, List<String> formTemplateFileList)
4909 12 Jul 18 olle 1991     throws IOException
4909 12 Jul 18 olle 1992   {    
4909 12 Jul 18 olle 1993     // Get PDF templates of same type
4909 12 Jul 18 olle 1994     String itemIdDigits = "";
4909 12 Jul 18 olle 1995     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, REF_FORM_TEMPLATE_FFPE_SECTION_ORDER_FORM);
4910 12 Jul 18 olle 1996     // Call form creation method the needed number of times
4910 12 Jul 18 olle 1997     int maxEntriesPerForm = 8;
4910 12 Jul 18 olle 1998     int itemsProcessed = 0;
4910 12 Jul 18 olle 1999     int itemsLeft = itemIdList.size();
4910 12 Jul 18 olle 2000     while (itemsLeft > 0)
4909 12 Jul 18 olle 2001     {
4910 12 Jul 18 olle 2002       List<String> tmpItemIdList = new ArrayList<String>();
4910 12 Jul 18 olle 2003       int startItem = itemsProcessed;
4910 12 Jul 18 olle 2004       for (int i = 0; (i < maxEntriesPerForm) && (i < itemsLeft); i++)
4910 12 Jul 18 olle 2005       {
4910 12 Jul 18 olle 2006         tmpItemIdList.add((String)itemIdList.get(startItem + i));
4910 12 Jul 18 olle 2007         itemsProcessed++;
4910 12 Jul 18 olle 2008       }
4910 12 Jul 18 olle 2009       itemsLeft -= itemsProcessed;
4910 12 Jul 18 olle 2010       // Generate a new FFPE section order form page
4910 12 Jul 18 olle 2011       for (File pdfFile: pdfFiles)
4910 12 Jul 18 olle 2012       {
4910 12 Jul 18 olle 2013         pdfUtil = createFfpeSectionOrderForm(dc, pdfUtil, pdfFile, tmpItemIdList, formTemplateFileList);
4910 12 Jul 18 olle 2014       }
4909 12 Jul 18 olle 2015     }
4909 12 Jul 18 olle 2016
4909 12 Jul 18 olle 2017     return pdfUtil;
4909 12 Jul 18 olle 2018   }
4909 12 Jul 18 olle 2019
4909 12 Jul 18 olle 2020   private PdfUtil createFfpeSectionOrderForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, List<String> itemIdList, List<String> formTemplateFileList)
4909 12 Jul 18 olle 2021     throws IOException
4909 12 Jul 18 olle 2022   {
4909 12 Jul 18 olle 2023     // Use stored annotation snapshots for performance reasons
4909 12 Jul 18 olle 2024     SnapshotManager manager = new SnapshotManager();        
4909 12 Jul 18 olle 2025     if (pdfFile == null)
4909 12 Jul 18 olle 2026     {
4909 12 Jul 18 olle 2027       return pdfUtil;
4909 12 Jul 18 olle 2028     }
4909 12 Jul 18 olle 2029     if (!formTemplateFileList.contains(pdfFile.getName()))
4909 12 Jul 18 olle 2030     {
4909 12 Jul 18 olle 2031       formTemplateFileList.add(pdfFile.getName());
4909 12 Jul 18 olle 2032     }
4909 12 Jul 18 olle 2033     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4909 12 Jul 18 olle 2034     pdfUtil.newPage();
4909 12 Jul 18 olle 2035     // Import PDF document template
4909 12 Jul 18 olle 2036     float xOrig = 0.0f;
4909 12 Jul 18 olle 2037     float yOrig = 0.0f;
4909 12 Jul 18 olle 2038     float scaleX = 1.0f;
4909 12 Jul 18 olle 2039     float scaleY = 1.0f;
4909 12 Jul 18 olle 2040     int pageNo = 1;
4909 12 Jul 18 olle 2041     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4909 12 Jul 18 olle 2042
4909 12 Jul 18 olle 2043     for (int i = 0; i < itemIdList.size(); i++)
4909 12 Jul 18 olle 2044     {
4909 12 Jul 18 olle 2045       // Get item name from list
4909 12 Jul 18 olle 2046       String itemId = (String)itemIdList.get(i);
4909 12 Jul 18 olle 2047       // Get FFPE block item from item name in order to get annotation
4909 12 Jul 18 olle 2048       FfpeBlock ffpeBlock = FfpeBlock.getByName(dc, itemId);
4909 12 Jul 18 olle 2049       System.out.println("ReferralGeneratorServlet::createFfpeSectionOrderForm(): i = " + i + " ffpeBlock = " + ffpeBlock);
4909 12 Jul 18 olle 2050       // Only print referral info for existing FFPE block items
4909 12 Jul 18 olle 2051       if (ffpeBlock != null)
4909 12 Jul 18 olle 2052       {
4909 12 Jul 18 olle 2053         String pad = (String) Annotationtype.PAD.getAnnotationValue(dc, manager, ffpeBlock.getSample());
4909 12 Jul 18 olle 2054         // Exchange null value for empty string in order not to upset bar code utilities 
4909 12 Jul 18 olle 2055         if (pad == null)
4909 12 Jul 18 olle 2056         {
4909 12 Jul 18 olle 2057           pad = "";
4909 12 Jul 18 olle 2058         }
5082 12 Nov 18 olle 2059         String materialNumber = (String) Annotationtype.MATERIAL_NUMBER.getAnnotationValue(dc, manager, ffpeBlock.getSample());
5082 12 Nov 18 olle 2060         // Exchange null value for empty string in order not to upset bar code utilities 
5082 12 Nov 18 olle 2061         if (materialNumber == null)
5082 12 Nov 18 olle 2062         {
5082 12 Nov 18 olle 2063           materialNumber = "";
5082 12 Nov 18 olle 2064         }
5854 06 Mar 20 olle 2065         String combinedPadMaterialNumber = pad + "_" + materialNumber;
5082 12 Nov 18 olle 2066         System.out.println("ReferralGeneratorServlet::createFfpeSectionOrderForm(): i = " + i + " materialNumber = " + materialNumber + " pad = " + pad);
5053 26 Oct 18 olle 2067         String yellowLabel = (String) Annotationtype.YELLOW_LABEL.getAnnotationValue(dc, manager, ffpeBlock.getSample());
4909 12 Jul 18 olle 2068
5082 12 Nov 18 olle 2069         float singleRowOffset = 84.7f;
4909 12 Jul 18 olle 2070         float rowOffset = singleRowOffset * i;
4909 12 Jul 18 olle 2071
5082 12 Nov 18 olle 2072         String text = "";
4909 12 Jul 18 olle 2073         float xTextUpperRight = 365.0f;
5082 12 Nov 18 olle 2074         float yTextUpperRight = 717.0f - rowOffset;
4909 12 Jul 18 olle 2075         float yBarcodeUpperRight = yTextUpperRight + 10.0f;
5082 12 Nov 18 olle 2076         int align = Element.ALIGN_LEFT;
5082 12 Nov 18 olle 2077         float textFontSize = 8.0f;
5082 12 Nov 18 olle 2078         float width = 85.0f;
4909 12 Jul 18 olle 2079         float height = 16.0f;
4909 12 Jul 18 olle 2080
5854 06 Mar 20 olle 2081 /*
5393 02 May 19 olle 2082         // Barcode with PAD number at upper left
5393 02 May 19 olle 2083         text = pad;
5082 12 Nov 18 olle 2084         float xBarcodeUpperLeft = 120.0f;
4909 12 Jul 18 olle 2085         float yBarcodeUpperLeft = yBarcodeUpperRight;
4909 12 Jul 18 olle 2086         align = Element.ALIGN_LEFT;
5082 12 Nov 18 olle 2087         pdfUtil.addBarcode(text, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
5052 25 Oct 18 olle 2088         
5393 02 May 19 olle 2089         // PAD number below bar code at upper left
4909 12 Jul 18 olle 2090         float xTextUpperLeft = xBarcodeUpperLeft;
4909 12 Jul 18 olle 2091         float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4909 12 Jul 18 olle 2092         pdfUtil.addBoldText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
5854 06 Mar 20 olle 2093 */
5082 12 Nov 18 olle 2094
5854 06 Mar 20 olle 2095         // Barcode with combined PAD/Material number at upper left
5854 06 Mar 20 olle 2096         text = combinedPadMaterialNumber;
5854 06 Mar 20 olle 2097         float xBarcodeUpperLeft = 120.0f;
5854 06 Mar 20 olle 2098         float yBarcodeUpperLeft = yBarcodeUpperRight;
5854 06 Mar 20 olle 2099         align = Element.ALIGN_LEFT;
5854 06 Mar 20 olle 2100         pdfUtil.addBarcode(text, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
5854 06 Mar 20 olle 2101         
5854 06 Mar 20 olle 2102         // Combined PAD/Material number below bar code at upper left
5854 06 Mar 20 olle 2103         float xTextUpperLeft = xBarcodeUpperLeft;
5854 06 Mar 20 olle 2104         float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
5854 06 Mar 20 olle 2105         pdfUtil.addBoldText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
5854 06 Mar 20 olle 2106
5082 12 Nov 18 olle 2107         // Glass slide ".F" label name
5082 12 Nov 18 olle 2108         text = itemId + ".F";
5082 12 Nov 18 olle 2109         float xHeGlassTextUpperLeft = xBarcodeUpperLeft + 15.0f;
5082 12 Nov 18 olle 2110         float yHeGlassTextUpperLeft = yBarcodeUpperLeft - 35.0f;
5082 12 Nov 18 olle 2111         pdfUtil.addBoldText(text, textFontSize, align, xHeGlassTextUpperLeft, yHeGlassTextUpperLeft);
5082 12 Nov 18 olle 2112
5082 12 Nov 18 olle 2113         // Glass slide ".E" label name
5082 12 Nov 18 olle 2114         text = itemId + ".E";
5082 12 Nov 18 olle 2115         yHeGlassTextUpperLeft = yHeGlassTextUpperLeft - 24.0f;
5082 12 Nov 18 olle 2116         pdfUtil.addBoldText(text, textFontSize, align, xHeGlassTextUpperLeft, yHeGlassTextUpperLeft);
5082 12 Nov 18 olle 2117
5393 02 May 19 olle 2118         // Barcode with material number (block number) at center
5393 02 May 19 olle 2119         text = materialNumber;
5082 12 Nov 18 olle 2120         float xBarcodeCenter = 290.0f;
5082 12 Nov 18 olle 2121         float yBarcodeCenter = yBarcodeUpperLeft;
5082 12 Nov 18 olle 2122         align = Element.ALIGN_LEFT;
5393 02 May 19 olle 2123         pdfUtil.addBarcode(text, xBarcodeCenter, yBarcodeCenter, width, height, align);
5082 12 Nov 18 olle 2124         
5393 02 May 19 olle 2125         // Material number below bar code at center
5082 12 Nov 18 olle 2126         float xTextCenter = xBarcodeCenter;
5082 12 Nov 18 olle 2127         float yTextCenter = yBarcodeCenter - 10.0f;
5082 12 Nov 18 olle 2128         pdfUtil.addBoldText(text, textFontSize, align, xTextCenter, yTextCenter);
5053 26 Oct 18 olle 2129         // Add large asterisk "*" as prefix to name for "Yellow label" FFPE block ID, but no change in bar code
5053 26 Oct 18 olle 2130         if (yellowLabel != null && yellowLabel.equals("yellow"))
5053 26 Oct 18 olle 2131         {
5154 28 Nov 18 olle 2132 /*
5053 26 Oct 18 olle 2133           text = "*";
5053 26 Oct 18 olle 2134           // Adjust horizontal position to place large asterisk to the left of FFPE block ID
5082 12 Nov 18 olle 2135           xTextCenter = xBarcodeCenter - 9.0f;
5053 26 Oct 18 olle 2136           // Adjust vertical position to place large asterisk at same level as FFPE block ID
5082 12 Nov 18 olle 2137           yTextCenter = yBarcodeCenter - 13.7f;
5053 26 Oct 18 olle 2138           // Use font size 12 instead of 8 for asterisk prefix
5154 28 Nov 18 olle 2139           float asteriskFontSize = 12.0f;
5082 12 Nov 18 olle 2140           pdfUtil.addBoldText(text, asteriskFontSize, align, xTextCenter, yTextCenter);
5154 28 Nov 18 olle 2141 */
5154 28 Nov 18 olle 2142           URL imageUrl = Meludi.class.getResource("/resources/images/yellow-label-small.png");
5154 28 Nov 18 olle 2143           float xImage = xBarcodeCenter - 14.0f;
5154 28 Nov 18 olle 2144           float yImage = yBarcodeCenter - 13.0f;
5154 28 Nov 18 olle 2145           float imageWidth = 12.0f;
5154 28 Nov 18 olle 2146           float imageHeight = 12.0f;
5154 28 Nov 18 olle 2147           pdfUtil.importImage(imageUrl.openStream(), xImage, yImage, imageWidth, imageHeight, align);
5053 26 Oct 18 olle 2148         }
5082 12 Nov 18 olle 2149
5082 12 Nov 18 olle 2150         // Barcode with FFPE section name at upper right
5082 12 Nov 18 olle 2151         text = itemId + ".s";
5082 12 Nov 18 olle 2152         float xBarcodeUpperRight = 450.0f;
5082 12 Nov 18 olle 2153         yBarcodeUpperRight = yBarcodeUpperLeft;
5082 12 Nov 18 olle 2154         align = Element.ALIGN_LEFT;
5082 12 Nov 18 olle 2155         pdfUtil.addBarcode(text, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
5052 25 Oct 18 olle 2156         
5082 12 Nov 18 olle 2157         // FFPE section name below bar code at upper right
5082 12 Nov 18 olle 2158         xTextUpperRight = xBarcodeUpperRight;
5082 12 Nov 18 olle 2159         yTextUpperRight = yBarcodeUpperRight - 10.0f;
5082 12 Nov 18 olle 2160         pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
5082 12 Nov 18 olle 2161         // Add large asterisk "*" as prefix to name for "Yellow label" FFPE block ID, but no change in bar code
5082 12 Nov 18 olle 2162         if (yellowLabel != null && yellowLabel.equals("yellow"))
5082 12 Nov 18 olle 2163         {
5154 28 Nov 18 olle 2164 /*
5082 12 Nov 18 olle 2165           text = "*";
5082 12 Nov 18 olle 2166           // Adjust horizontal position to place large asterisk to the left of FFPE block ID
5082 12 Nov 18 olle 2167           xTextUpperRight = xBarcodeUpperRight - 9.0f;
5082 12 Nov 18 olle 2168           // Adjust vertical position to place large asterisk at same level as FFPE block ID
5082 12 Nov 18 olle 2169           yTextUpperRight = yBarcodeUpperRight - 13.7f;
5082 12 Nov 18 olle 2170           // Use font size 12 instead of 8 for asterisk prefix
5082 12 Nov 18 olle 2171           float asteriskFontSize = 12.0f;          
5082 12 Nov 18 olle 2172           pdfUtil.addBoldText(text, asteriskFontSize, align, xTextUpperRight, yTextUpperRight);
5154 28 Nov 18 olle 2173 */
5154 28 Nov 18 olle 2174           URL imageUrl = Meludi.class.getResource("/resources/images/yellow-label-small.png");
5154 28 Nov 18 olle 2175           float xImage = xBarcodeUpperRight - 14.0f;
5154 28 Nov 18 olle 2176           float yImage = yBarcodeUpperRight - 13.0f;
5154 28 Nov 18 olle 2177           float imageWidth = 12.0f;
5154 28 Nov 18 olle 2178           float imageHeight = 12.0f;
5154 28 Nov 18 olle 2179           pdfUtil.importImage(imageUrl.openStream(), xImage, yImage, imageWidth, imageHeight, align);
5082 12 Nov 18 olle 2180         }
4909 12 Jul 18 olle 2181       }
4909 12 Jul 18 olle 2182     }
4909 12 Jul 18 olle 2183
4909 12 Jul 18 olle 2184     // Add blank extra page
4909 12 Jul 18 olle 2185     //pdfUtil.newBlankPage();
4909 12 Jul 18 olle 2186
4909 12 Jul 18 olle 2187     return pdfUtil;
4909 12 Jul 18 olle 2188   }
4909 12 Jul 18 olle 2189
4908 12 Jul 18 olle 2190   private PdfUtil createBarcodeNonStickerForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4767 17 Apr 18 olle 2191     throws IOException
4767 17 Apr 18 olle 2192   {    
4767 17 Apr 18 olle 2193     // Get PDF templates of same type
4908 12 Jul 18 olle 2194     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2195     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2196     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, REF_FORM_TEMPLATE_CONSENT_REPORT);
4767 17 Apr 18 olle 2197     for (File pdfFile: pdfFiles)
4767 17 Apr 18 olle 2198     {
4908 12 Jul 18 olle 2199       pdfUtil = createBarcodeNonStickerForm(dc, pdfUtil, pdfFile, itemId, formTemplateFileList);
4767 17 Apr 18 olle 2200     }
4767 17 Apr 18 olle 2201
4951 30 Aug 18 olle 2202     // Add blank extra page
5202 07 Jan 19 olle 2203     //pdfUtil.newBlankPage();
4951 30 Aug 18 olle 2204
4767 17 Apr 18 olle 2205     return pdfUtil;
4767 17 Apr 18 olle 2206   }
4767 17 Apr 18 olle 2207
4908 12 Jul 18 olle 2208   private PdfUtil createBarcodeNonStickerForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, List<String> formTemplateFileList)
4767 17 Apr 18 olle 2209     throws IOException
4767 17 Apr 18 olle 2210   {    
4767 17 Apr 18 olle 2211     if (pdfFile == null)
4767 17 Apr 18 olle 2212     {
4767 17 Apr 18 olle 2213       return pdfUtil;
4767 17 Apr 18 olle 2214     }
4767 17 Apr 18 olle 2215     if (!formTemplateFileList.contains(pdfFile.getName()))
4767 17 Apr 18 olle 2216     {
4767 17 Apr 18 olle 2217       formTemplateFileList.add(pdfFile.getName());
4767 17 Apr 18 olle 2218     }
4767 17 Apr 18 olle 2219     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4767 17 Apr 18 olle 2220     pdfUtil.newPage();
4767 17 Apr 18 olle 2221     // Import PDF document template
4767 17 Apr 18 olle 2222     float xOrig = 0.0f;
4767 17 Apr 18 olle 2223     float yOrig = 0.0f;
4767 17 Apr 18 olle 2224     float scaleX = 1.0f;
4767 17 Apr 18 olle 2225     float scaleY = 1.0f;
4767 17 Apr 18 olle 2226     int pageNo = 1;
4767 17 Apr 18 olle 2227     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
5700 05 Nov 19 olle 2228     // Item ID below bar code
4908 12 Jul 18 olle 2229     String text = itemId;
4784 26 Apr 18 olle 2230     //float textFontSize = 11.9f;
4784 26 Apr 18 olle 2231     float textFontSize = 10.0f;
4767 17 Apr 18 olle 2232     int align = Element.ALIGN_LEFT;
4784 26 Apr 18 olle 2233     //float width = 94.0f;
4784 26 Apr 18 olle 2234     float width = 105.8f;
4767 17 Apr 18 olle 2235     float height = 20.0f;
5700 05 Nov 19 olle 2236     // Get project name
5700 05 Nov 19 olle 2237     SessionControl sc = dc.getSessionControl();
5700 05 Nov 19 olle 2238     Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5700 05 Nov 19 olle 2239     String projectName = activeProject.getName();
5700 05 Nov 19 olle 2240     if (!projectName.equals("NordicTrip"))
5700 05 Nov 19 olle 2241     {
5700 05 Nov 19 olle 2242       // Item ID below bar code in upper right section
5700 05 Nov 19 olle 2243       float xTextUpperRight = 357.0f;
5700 05 Nov 19 olle 2244       float yTextUpperRight = 642.0f;
5700 05 Nov 19 olle 2245       pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
5700 05 Nov 19 olle 2246       //
5700 05 Nov 19 olle 2247       //pdfUtil.drawGrid();
4767 17 Apr 18 olle 2248
5700 05 Nov 19 olle 2249       // Barcode at upper right
5700 05 Nov 19 olle 2250       float xBarcodeUpperRight = xTextUpperRight;
5700 05 Nov 19 olle 2251       float yBarcodeUpperRight = yTextUpperRight + 10.0f;
5700 05 Nov 19 olle 2252       align = Element.ALIGN_LEFT;
5700 05 Nov 19 olle 2253       pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
5700 05 Nov 19 olle 2254     }
5700 05 Nov 19 olle 2255
4767 17 Apr 18 olle 2256     // Barcode at upper left
4767 17 Apr 18 olle 2257     float xBarcodeUpperLeft = 52.0f;
4767 17 Apr 18 olle 2258     float yBarcodeUpperLeft = 705.0f;
4767 17 Apr 18 olle 2259     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 2260     pdfUtil.addBarcode(itemId, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
4767 17 Apr 18 olle 2261     textFontSize = 10.0f;
4767 17 Apr 18 olle 2262     float xTextUpperLeft = xBarcodeUpperLeft;
4767 17 Apr 18 olle 2263     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4767 17 Apr 18 olle 2264     pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
4767 17 Apr 18 olle 2265
4767 17 Apr 18 olle 2266     // Add blank extra page
4767 17 Apr 18 olle 2267     //pdfUtil.newBlankPage();
4767 17 Apr 18 olle 2268
4767 17 Apr 18 olle 2269     return pdfUtil;
4767 17 Apr 18 olle 2270   }
4767 17 Apr 18 olle 2271
4908 12 Jul 18 olle 2272   private PdfUtil createBarcodeStickerForm(DbControl dc, PdfUtil pdfUtil, String itemId, String formTemplateName, List<String> formTemplateFileList)
4767 17 Apr 18 olle 2273     throws IOException
4767 17 Apr 18 olle 2274   {    
4767 17 Apr 18 olle 2275     // Get PDF templates of same type
4908 12 Jul 18 olle 2276     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2277     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2278     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, formTemplateName);
4767 17 Apr 18 olle 2279     for (File pdfFile: pdfFiles)
4767 17 Apr 18 olle 2280     {
4908 12 Jul 18 olle 2281       pdfUtil = createBarcodeStickerForm(dc, pdfUtil, pdfFile, itemId, formTemplateName, formTemplateFileList);
4767 17 Apr 18 olle 2282     }
4767 17 Apr 18 olle 2283
4767 17 Apr 18 olle 2284     return pdfUtil;
4767 17 Apr 18 olle 2285   }
4767 17 Apr 18 olle 2286
4908 12 Jul 18 olle 2287   private PdfUtil createBarcodeStickerForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, String formTemplateName, List<String> formTemplateFileList)
4767 17 Apr 18 olle 2288     throws IOException
4767 17 Apr 18 olle 2289   {    
4767 17 Apr 18 olle 2290     if (pdfFile == null)
4767 17 Apr 18 olle 2291     {
4767 17 Apr 18 olle 2292       return pdfUtil;
4767 17 Apr 18 olle 2293     }
5700 05 Nov 19 olle 2294     System.out.println("ReferralGeneratorServlet::createBarcodeStickerForm(): pdfFile.getName() = " + pdfFile.getName() + " itemId = " + itemId + " formTemplateFileList = " + formTemplateFileList);
5732 19 Nov 19 olle 2295     String pdfFilename = pdfFile.getName();
5732 19 Nov 19 olle 2296     if (!formTemplateFileList.contains(pdfFilename))
4767 17 Apr 18 olle 2297     {
5732 19 Nov 19 olle 2298       formTemplateFileList.add(pdfFilename);
4767 17 Apr 18 olle 2299     }
4767 17 Apr 18 olle 2300     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4767 17 Apr 18 olle 2301     pdfUtil.newPage();
4767 17 Apr 18 olle 2302     // Import PDF document template
4767 17 Apr 18 olle 2303     float xOrig = 0.0f;
4767 17 Apr 18 olle 2304     float yOrig = 0.0f;
4767 17 Apr 18 olle 2305     float scaleX = 1.0f;
4767 17 Apr 18 olle 2306     float scaleY = 1.0f;
4767 17 Apr 18 olle 2307     int pageNo = 1;
4767 17 Apr 18 olle 2308     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4767 17 Apr 18 olle 2309
5703 06 Nov 19 olle 2310     // Get project name
5703 06 Nov 19 olle 2311     SessionControl sc = dc.getSessionControl();
5703 06 Nov 19 olle 2312     Project activeProject = Project.getById(dc, sc.getActiveProjectId());
5703 06 Nov 19 olle 2313     String projectName = activeProject.getName();
4767 17 Apr 18 olle 2314
5703 06 Nov 19 olle 2315     // No bar code/text for NordicTrip whole blood clinical sticker form
5703 06 Nov 19 olle 2316     if (projectName.equals("NordicTrip") && (pdfFile.getName().indexOf("clinical_0A") > -1))
5703 06 Nov 19 olle 2317     {
5703 06 Nov 19 olle 2318       return pdfUtil;
5703 06 Nov 19 olle 2319     }
5727 14 Nov 19 olle 2320
5732 19 Nov 19 olle 2321     // Referral form identification suffix for NordicTrip project
5732 19 Nov 19 olle 2322     String refFormIdentSuffix = "";
5732 19 Nov 19 olle 2323     if (projectName.equals("NordicTrip"))
5732 19 Nov 19 olle 2324     {
5732 19 Nov 19 olle 2325       // Find form type suffix from form template filename
5732 19 Nov 19 olle 2326       String formTypeSuffix = "";
5732 19 Nov 19 olle 2327       if (pdfFilename.indexOf("_blood") > -1)
5732 19 Nov 19 olle 2328       {
5732 19 Nov 19 olle 2329         formTypeSuffix = "B";
5732 19 Nov 19 olle 2330       }
5732 19 Nov 19 olle 2331       else if (pdfFilename.indexOf("_Tissue") > -1)
5732 19 Nov 19 olle 2332       {
5732 19 Nov 19 olle 2333         formTypeSuffix = "T";
5732 19 Nov 19 olle 2334       }
5732 19 Nov 19 olle 2335       // Find form number suffix
5732 19 Nov 19 olle 2336       String formNumberSuffix = "";
5732 19 Nov 19 olle 2337       int usIndex;
5732 19 Nov 19 olle 2338       usIndex = pdfFilename.lastIndexOf("_");
5732 19 Nov 19 olle 2339       if (usIndex > -1)
5732 19 Nov 19 olle 2340       {
5732 19 Nov 19 olle 2341         // Get part of filename after last underscore
5732 19 Nov 19 olle 2342         formNumberSuffix = pdfFilename.substring(usIndex + 1);
5732 19 Nov 19 olle 2343         // Get part of form number suffix before first dot "."
5732 19 Nov 19 olle 2344         int dotIndex;
5732 19 Nov 19 olle 2345         dotIndex = formNumberSuffix.indexOf(".");
5732 19 Nov 19 olle 2346         if (dotIndex > -1)
5732 19 Nov 19 olle 2347         {
5732 19 Nov 19 olle 2348           formNumberSuffix = formNumberSuffix.substring(0, dotIndex);
5732 19 Nov 19 olle 2349         }
5732 19 Nov 19 olle 2350       }
5732 19 Nov 19 olle 2351       // Construct referral form identification suffix
5732 19 Nov 19 olle 2352       refFormIdentSuffix = formTypeSuffix + formNumberSuffix;
5732 19 Nov 19 olle 2353       // Map original referral from identification string to single lower-case letter suffix
5732 19 Nov 19 olle 2354       refFormIdentSuffix = refFormTemplTypeIdentSuffixHM.get(refFormIdentSuffix);
5732 19 Nov 19 olle 2355       
5732 19 Nov 19 olle 2356       itemId = itemId + refFormIdentSuffix;
5732 19 Nov 19 olle 2357     }
5732 19 Nov 19 olle 2358     
5727 14 Nov 19 olle 2359     // Whole blood form flag
5727 14 Nov 19 olle 2360     Boolean wholeBloodFormFlag = false;
5732 19 Nov 19 olle 2361     String wholeBloodLabelSuffixStart = ".b";
5732 19 Nov 19 olle 2362     float wholeBloodLabelBarcodeWidth = 61.7f;
5732 19 Nov 19 olle 2363     float wholeBloodLabelBarcodeHeight = 13.5f;
5727 14 Nov 19 olle 2364     if (formTemplateName.equals(REF_FORM_TEMPLATE_WHOLE_BLOOD))
5727 14 Nov 19 olle 2365     {
5727 14 Nov 19 olle 2366       wholeBloodFormFlag = true;
5727 14 Nov 19 olle 2367     }
5732 19 Nov 19 olle 2368     if (projectName.equals("NordicTrip") && (pdfFilename.indexOf("_blood") > -1))
5727 14 Nov 19 olle 2369     {
5727 14 Nov 19 olle 2370       wholeBloodFormFlag = true;
5732 19 Nov 19 olle 2371       wholeBloodLabelSuffixStart = "";
5732 19 Nov 19 olle 2372       wholeBloodLabelBarcodeWidth = 48.5f;
5732 19 Nov 19 olle 2373       wholeBloodLabelBarcodeHeight = 16.7f;
5727 14 Nov 19 olle 2374     }
5727 14 Nov 19 olle 2375
5703 06 Nov 19 olle 2376     String text;
5703 06 Nov 19 olle 2377     float textFontSize;
5703 06 Nov 19 olle 2378     int align;
4767 17 Apr 18 olle 2379     float width = 105.8f;
4767 17 Apr 18 olle 2380     float height = 20.0f;
4767 17 Apr 18 olle 2381
5703 06 Nov 19 olle 2382     // No bar code/text at upper right for NordicTrip sticker forms
5703 06 Nov 19 olle 2383     if (!projectName.equals("NordicTrip"))
5703 06 Nov 19 olle 2384     {
5703 06 Nov 19 olle 2385       // Item ID below bar code in upper right section
5703 06 Nov 19 olle 2386       text = itemId;
5703 06 Nov 19 olle 2387       //textFontSize = 11.9f;
5703 06 Nov 19 olle 2388       textFontSize = 10.0f;
5703 06 Nov 19 olle 2389       align = Element.ALIGN_LEFT;
5703 06 Nov 19 olle 2390       float xTextUpperRight = 357.0f;
5703 06 Nov 19 olle 2391       float yTextUpperRight = 642.0f;
5703 06 Nov 19 olle 2392       pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
5703 06 Nov 19 olle 2393
5703 06 Nov 19 olle 2394       // Barcode at upper right
5703 06 Nov 19 olle 2395       float xBarcodeUpperRight = xTextUpperRight;
5703 06 Nov 19 olle 2396       float yBarcodeUpperRight = yTextUpperRight + 10.0f;
5703 06 Nov 19 olle 2397       align = Element.ALIGN_LEFT;
5703 06 Nov 19 olle 2398       pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
5703 06 Nov 19 olle 2399     }
5703 06 Nov 19 olle 2400
4767 17 Apr 18 olle 2401     // Barcode and text at upper left
4908 12 Jul 18 olle 2402     text = itemId;
4767 17 Apr 18 olle 2403     float xBarcodeUpperLeft = 52.0f;
4767 17 Apr 18 olle 2404     float yBarcodeUpperLeft = 705.0f;
4767 17 Apr 18 olle 2405     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 2406     pdfUtil.addBarcode(itemId, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
4767 17 Apr 18 olle 2407     textFontSize = 10.0f;
4767 17 Apr 18 olle 2408     float xTextUpperLeft = xBarcodeUpperLeft;
4767 17 Apr 18 olle 2409     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4767 17 Apr 18 olle 2410     pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
4767 17 Apr 18 olle 2411
4908 12 Jul 18 olle 2412     // Item ID on both short sides of labels
4767 17 Apr 18 olle 2413     textFontSize = 6.5f;
4767 17 Apr 18 olle 2414     align = Element.ALIGN_LEFT;
4809 15 May 18 olle 2415     float xTextLowerLeft = 52.8f;
4809 15 May 18 olle 2416     float yTextLowerLeft = 145.0f;
4809 15 May 18 olle 2417     float yTextLowerLeft2 = yTextLowerLeft - 110.4f;
4767 17 Apr 18 olle 2418     float xLowerLeftDiff = 56.0f;
4767 17 Apr 18 olle 2419     // Extra correction term specific for each label
4767 17 Apr 18 olle 2420     float[] xDiffSpecArr = new float[9];
4767 17 Apr 18 olle 2421     xDiffSpecArr[0] = -3.5f;
4767 17 Apr 18 olle 2422     xDiffSpecArr[1] = -3.5f;
4767 17 Apr 18 olle 2423     xDiffSpecArr[2] = 0.0f;
4767 17 Apr 18 olle 2424     xDiffSpecArr[3] = 0.0f;
4767 17 Apr 18 olle 2425     xDiffSpecArr[4] = 0.0f;
4767 17 Apr 18 olle 2426     xDiffSpecArr[5] = 0.0f;
4767 17 Apr 18 olle 2427     xDiffSpecArr[6] = 0.0f;
4767 17 Apr 18 olle 2428     xDiffSpecArr[7] = 0.0f;
4767 17 Apr 18 olle 2429     xDiffSpecArr[8] = 0.0f;
4767 17 Apr 18 olle 2430
5703 06 Nov 19 olle 2431     int bloodLabelStartNo = 6;
5703 06 Nov 19 olle 2432     int bloodLabelStopNo = 7;
4767 17 Apr 18 olle 2433     for (int i = 0; i < 9; i++)
4767 17 Apr 18 olle 2434     {
5311 20 Feb 19 olle 2435       text = itemId;
5727 14 Nov 19 olle 2436       if (wholeBloodFormFlag)
4971 14 Sep 18 olle 2437       {
5703 06 Nov 19 olle 2438         if (i >= bloodLabelStartNo && i <= bloodLabelStopNo)
4971 14 Sep 18 olle 2439         {
5732 19 Nov 19 olle 2440           text = itemId + wholeBloodLabelSuffixStart + (i - bloodLabelStartNo + 1);
4971 14 Sep 18 olle 2441         }
4971 14 Sep 18 olle 2442       }
4767 17 Apr 18 olle 2443       float xTextLowerLeft2 = xTextLowerLeft + i*xLowerLeftDiff + xDiffSpecArr[i];
4767 17 Apr 18 olle 2444       pdfUtil.addBoldText(text, textFontSize, align, xTextLowerLeft2, yTextLowerLeft);
4767 17 Apr 18 olle 2445       pdfUtil.addBoldText(text, textFontSize, align, xTextLowerLeft2, yTextLowerLeft2);
4767 17 Apr 18 olle 2446     }
4767 17 Apr 18 olle 2447     //
4767 17 Apr 18 olle 2448     //pdfUtil.drawGrid();
4767 17 Apr 18 olle 2449
4767 17 Apr 18 olle 2450     // Barcode on labels
4971 14 Sep 18 olle 2451     String labelText = itemId;
5732 19 Nov 19 olle 2452     float labelBarcodeWidthNormal = 61.7f;
5732 19 Nov 19 olle 2453     float labelBarcodeHeightNormal = 13.5f;
4767 17 Apr 18 olle 2454     float rotationDegrees = 90.0f;
5732 19 Nov 19 olle 2455     float xLabelBarcodeLeftNormal = 67.8f;
4809 15 May 18 olle 2456     float yLabelBarcodeLeft = 72.5f;
4767 17 Apr 18 olle 2457     align = Element.ALIGN_LEFT;
4767 17 Apr 18 olle 2458     for (int i = 0; i < 9; i++)
4767 17 Apr 18 olle 2459     {
5311 20 Feb 19 olle 2460       labelText = itemId;
5732 19 Nov 19 olle 2461       float labelBarcodeWidth = labelBarcodeWidthNormal;
5732 19 Nov 19 olle 2462       float labelBarcodeHeight = labelBarcodeHeightNormal;
5732 19 Nov 19 olle 2463       float xLabelBarcodeLeft =  xLabelBarcodeLeftNormal;
5727 14 Nov 19 olle 2464       if (wholeBloodFormFlag)
4971 14 Sep 18 olle 2465       {
5703 06 Nov 19 olle 2466         if (i >= bloodLabelStartNo && i <= bloodLabelStopNo)
4971 14 Sep 18 olle 2467         {
5732 19 Nov 19 olle 2468           labelText = itemId + wholeBloodLabelSuffixStart + (i - bloodLabelStartNo + 1);
5732 19 Nov 19 olle 2469           labelBarcodeWidth = wholeBloodLabelBarcodeWidth;
5732 19 Nov 19 olle 2470           labelBarcodeHeight = wholeBloodLabelBarcodeHeight;
5732 19 Nov 19 olle 2471           xLabelBarcodeLeft = xLabelBarcodeLeftNormal - (labelBarcodeHeight - labelBarcodeHeightNormal);
4971 14 Sep 18 olle 2472         }
4971 14 Sep 18 olle 2473       }
4767 17 Apr 18 olle 2474       float xLabelBarcodeLeft2 = xLabelBarcodeLeft + i*xLowerLeftDiff + xDiffSpecArr[i];
5732 19 Nov 19 olle 2475       float xLabelBarcodeLeft2Normal = xLabelBarcodeLeftNormal + i*xLowerLeftDiff + xDiffSpecArr[i];
4971 14 Sep 18 olle 2476       pdfUtil.addBarcode(labelText, xLabelBarcodeLeft2, yLabelBarcodeLeft, labelBarcodeWidth, labelBarcodeHeight, align, rotationDegrees);
4767 17 Apr 18 olle 2477       // Barcode label text
5732 19 Nov 19 olle 2478       //float xLabelTextLeft = xLabelBarcodeLeft2 + 22.0f;
5732 19 Nov 19 olle 2479       float xLabelTextLeft = xLabelBarcodeLeft2Normal + 22.0f;
4767 17 Apr 18 olle 2480       float yLabelTextLeft = yLabelBarcodeLeft;
4767 17 Apr 18 olle 2481       float labelTextFontSize = 9.0f;
4767 17 Apr 18 olle 2482       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelTextLeft, yLabelTextLeft, rotationDegrees);
4767 17 Apr 18 olle 2483     }
4767 17 Apr 18 olle 2484
4767 17 Apr 18 olle 2485     return pdfUtil;
4767 17 Apr 18 olle 2486   }
4767 17 Apr 18 olle 2487
6783 25 Jul 22 olle 2488   private PdfUtil createLucasStickerForm(DbControl dc, PdfUtil pdfUtil, String itemId, String formTemplateName, List<String> formTemplateFileList)
6783 25 Jul 22 olle 2489     throws IOException
6783 25 Jul 22 olle 2490   {    
6783 25 Jul 22 olle 2491     // Get PDF templates of same type
6783 25 Jul 22 olle 2492     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
6783 25 Jul 22 olle 2493     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
6783 25 Jul 22 olle 2494     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, formTemplateName);
6783 25 Jul 22 olle 2495     for (File pdfFile: pdfFiles)
6783 25 Jul 22 olle 2496     {
6783 25 Jul 22 olle 2497       pdfUtil = createLucasStickerForm(dc, pdfUtil, pdfFile, itemId, formTemplateName, formTemplateFileList);
6783 25 Jul 22 olle 2498     }
6783 25 Jul 22 olle 2499
6783 25 Jul 22 olle 2500     return pdfUtil;
6783 25 Jul 22 olle 2501   }
6783 25 Jul 22 olle 2502
6783 25 Jul 22 olle 2503   private PdfUtil createLucasStickerForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, String formTemplateName, List<String> formTemplateFileList)
6783 25 Jul 22 olle 2504     throws IOException
6783 25 Jul 22 olle 2505   {    
6783 25 Jul 22 olle 2506     if (pdfFile == null)
6783 25 Jul 22 olle 2507     {
6783 25 Jul 22 olle 2508       return pdfUtil;
6783 25 Jul 22 olle 2509     }
6783 25 Jul 22 olle 2510     System.out.println("ReferralGeneratorServlet::createLucasStickerForm(): pdfFile.getName() = " + pdfFile.getName() + " itemId = " + itemId + " formTemplateFileList = " + formTemplateFileList);
6783 25 Jul 22 olle 2511     String pdfFilename = pdfFile.getName();
6783 25 Jul 22 olle 2512     if (!formTemplateFileList.contains(pdfFilename))
6783 25 Jul 22 olle 2513     {
6783 25 Jul 22 olle 2514       formTemplateFileList.add(pdfFilename);
6783 25 Jul 22 olle 2515     }
6783 25 Jul 22 olle 2516     InputStream pdfInStream = pdfFile.getDownloadStream(0);
6783 25 Jul 22 olle 2517     pdfUtil.newPage();
6783 25 Jul 22 olle 2518     // Import PDF document template
6783 25 Jul 22 olle 2519     float xOrig = 0.0f;
6783 25 Jul 22 olle 2520     float yOrig = 0.0f;
6783 25 Jul 22 olle 2521     float scaleX = 1.0f;
6783 25 Jul 22 olle 2522     float scaleY = 1.0f;
6783 25 Jul 22 olle 2523     int pageNo = 1;
6783 25 Jul 22 olle 2524     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
6783 25 Jul 22 olle 2525
6783 25 Jul 22 olle 2526     // Get project name
6783 25 Jul 22 olle 2527     SessionControl sc = dc.getSessionControl();
6783 25 Jul 22 olle 2528     Project activeProject = Project.getById(dc, sc.getActiveProjectId());
6783 25 Jul 22 olle 2529     String projectName = activeProject.getName();
6783 25 Jul 22 olle 2530
6783 25 Jul 22 olle 2531     String text;
6783 25 Jul 22 olle 2532     float textFontSize;
6783 25 Jul 22 olle 2533     int align;
6783 25 Jul 22 olle 2534     float width = 105.8f;
6783 25 Jul 22 olle 2535     float height = 20.0f;
6783 25 Jul 22 olle 2536
6783 25 Jul 22 olle 2537     // Text at upper right for LUCAS sticker forms
6783 25 Jul 22 olle 2538     // Item ID below bar code in upper right section
6783 25 Jul 22 olle 2539     text = itemId;
6802 23 Aug 22 olle 2540     textFontSize = 9.0f;
6783 25 Jul 22 olle 2541     align = Element.ALIGN_LEFT;
6802 23 Aug 22 olle 2542     float xTextUpperRight = 407.0f;
6802 23 Aug 22 olle 2543     float yTextUpperRight = 596.0f;
6783 25 Jul 22 olle 2544     pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
6783 25 Jul 22 olle 2545
6783 25 Jul 22 olle 2546     // Barcode at upper right
6783 25 Jul 22 olle 2547     float xBarcodeUpperRight = xTextUpperRight;
6783 25 Jul 22 olle 2548     float yBarcodeUpperRight = yTextUpperRight + 10.0f;
6783 25 Jul 22 olle 2549     align = Element.ALIGN_LEFT;
6783 25 Jul 22 olle 2550     //pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
6783 25 Jul 22 olle 2551
6783 25 Jul 22 olle 2552     // Barcode and text at upper left
6783 25 Jul 22 olle 2553     text = itemId;
6783 25 Jul 22 olle 2554     float xBarcodeUpperLeft = 52.0f;
6783 25 Jul 22 olle 2555     float yBarcodeUpperLeft = 705.0f;
6783 25 Jul 22 olle 2556     align = Element.ALIGN_LEFT;
6783 25 Jul 22 olle 2557     //pdfUtil.addBarcode(itemId, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
6783 25 Jul 22 olle 2558     textFontSize = 10.0f;
6783 25 Jul 22 olle 2559     float xTextUpperLeft = xBarcodeUpperLeft;
6783 25 Jul 22 olle 2560     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
6783 25 Jul 22 olle 2561     //pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
6783 25 Jul 22 olle 2562
6783 25 Jul 22 olle 2563     // Text at underline
6783 25 Jul 22 olle 2564     float textUnderlineFontSize = 9.0f;
6802 23 Aug 22 olle 2565     float xTextUnderlineLeft = 53.8f;
6783 25 Jul 22 olle 2566     float yTextUnderlineTop = 387.0f;
6802 23 Aug 22 olle 2567     float xDiffUnderline = 216.0f;
6783 25 Jul 22 olle 2568     float yDiffUnderline = 26.0f;
6783 25 Jul 22 olle 2569     float xDiffOffset = 5.0f;
6783 25 Jul 22 olle 2570     // Loop over columns from left
6783 25 Jul 22 olle 2571     for (int i = 0; i < 2; i++)
6783 25 Jul 22 olle 2572     {
6783 25 Jul 22 olle 2573       // Loop over rows from top
6783 25 Jul 22 olle 2574       for (int j = 0; j < 2; j++)
6783 25 Jul 22 olle 2575       {
6783 25 Jul 22 olle 2576         text = itemId + "_" + (2*i + (j + 1)) + ":";
6783 25 Jul 22 olle 2577         float xText = xTextUnderlineLeft + i*xDiffUnderline + xDiffOffset;
6783 25 Jul 22 olle 2578         float yText = yTextUnderlineTop - j*yDiffUnderline;
6783 25 Jul 22 olle 2579         pdfUtil.addText(text, textUnderlineFontSize, align, xText, yText);
6783 25 Jul 22 olle 2580       }
6783 25 Jul 22 olle 2581     }
6783 25 Jul 22 olle 2582     
6783 25 Jul 22 olle 2583     // Item ID on top of labels
6802 23 Aug 22 olle 2584     textFontSize = 7.0f;
6783 25 Jul 22 olle 2585     align = Element.ALIGN_LEFT;
6783 25 Jul 22 olle 2586     float xTextLowerLeft = 52.8f;
6802 23 Aug 22 olle 2587     float yTextLowerLeft = 148.0f;
6783 25 Jul 22 olle 2588     float yTextLowerLeft2 = yTextLowerLeft - 110.4f;
6783 25 Jul 22 olle 2589     float xLowerLeftDiff = 56.0f;
6783 25 Jul 22 olle 2590     // Extra correction term specific for each label
6783 25 Jul 22 olle 2591     float[] xDiffSpecArr = new float[9];
6802 23 Aug 22 olle 2592     xDiffSpecArr[0] = -1.0f;
6802 23 Aug 22 olle 2593     xDiffSpecArr[1] = -1.0f;
6802 23 Aug 22 olle 2594     xDiffSpecArr[2] = -1.0f;
6802 23 Aug 22 olle 2595     xDiffSpecArr[3] = 0.0f;
6783 25 Jul 22 olle 2596     xDiffSpecArr[4] = -3.0f;
6783 25 Jul 22 olle 2597     xDiffSpecArr[5] = -3.0f;
6783 25 Jul 22 olle 2598     xDiffSpecArr[6] = -3.0f;
6783 25 Jul 22 olle 2599     xDiffSpecArr[7] = 0.0f;
6783 25 Jul 22 olle 2600     xDiffSpecArr[8] = 0.0f;
6783 25 Jul 22 olle 2601
6783 25 Jul 22 olle 2602     // Text on top of labels
6783 25 Jul 22 olle 2603     for (int i = 0; i < 4; i++)
6783 25 Jul 22 olle 2604     {
6783 25 Jul 22 olle 2605       text = itemId + "_" + (i + 1);
6802 23 Aug 22 olle 2606       float xTextLowerLeft2 = xTextLowerLeft + i*xLowerLeftDiff + xDiffOffset + xDiffSpecArr[i];
6783 25 Jul 22 olle 2607       pdfUtil.addText(text, textFontSize, align, xTextLowerLeft2, yTextLowerLeft);
6783 25 Jul 22 olle 2608     }
6783 25 Jul 22 olle 2609     //
6783 25 Jul 22 olle 2610     //pdfUtil.drawGrid();
6783 25 Jul 22 olle 2611
6783 25 Jul 22 olle 2612     // Barcode on labels
6783 25 Jul 22 olle 2613     String labelText = itemId;
6783 25 Jul 22 olle 2614     float labelBarcodeWidthNormal = 61.7f;
6783 25 Jul 22 olle 2615     float labelBarcodeHeightNormal = 13.5f;
6783 25 Jul 22 olle 2616     float rotationDegrees = 90.0f;
6783 25 Jul 22 olle 2617     float xLabelBarcodeLeftNormal = 67.8f;
6802 23 Aug 22 olle 2618     float[] xLabelDiffSpecArr = new float[9];
6802 23 Aug 22 olle 2619     xLabelDiffSpecArr[0] = -4.0f;
6802 23 Aug 22 olle 2620     xLabelDiffSpecArr[1] = -4.0f;
6802 23 Aug 22 olle 2621     xLabelDiffSpecArr[2] = -4.0f;
6802 23 Aug 22 olle 2622     xLabelDiffSpecArr[3] = -3.0f;
6802 23 Aug 22 olle 2623     xLabelDiffSpecArr[4] = -1.0f;
6802 23 Aug 22 olle 2624     xLabelDiffSpecArr[5] = -1.0f;
6802 23 Aug 22 olle 2625     xLabelDiffSpecArr[6] = -1.0f;
6802 23 Aug 22 olle 2626     xLabelDiffSpecArr[7] = 0.0f;
6802 23 Aug 22 olle 2627     xLabelDiffSpecArr[8] = 0.0f;
6802 23 Aug 22 olle 2628     float yLabelBarcodeLeft = 67.0f;
6783 25 Jul 22 olle 2629     align = Element.ALIGN_LEFT;
6783 25 Jul 22 olle 2630     for (int i = 0; i < 9; i++)
6783 25 Jul 22 olle 2631     {
6783 25 Jul 22 olle 2632       labelText = itemId;
6783 25 Jul 22 olle 2633       float labelBarcodeWidth = labelBarcodeWidthNormal;
6783 25 Jul 22 olle 2634       float labelBarcodeHeight = labelBarcodeHeightNormal;
6783 25 Jul 22 olle 2635       float xLabelBarcodeLeft = xLabelBarcodeLeftNormal;
6783 25 Jul 22 olle 2636       float yLabelTextCorr = 0.0f;
6783 25 Jul 22 olle 2637       if (i >= 0 && i < 4)
6783 25 Jul 22 olle 2638       {
6783 25 Jul 22 olle 2639         labelText = itemId + "_" + (i + 1);
6783 25 Jul 22 olle 2640         yLabelTextCorr = -5.0f;
6783 25 Jul 22 olle 2641       }
6783 25 Jul 22 olle 2642       float xLabelBarcodeLeft2 = xLabelBarcodeLeft + i*xLowerLeftDiff + xDiffSpecArr[i];
6802 23 Aug 22 olle 2643       float xLabelBarcodeLeft2Normal = xLabelBarcodeLeftNormal + i*xLowerLeftDiff + xLabelDiffSpecArr[i];
6783 25 Jul 22 olle 2644       //pdfUtil.addBarcode(labelText, xLabelBarcodeLeft2, yLabelBarcodeLeft, labelBarcodeWidth, labelBarcodeHeight, align, rotationDegrees);
6783 25 Jul 22 olle 2645       // Barcode label text
6783 25 Jul 22 olle 2646       float xLabelTextLeft = xLabelBarcodeLeft2Normal + 15.0f;
6783 25 Jul 22 olle 2647       float yLabelTextLeft = yLabelBarcodeLeft + yLabelTextCorr;
6783 25 Jul 22 olle 2648       float labelTextFontSize = 10.0f;
6783 25 Jul 22 olle 2649       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelTextLeft, yLabelTextLeft, rotationDegrees);
6783 25 Jul 22 olle 2650     }
6783 25 Jul 22 olle 2651
6783 25 Jul 22 olle 2652     return pdfUtil;
6783 25 Jul 22 olle 2653   }
6783 25 Jul 22 olle 2654
4908 12 Jul 18 olle 2655   private PdfUtil createPatientInfoForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2656     throws IOException
4706 19 Mar 18 olle 2657   {    
4706 19 Mar 18 olle 2658     // Get PDF templates of same type
4908 12 Jul 18 olle 2659     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2660     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2661     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, REF_FORM_TEMPLATE_PATIENT_INFO);
4706 19 Mar 18 olle 2662     for (File pdfFile: pdfFiles)
4706 19 Mar 18 olle 2663     {
4908 12 Jul 18 olle 2664       pdfUtil = createPatientInfoForm(dc, pdfUtil, pdfFile, itemId, formTemplateFileList);
4706 19 Mar 18 olle 2665     }
4706 19 Mar 18 olle 2666
4706 19 Mar 18 olle 2667     return pdfUtil;
4706 19 Mar 18 olle 2668   }
4706 19 Mar 18 olle 2669
4908 12 Jul 18 olle 2670   private PdfUtil createPatientInfoForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2671     throws IOException
4706 19 Mar 18 olle 2672   {    
4706 19 Mar 18 olle 2673     if (pdfFile == null)
4706 19 Mar 18 olle 2674     {
4706 19 Mar 18 olle 2675       return pdfUtil;
4706 19 Mar 18 olle 2676     }
4706 19 Mar 18 olle 2677     if (!formTemplateFileList.contains(pdfFile.getName()))
4706 19 Mar 18 olle 2678     {
4706 19 Mar 18 olle 2679       formTemplateFileList.add(pdfFile.getName());
4706 19 Mar 18 olle 2680     }
4706 19 Mar 18 olle 2681     InputStream pdfInStream;
4706 19 Mar 18 olle 2682     int numPages = 0;
4706 19 Mar 18 olle 2683     try
4706 19 Mar 18 olle 2684     {
4706 19 Mar 18 olle 2685       for (int pageNo = 1; pageNo < 100000; pageNo++)
4706 19 Mar 18 olle 2686       {
4706 19 Mar 18 olle 2687         pdfInStream = pdfFile.getDownloadStream(0);
4706 19 Mar 18 olle 2688         pdfUtil.newPage();
4706 19 Mar 18 olle 2689         // Import PDF document template
4706 19 Mar 18 olle 2690         float xOrig = 0.0f;
4706 19 Mar 18 olle 2691         float yOrig = 0.0f;
4706 19 Mar 18 olle 2692         float scaleX = 1.0f;
4706 19 Mar 18 olle 2693         float scaleY = 1.0f;
4706 19 Mar 18 olle 2694         pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4706 19 Mar 18 olle 2695         // Update page counter
4706 19 Mar 18 olle 2696         numPages++;
4908 12 Jul 18 olle 2697         // Item ID in upper right page header
4908 12 Jul 18 olle 2698         String text = itemId;
4706 19 Mar 18 olle 2699         float textFontSize = 10.0f;
4706 19 Mar 18 olle 2700         int align = Element.ALIGN_LEFT;
4951 30 Aug 18 olle 2701         // Referral number below underline after label "Initialer:"
4951 30 Aug 18 olle 2702         //float xTextUpperRight = 465.0f;
4951 30 Aug 18 olle 2703         // Referral number below label "Initialer:"
4951 30 Aug 18 olle 2704         float xTextUpperRight = 419.0f;
4706 19 Mar 18 olle 2705         float yTextUpperRight = 769.2f;
4706 19 Mar 18 olle 2706         pdfUtil.addText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
4706 19 Mar 18 olle 2707       }
4706 19 Mar 18 olle 2708     }
4706 19 Mar 18 olle 2709     catch (Exception e)
4706 19 Mar 18 olle 2710     {
4706 19 Mar 18 olle 2711 //System.out.println(new Date() + " createPatientInfoForm(): Exception e: " + e);
4706 19 Mar 18 olle 2712     }
4706 19 Mar 18 olle 2713     // Add blank extra page, if the number of pages is odd
4706 19 Mar 18 olle 2714     if (numPages % 2 > 0)
4706 19 Mar 18 olle 2715     {
4706 19 Mar 18 olle 2716       pdfUtil.newBlankPage();
4706 19 Mar 18 olle 2717     }
4706 19 Mar 18 olle 2718
4706 19 Mar 18 olle 2719     return pdfUtil;
4706 19 Mar 18 olle 2720   }
4706 19 Mar 18 olle 2721
4908 12 Jul 18 olle 2722   private PdfUtil createConsentReportForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2723     throws IOException
4706 19 Mar 18 olle 2724   {    
4706 19 Mar 18 olle 2725     // Get PDF templates of same type
4908 12 Jul 18 olle 2726     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2727     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, REF_FORM_TEMPLATE_CONSENT_REPORT);
4706 19 Mar 18 olle 2728     for (File pdfFile: pdfFiles)
4706 19 Mar 18 olle 2729     {
4908 12 Jul 18 olle 2730       pdfUtil = createBarcodeNonStickerForm(dc, pdfUtil, itemId, formTemplateFileList);
4706 19 Mar 18 olle 2731     }
4706 19 Mar 18 olle 2732
4706 19 Mar 18 olle 2733     return pdfUtil;
4706 19 Mar 18 olle 2734   }
4706 19 Mar 18 olle 2735
4908 12 Jul 18 olle 2736   private PdfUtil createConsentReportForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2737     throws IOException
4706 19 Mar 18 olle 2738   {    
4706 19 Mar 18 olle 2739     if (pdfFile == null)
4706 19 Mar 18 olle 2740     {
4706 19 Mar 18 olle 2741       return pdfUtil;
4706 19 Mar 18 olle 2742     }
4706 19 Mar 18 olle 2743     if (!formTemplateFileList.contains(pdfFile.getName()))
4706 19 Mar 18 olle 2744     {
4706 19 Mar 18 olle 2745       formTemplateFileList.add(pdfFile.getName());
4706 19 Mar 18 olle 2746     }
4706 19 Mar 18 olle 2747     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4706 19 Mar 18 olle 2748     pdfUtil.newPage();
4706 19 Mar 18 olle 2749     // Import PDF document template
4706 19 Mar 18 olle 2750     float xOrig = 29.6f;
4706 19 Mar 18 olle 2751     float yOrig = 17.4f;
4706 19 Mar 18 olle 2752     float scaleX = 0.995f;
4706 19 Mar 18 olle 2753     float scaleY = 0.997f;
4706 19 Mar 18 olle 2754     int pageNo = 1;
4706 19 Mar 18 olle 2755     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4908 12 Jul 18 olle 2756     // Item ID below bar code in upper right section
4908 12 Jul 18 olle 2757     String text = itemId;
4706 19 Mar 18 olle 2758     float textFontSize = 12.1f;
4706 19 Mar 18 olle 2759     int align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 2760     float xTextUpperRight = 430.8f;
4706 19 Mar 18 olle 2761     float yTextUpperRight = 698.2f;
4706 19 Mar 18 olle 2762     pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
4908 12 Jul 18 olle 2763     // Item ID in lower section
4724 29 Mar 18 olle 2764 /*
4908 12 Jul 18 olle 2765     text = itemIdDigits;
4706 19 Mar 18 olle 2766     textFontSize = 10.7f;
4706 19 Mar 18 olle 2767     align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 2768     float xTextLowerLeft = 186.0f;
4706 19 Mar 18 olle 2769     float yTextLowerLeft = 153.4f;
4706 19 Mar 18 olle 2770     pdfUtil.addBoldText(text, textFontSize, align, xTextLowerLeft, yTextLowerLeft);
4724 29 Mar 18 olle 2771 */
4706 19 Mar 18 olle 2772     //
4706 19 Mar 18 olle 2773     //pdfUtil.drawGrid();
4706 19 Mar 18 olle 2774
4706 19 Mar 18 olle 2775     // Barcode at upper right
4706 19 Mar 18 olle 2776     float xBarcodeUpperRight = xTextUpperRight + 0.7f;
4706 19 Mar 18 olle 2777     float yBarcodeUpperRight = yTextUpperRight + 12.0f;
4706 19 Mar 18 olle 2778     float width = 94.0f;
4706 19 Mar 18 olle 2779     float height = 20.0f;
4706 19 Mar 18 olle 2780     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 2781     pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
4706 19 Mar 18 olle 2782
4706 19 Mar 18 olle 2783     // Barcode at upper left
4706 19 Mar 18 olle 2784     float xBarcodeUpperLeft = 65.0f;
4724 29 Mar 18 olle 2785     //float yBarcodeUpperLeft = yBarcodeUpperRight - 4.0f;
4724 29 Mar 18 olle 2786     float yBarcodeUpperLeft = yBarcodeUpperRight + 35.0f;
4706 19 Mar 18 olle 2787     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 2788     pdfUtil.addBarcode(itemId, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
4706 19 Mar 18 olle 2789     textFontSize = 10.0f;
4706 19 Mar 18 olle 2790     float xTextUpperLeft = 92.2f;
4706 19 Mar 18 olle 2791     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4706 19 Mar 18 olle 2792     pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
4706 19 Mar 18 olle 2793
4706 19 Mar 18 olle 2794     // Add blank extra page
4706 19 Mar 18 olle 2795     pdfUtil.newBlankPage();
4706 19 Mar 18 olle 2796
4706 19 Mar 18 olle 2797     return pdfUtil;
4706 19 Mar 18 olle 2798   }
4706 19 Mar 18 olle 2799
4908 12 Jul 18 olle 2800   private PdfUtil createPadReferralForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2801     throws IOException
4706 19 Mar 18 olle 2802   {    
4724 29 Mar 18 olle 2803     // Get PDF templates of same type
4908 12 Jul 18 olle 2804     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2805     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2806     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, REF_FORM_TEMPLATE_PAD_REFERRAL);
4724 29 Mar 18 olle 2807     for (File pdfFile: pdfFiles)
4724 29 Mar 18 olle 2808     {
4724 29 Mar 18 olle 2809       // Note: Uses same bar code layout as consent report form
4908 12 Jul 18 olle 2810       //pdfUtil = createConsentReportForm(dc, pdfUtil, pdfFile, itemId, formTemplateFileList);
4908 12 Jul 18 olle 2811       pdfUtil = createBarcodeNonStickerForm(dc, pdfUtil, pdfFile, itemId, formTemplateFileList);
4724 29 Mar 18 olle 2812     }
4724 29 Mar 18 olle 2813
4724 29 Mar 18 olle 2814     return pdfUtil;
4706 19 Mar 18 olle 2815   }
4706 19 Mar 18 olle 2816
4908 12 Jul 18 olle 2817   private PdfUtil createBiopsyClinChemForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2818     throws IOException
4706 19 Mar 18 olle 2819   {    
4908 12 Jul 18 olle 2820     //return createClinChemForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY, formTemplateFileList);
4908 12 Jul 18 olle 2821     return createBarcodeStickerForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY, formTemplateFileList);
4706 19 Mar 18 olle 2822   }
4706 19 Mar 18 olle 2823
4908 12 Jul 18 olle 2824   private PdfUtil createWholeBloodForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4767 17 Apr 18 olle 2825     throws IOException
4767 17 Apr 18 olle 2826   {    
4908 12 Jul 18 olle 2827     return createBarcodeStickerForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_WHOLE_BLOOD, formTemplateFileList);
4767 17 Apr 18 olle 2828   }
4767 17 Apr 18 olle 2829
4908 12 Jul 18 olle 2830   private PdfUtil createFollowUpForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4767 17 Apr 18 olle 2831     throws IOException
4767 17 Apr 18 olle 2832   {    
4908 12 Jul 18 olle 2833     return createBarcodeStickerForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_FOLLOW_UP, formTemplateFileList);
4767 17 Apr 18 olle 2834   }
4767 17 Apr 18 olle 2835
5700 05 Nov 19 olle 2836   private PdfUtil createFollowUpTissueForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
5700 05 Nov 19 olle 2837     throws IOException
5700 05 Nov 19 olle 2838   {    
5700 05 Nov 19 olle 2839     return createBarcodeStickerForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_FOLLOW_UP_TISSUE, formTemplateFileList);
5700 05 Nov 19 olle 2840   }
5700 05 Nov 19 olle 2841
4908 12 Jul 18 olle 2842   private PdfUtil createClinChemForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2843     throws IOException
4706 19 Mar 18 olle 2844   {    
4908 12 Jul 18 olle 2845     //return createClinChemForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_CHEM, formTemplateFileList);
4908 12 Jul 18 olle 2846     return createBarcodeStickerForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_CHEM, formTemplateFileList);
4724 29 Mar 18 olle 2847   }
4724 29 Mar 18 olle 2848
5702 06 Nov 19 olle 2849   private PdfUtil createNordicTripStickerForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
5702 06 Nov 19 olle 2850     throws IOException
5702 06 Nov 19 olle 2851   {    
5702 06 Nov 19 olle 2852     return createBarcodeStickerForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_NORDIC_TRIP_STICKER_FORM, formTemplateFileList);
5702 06 Nov 19 olle 2853   }
5702 06 Nov 19 olle 2854
6783 25 Jul 22 olle 2855   private PdfUtil createLucasStickerForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
6783 25 Jul 22 olle 2856     throws IOException
6783 25 Jul 22 olle 2857   {    
6783 25 Jul 22 olle 2858     return createLucasStickerForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_LUCAS_STICKER_FORM, formTemplateFileList);
6783 25 Jul 22 olle 2859   }
6783 25 Jul 22 olle 2860
4908 12 Jul 18 olle 2861   private PdfUtil createClinChemForm(DbControl dc, PdfUtil pdfUtil, String itemId, String formTemplateName, List<String> formTemplateFileList)
4724 29 Mar 18 olle 2862     throws IOException
4724 29 Mar 18 olle 2863   {    
4706 19 Mar 18 olle 2864     // Get PDF templates of same type
4908 12 Jul 18 olle 2865     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2866     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 2867     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, formTemplateName);
4706 19 Mar 18 olle 2868     for (File pdfFile: pdfFiles)
4706 19 Mar 18 olle 2869     {
4908 12 Jul 18 olle 2870       pdfUtil = createClinChemForm(dc, pdfUtil, pdfFile, itemId, formTemplateName, formTemplateFileList);
4706 19 Mar 18 olle 2871     }
4706 19 Mar 18 olle 2872
4706 19 Mar 18 olle 2873     return pdfUtil;
4706 19 Mar 18 olle 2874   }
4706 19 Mar 18 olle 2875
4908 12 Jul 18 olle 2876   private PdfUtil createClinChemForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, String formTemplateName, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2877     throws IOException
4706 19 Mar 18 olle 2878   {    
4706 19 Mar 18 olle 2879     if (pdfFile == null)
4706 19 Mar 18 olle 2880     {
4706 19 Mar 18 olle 2881       return pdfUtil;
4706 19 Mar 18 olle 2882     }
4706 19 Mar 18 olle 2883     if (!formTemplateFileList.contains(pdfFile.getName()))
4706 19 Mar 18 olle 2884     {
4706 19 Mar 18 olle 2885       formTemplateFileList.add(pdfFile.getName());
4706 19 Mar 18 olle 2886     }
4706 19 Mar 18 olle 2887     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4706 19 Mar 18 olle 2888     pdfUtil.newPage();
4706 19 Mar 18 olle 2889     // Import PDF document template
4706 19 Mar 18 olle 2890     float xOrig = 28.1f;
4706 19 Mar 18 olle 2891     float yOrig = 15.9f;
4706 19 Mar 18 olle 2892     float scaleX = 0.998f;
4706 19 Mar 18 olle 2893     float scaleY = 0.997f;
4706 19 Mar 18 olle 2894     int pageNo = 1;
4706 19 Mar 18 olle 2895     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4706 19 Mar 18 olle 2896
4908 12 Jul 18 olle 2897     // Item ID below bar code in upper right section
4908 12 Jul 18 olle 2898     String text = itemId;
4706 19 Mar 18 olle 2899     float textFontSize = 11.9f;
4706 19 Mar 18 olle 2900     int align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 2901     float xTextUpperRight = 448.0f;
4706 19 Mar 18 olle 2902     float yTextUpperRight = 651.0f;
4706 19 Mar 18 olle 2903     if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY))
4706 19 Mar 18 olle 2904     {
4706 19 Mar 18 olle 2905       xTextUpperRight = 443.0f;
4706 19 Mar 18 olle 2906     }
4706 19 Mar 18 olle 2907     pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
4706 19 Mar 18 olle 2908
4706 19 Mar 18 olle 2909     // Barcode at upper right
4706 19 Mar 18 olle 2910     float xBarcodeUpperRight = xTextUpperRight + 0.7f;
4706 19 Mar 18 olle 2911     float yBarcodeUpperRight = yTextUpperRight + 12.0f;
4706 19 Mar 18 olle 2912     float width = 94.1f;
4706 19 Mar 18 olle 2913     float height = 20.0f;
4706 19 Mar 18 olle 2914     if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY))
4706 19 Mar 18 olle 2915     {
4706 19 Mar 18 olle 2916       width = 105.8f;
4706 19 Mar 18 olle 2917       xBarcodeUpperRight = xTextUpperRight - 0.2f;
4706 19 Mar 18 olle 2918     }
4706 19 Mar 18 olle 2919     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 2920     pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
4706 19 Mar 18 olle 2921
4706 19 Mar 18 olle 2922     // Barcode and text at upper left
4908 12 Jul 18 olle 2923     text = itemId;
4706 19 Mar 18 olle 2924     float xBarcodeUpperLeft = 65.2f;
4724 29 Mar 18 olle 2925     //float yBarcodeUpperLeft = 707.0f;
4724 29 Mar 18 olle 2926     float yBarcodeUpperLeft = 742.0f;
4706 19 Mar 18 olle 2927     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 2928     pdfUtil.addBarcode(itemId, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
4706 19 Mar 18 olle 2929     textFontSize = 10.0f;
4706 19 Mar 18 olle 2930     float xTextUpperLeft = 92.5f;
4706 19 Mar 18 olle 2931     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4706 19 Mar 18 olle 2932     if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY))
4706 19 Mar 18 olle 2933     {
4706 19 Mar 18 olle 2934       xTextUpperLeft = 95.5f;
4706 19 Mar 18 olle 2935     }
4706 19 Mar 18 olle 2936     pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
4706 19 Mar 18 olle 2937
4908 12 Jul 18 olle 2938     // Item ID on both short sides of labels
4706 19 Mar 18 olle 2939     textFontSize = 7.0f;
4706 19 Mar 18 olle 2940     align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 2941     float xTextLowerLeft = 56.8f;
4706 19 Mar 18 olle 2942     float yTextLowerLeft = 146.9f;
4706 19 Mar 18 olle 2943     float yTextLowerLeft2 = yTextLowerLeft - 117.4f;
4706 19 Mar 18 olle 2944     float xLowerLeftDiff = 55.6f;
4706 19 Mar 18 olle 2945     // Extra correction term specific for each label
4706 19 Mar 18 olle 2946     float[] xDiffSpecArr = new float[9];
4706 19 Mar 18 olle 2947     xDiffSpecArr[0] = 0.0f;
4706 19 Mar 18 olle 2948     xDiffSpecArr[1] = 0.0f;
4706 19 Mar 18 olle 2949     xDiffSpecArr[2] = 0.0f;
4706 19 Mar 18 olle 2950     xDiffSpecArr[3] = 0.5f;
4706 19 Mar 18 olle 2951     xDiffSpecArr[4] = 1.5f;
4706 19 Mar 18 olle 2952     xDiffSpecArr[5] = 3.5f;
4706 19 Mar 18 olle 2953     xDiffSpecArr[6] = 4.0f;
4706 19 Mar 18 olle 2954     xDiffSpecArr[7] = 3.5f;
4706 19 Mar 18 olle 2955     xDiffSpecArr[8] = 0.5f;
4706 19 Mar 18 olle 2956     if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_CLIN_CHEM_BIOPSY))
4706 19 Mar 18 olle 2957     {
4706 19 Mar 18 olle 2958       xDiffSpecArr[0] = -3.5f;
4706 19 Mar 18 olle 2959       xDiffSpecArr[8] = 4.0f;
4706 19 Mar 18 olle 2960     }
4706 19 Mar 18 olle 2961
4706 19 Mar 18 olle 2962     for (int i = 0; i < 7; i++)
4706 19 Mar 18 olle 2963     {
4706 19 Mar 18 olle 2964       float xTextLowerLeft2 = xTextLowerLeft + i*xLowerLeftDiff + xDiffSpecArr[i];
4706 19 Mar 18 olle 2965       pdfUtil.addBoldText(text, textFontSize, align, xTextLowerLeft2, yTextLowerLeft);
4706 19 Mar 18 olle 2966       pdfUtil.addBoldText(text, textFontSize, align, xTextLowerLeft2, yTextLowerLeft2);
4706 19 Mar 18 olle 2967     }
4706 19 Mar 18 olle 2968     //
4706 19 Mar 18 olle 2969     //pdfUtil.drawGrid();
4706 19 Mar 18 olle 2970
4706 19 Mar 18 olle 2971     // Barcode on labels
4706 19 Mar 18 olle 2972     float labelBarcodeWidth = 61.7f;
4706 19 Mar 18 olle 2973     float labelBarcodeHeight = 13.5f;
4706 19 Mar 18 olle 2974     float rotationDegrees = 90.0f;
4706 19 Mar 18 olle 2975     float xLabelBarcodeLeft = 67.8f;
4706 19 Mar 18 olle 2976     float yLabelBarcodeLeft = 43.5f;
4706 19 Mar 18 olle 2977     align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 2978     for (int i = 0; i < 9; i++)
4706 19 Mar 18 olle 2979     {
4706 19 Mar 18 olle 2980       float xLabelBarcodeLeft2 = xLabelBarcodeLeft + i*xLowerLeftDiff + xDiffSpecArr[i];
4908 12 Jul 18 olle 2981       pdfUtil.addBarcode(itemId, xLabelBarcodeLeft2, yLabelBarcodeLeft, labelBarcodeWidth, labelBarcodeHeight, align, rotationDegrees);
4706 19 Mar 18 olle 2982       // Barcode label text
4908 12 Jul 18 olle 2983       String labelText = itemId;
4706 19 Mar 18 olle 2984       float xLabelTextLeft = xLabelBarcodeLeft2 + 24.0f;
4706 19 Mar 18 olle 2985       float yLabelTextLeft = yLabelBarcodeLeft - 2.0f;
4706 19 Mar 18 olle 2986       float labelTextFontSize = 10.0f;
4706 19 Mar 18 olle 2987       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelTextLeft, yLabelTextLeft, rotationDegrees);
4706 19 Mar 18 olle 2988     }
4706 19 Mar 18 olle 2989
4706 19 Mar 18 olle 2990     return pdfUtil;
4706 19 Mar 18 olle 2991   }
4706 19 Mar 18 olle 2992
4908 12 Jul 18 olle 2993   private PdfUtil createMiddleNeedleBiopsyForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 2994     throws IOException
4706 19 Mar 18 olle 2995   {    
4908 12 Jul 18 olle 2996     return createClinPathologyForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY, formTemplateFileList);
4706 19 Mar 18 olle 2997   }
4706 19 Mar 18 olle 2998
4908 12 Jul 18 olle 2999   private PdfUtil createClinPathologyForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3000     throws IOException
4706 19 Mar 18 olle 3001   {    
4908 12 Jul 18 olle 3002     return createClinPathologyForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_PATHOLOGY, formTemplateFileList);
4706 19 Mar 18 olle 3003   }
4706 19 Mar 18 olle 3004
4908 12 Jul 18 olle 3005   private PdfUtil createClinPathologyForm(DbControl dc, PdfUtil pdfUtil, String itemId, String formTemplateName, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3006     throws IOException
4706 19 Mar 18 olle 3007   {    
4706 19 Mar 18 olle 3008     // Get PDF templates of same type
4908 12 Jul 18 olle 3009     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3010     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3011     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, formTemplateName);
4706 19 Mar 18 olle 3012     for (File pdfFile: pdfFiles)
4706 19 Mar 18 olle 3013     {
4908 12 Jul 18 olle 3014       pdfUtil = createClinPathologyForm(dc, pdfUtil, pdfFile, itemId, formTemplateName, formTemplateFileList);
4706 19 Mar 18 olle 3015     }
4706 19 Mar 18 olle 3016
4706 19 Mar 18 olle 3017     return pdfUtil;
4706 19 Mar 18 olle 3018   }
4706 19 Mar 18 olle 3019
4908 12 Jul 18 olle 3020   private PdfUtil createClinPathologyForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, String formTemplateName, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3021     throws IOException
4706 19 Mar 18 olle 3022   {    
4706 19 Mar 18 olle 3023     if (pdfFile == null)
4706 19 Mar 18 olle 3024     {
4706 19 Mar 18 olle 3025       return pdfUtil;
4706 19 Mar 18 olle 3026     }
4706 19 Mar 18 olle 3027     if (!formTemplateFileList.contains(pdfFile.getName()))
4706 19 Mar 18 olle 3028     {
4706 19 Mar 18 olle 3029       formTemplateFileList.add(pdfFile.getName());
4706 19 Mar 18 olle 3030     }
4706 19 Mar 18 olle 3031     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4706 19 Mar 18 olle 3032     pdfUtil.newPage();
4706 19 Mar 18 olle 3033     // Import PDF document template
4706 19 Mar 18 olle 3034     float xOrig = 28.1f;
4706 19 Mar 18 olle 3035     float yOrig = 15.9f;
4706 19 Mar 18 olle 3036     float scaleX = 0.998f;
4706 19 Mar 18 olle 3037     float scaleY = 0.997f;
4706 19 Mar 18 olle 3038     int pageNo = 1;
4706 19 Mar 18 olle 3039     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4706 19 Mar 18 olle 3040
4908 12 Jul 18 olle 3041     // Item ID below bar code in upper right section
4908 12 Jul 18 olle 3042     String text = itemId;
4706 19 Mar 18 olle 3043     float textFontSize = 11.9f;
4706 19 Mar 18 olle 3044     int align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 3045     float xTextUpperRight = 448.0f;
4706 19 Mar 18 olle 3046     float yTextUpperRight = 651.0f;
4706 19 Mar 18 olle 3047     if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY))
4706 19 Mar 18 olle 3048     {
4706 19 Mar 18 olle 3049       xTextUpperRight = 443.0f;
4706 19 Mar 18 olle 3050     }
4706 19 Mar 18 olle 3051     pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
4706 19 Mar 18 olle 3052
4706 19 Mar 18 olle 3053     // Barcode at upper right
4706 19 Mar 18 olle 3054     float xBarcodeUpperRight = xTextUpperRight + 0.7f;
4706 19 Mar 18 olle 3055     float yBarcodeUpperRight = yTextUpperRight + 12.0f;
4706 19 Mar 18 olle 3056     float width = 94.1f;
4706 19 Mar 18 olle 3057     float height = 20.0f;
4706 19 Mar 18 olle 3058     if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY))
4706 19 Mar 18 olle 3059     {
4706 19 Mar 18 olle 3060       width = 105.8f;
4706 19 Mar 18 olle 3061       xBarcodeUpperRight = xTextUpperRight - 0.2f;
4706 19 Mar 18 olle 3062     }
4706 19 Mar 18 olle 3063     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 3064     pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
4706 19 Mar 18 olle 3065
4706 19 Mar 18 olle 3066     // Barcode and text at upper left
4908 12 Jul 18 olle 3067     text = itemId;
4706 19 Mar 18 olle 3068     float xBarcodeUpperLeft = 65.2f;
4706 19 Mar 18 olle 3069     float yBarcodeUpperLeft = 707.0f;
4706 19 Mar 18 olle 3070     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 3071     pdfUtil.addBarcode(itemId, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
4706 19 Mar 18 olle 3072     textFontSize = 10.0f;
4706 19 Mar 18 olle 3073     float xTextUpperLeft = 92.5f;
4706 19 Mar 18 olle 3074     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4706 19 Mar 18 olle 3075     if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY))
4706 19 Mar 18 olle 3076     {
4706 19 Mar 18 olle 3077       xTextUpperLeft = 95.5f;
4706 19 Mar 18 olle 3078     }
4706 19 Mar 18 olle 3079     pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
4706 19 Mar 18 olle 3080
4706 19 Mar 18 olle 3081     // Barcode on labels (short side)
4706 19 Mar 18 olle 3082     float labelBarcodeWidth = 44.5f;
4706 19 Mar 18 olle 3083     float labelBarcodeHeight = 14.4f;
4706 19 Mar 18 olle 3084     float rotationDegrees = 0.0f;
4706 19 Mar 18 olle 3085     float xLabelBarcodeLeft = 48.7f;
4706 19 Mar 18 olle 3086     float yLabelBarcodeLeft = 145.0f;
4706 19 Mar 18 olle 3087     float yLabelBarcodeLeft2 = yLabelBarcodeLeft - 120.7f;
4706 19 Mar 18 olle 3088     float xLowerLeftDiff = 56.6f;
4706 19 Mar 18 olle 3089     align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 3090     for (int i = 0; i < 6; i++)
4706 19 Mar 18 olle 3091     {
4706 19 Mar 18 olle 3092       float xLabelBarcodeLeft2 = xLabelBarcodeLeft + i*xLowerLeftDiff;
4908 12 Jul 18 olle 3093       pdfUtil.addBarcode(itemId, xLabelBarcodeLeft2, yLabelBarcodeLeft, labelBarcodeWidth, labelBarcodeHeight, align, rotationDegrees);
4908 12 Jul 18 olle 3094       pdfUtil.addBarcode(itemId, xLabelBarcodeLeft2, yLabelBarcodeLeft2, labelBarcodeWidth, labelBarcodeHeight, align, rotationDegrees);
4706 19 Mar 18 olle 3095       // Barcode label text
4908 12 Jul 18 olle 3096       String labelText = itemId;
4706 19 Mar 18 olle 3097       float xLabelTextLeft = xLabelBarcodeLeft2 + 7.4f;
4706 19 Mar 18 olle 3098       float yLabelTextLeft = yLabelBarcodeLeft - 7.0f;
4706 19 Mar 18 olle 3099       float yLabelTextBottom = yLabelBarcodeLeft2 + labelBarcodeHeight + 2.0f;
4706 19 Mar 18 olle 3100       float labelTextFontSize = 6.0f;
4706 19 Mar 18 olle 3101       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelTextLeft, yLabelTextLeft, rotationDegrees);
4706 19 Mar 18 olle 3102       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelTextLeft, yLabelTextBottom, rotationDegrees);
4706 19 Mar 18 olle 3103     }
4706 19 Mar 18 olle 3104
4908 12 Jul 18 olle 3105     // Item ID on long side of first six labels
4908 12 Jul 18 olle 3106     String labelText = itemId;
4706 19 Mar 18 olle 3107     float labelTextFontSize = 10.0f;
4706 19 Mar 18 olle 3108     rotationDegrees = 90.0f;
4706 19 Mar 18 olle 3109     float xLabelTextLeft = 72.5f;
4706 19 Mar 18 olle 3110     float yLabelText = 48.8f;
4706 19 Mar 18 olle 3111     for (int i = 0; i < 6; i++)
4706 19 Mar 18 olle 3112     {
4706 19 Mar 18 olle 3113       float xLabelText = xLabelTextLeft + i*xLowerLeftDiff;
4706 19 Mar 18 olle 3114       if (i > 0 && i < 5)
4706 19 Mar 18 olle 3115       {
4706 19 Mar 18 olle 3116         xLabelText += 9.0f;
4706 19 Mar 18 olle 3117       }
4706 19 Mar 18 olle 3118       else if (i == 5)
4706 19 Mar 18 olle 3119       {
4706 19 Mar 18 olle 3120         xLabelText += 7.0f;          
4706 19 Mar 18 olle 3121       }
4706 19 Mar 18 olle 3122       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelText, yLabelText, rotationDegrees);
4706 19 Mar 18 olle 3123     }
4706 19 Mar 18 olle 3124
4706 19 Mar 18 olle 3125     // Barcode on labels (long side)
4706 19 Mar 18 olle 3126     labelBarcodeWidth = 61.2f;
4706 19 Mar 18 olle 3127     labelBarcodeHeight = 21.4f;
4706 19 Mar 18 olle 3128     rotationDegrees = 90.0f;
4706 19 Mar 18 olle 3129     xLabelBarcodeLeft = 58.9f;
4706 19 Mar 18 olle 3130     yLabelBarcodeLeft = 52.7f;
4706 19 Mar 18 olle 3131     align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 3132     for (int i = 6; i < 9; i++)
4706 19 Mar 18 olle 3133     {
4706 19 Mar 18 olle 3134       if (formTemplateName != null && formTemplateName.equals(REF_FORM_TEMPLATE_MIDDLE_NEEDLE_BIOPSY))
4706 19 Mar 18 olle 3135       {
4706 19 Mar 18 olle 3136         if (i == 8)
4706 19 Mar 18 olle 3137         {
4706 19 Mar 18 olle 3138           xLabelBarcodeLeft -= 4.0f;
4706 19 Mar 18 olle 3139         }
4706 19 Mar 18 olle 3140       }
4706 19 Mar 18 olle 3141       float xLabelBarcodeLeft2 = xLabelBarcodeLeft + i*xLowerLeftDiff;
4908 12 Jul 18 olle 3142       pdfUtil.addBarcode(itemId, xLabelBarcodeLeft2, yLabelBarcodeLeft, labelBarcodeWidth, labelBarcodeHeight, align, rotationDegrees);
4706 19 Mar 18 olle 3143       // Barcode label text
4908 12 Jul 18 olle 3144       labelText = itemId;
4706 19 Mar 18 olle 3145       xLabelTextLeft = xLabelBarcodeLeft2 + 29.8f;
4706 19 Mar 18 olle 3146       float yLabelTextLeft = yLabelBarcodeLeft - 7.9f;
4706 19 Mar 18 olle 3147       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelTextLeft, yLabelTextLeft, rotationDegrees);
4706 19 Mar 18 olle 3148     }
4706 19 Mar 18 olle 3149
4706 19 Mar 18 olle 3150     return pdfUtil;
4706 19 Mar 18 olle 3151   }
4706 19 Mar 18 olle 3152
4908 12 Jul 18 olle 3153   private PdfUtil createClinChemBloodForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3154     throws IOException
4706 19 Mar 18 olle 3155   {    
4908 12 Jul 18 olle 3156     return createClinChemBloodForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD, formTemplateFileList);
4706 19 Mar 18 olle 3157   }
4706 19 Mar 18 olle 3158
4908 12 Jul 18 olle 3159   private PdfUtil createClinChemBloodLundCopyForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3160     throws IOException
4706 19 Mar 18 olle 3161   {    
4908 12 Jul 18 olle 3162     return createClinChemBloodForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_CHEM_BLOOD_LUNDCOPY, formTemplateFileList);
4706 19 Mar 18 olle 3163   }
4706 19 Mar 18 olle 3164
4908 12 Jul 18 olle 3165   private PdfUtil createClinChemBloodForm(DbControl dc, PdfUtil pdfUtil, String itemId, String formTemplateName, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3166     throws IOException
4706 19 Mar 18 olle 3167   {    
4706 19 Mar 18 olle 3168     // Get PDF templates of same type
4908 12 Jul 18 olle 3169     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3170     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3171     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, formTemplateName);
4706 19 Mar 18 olle 3172     for (File pdfFile: pdfFiles)
4706 19 Mar 18 olle 3173     {
4908 12 Jul 18 olle 3174       pdfUtil = createClinChemBloodForm(dc, pdfUtil, pdfFile, itemId, formTemplateFileList);
4706 19 Mar 18 olle 3175     }
4706 19 Mar 18 olle 3176
4706 19 Mar 18 olle 3177     return pdfUtil;
4706 19 Mar 18 olle 3178   }
4706 19 Mar 18 olle 3179
4908 12 Jul 18 olle 3180   private PdfUtil createClinChemBloodForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3181     throws IOException
4706 19 Mar 18 olle 3182   {    
4706 19 Mar 18 olle 3183     if (pdfFile == null)
4706 19 Mar 18 olle 3184     {
4706 19 Mar 18 olle 3185       return pdfUtil;
4706 19 Mar 18 olle 3186     }
4706 19 Mar 18 olle 3187     if (!formTemplateFileList.contains(pdfFile.getName()))
4706 19 Mar 18 olle 3188     {
4706 19 Mar 18 olle 3189       formTemplateFileList.add(pdfFile.getName());
4706 19 Mar 18 olle 3190     }
4706 19 Mar 18 olle 3191     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4706 19 Mar 18 olle 3192     pdfUtil.newPage();
4706 19 Mar 18 olle 3193     // Import PDF document template
4706 19 Mar 18 olle 3194     float xOrig = 29.4f;
4706 19 Mar 18 olle 3195     float yOrig = 14.2f;
4706 19 Mar 18 olle 3196     float scaleX = 1.000f;
4706 19 Mar 18 olle 3197     float scaleY = 1.000f;
4706 19 Mar 18 olle 3198     int pageNo = 1;
4706 19 Mar 18 olle 3199     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4706 19 Mar 18 olle 3200
4908 12 Jul 18 olle 3201     // Item ID below bar code in upper right section
4908 12 Jul 18 olle 3202     String text = itemId;
4706 19 Mar 18 olle 3203     float textFontSize = 12.5f;
4706 19 Mar 18 olle 3204     int align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 3205     float xTextUpperRight = 435.0f;
4706 19 Mar 18 olle 3206     float yTextUpperRight = 657.0f;
4706 19 Mar 18 olle 3207     pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
4706 19 Mar 18 olle 3208     //
4706 19 Mar 18 olle 3209     //pdfUtil.drawGrid();
4706 19 Mar 18 olle 3210
4706 19 Mar 18 olle 3211     // Barcode at upper right
4706 19 Mar 18 olle 3212     float xBarcodeUpperRight = xTextUpperRight + 0.9f;
4706 19 Mar 18 olle 3213     float yBarcodeUpperRight = yTextUpperRight + 12.0f;
4706 19 Mar 18 olle 3214     float width = 96.1f;
4706 19 Mar 18 olle 3215     float height = 20.0f;
4706 19 Mar 18 olle 3216     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 3217     pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
4706 19 Mar 18 olle 3218
4706 19 Mar 18 olle 3219     // Barcode at upper left
4706 19 Mar 18 olle 3220     float xBarcodeUpperLeft = 64.9f;
4706 19 Mar 18 olle 3221     float yBarcodeUpperLeft = 684.3f;
4706 19 Mar 18 olle 3222     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 3223     pdfUtil.addBarcode(itemId, xBarcodeUpperLeft, yBarcodeUpperLeft, width, height, align);
4908 12 Jul 18 olle 3224     text = itemId;
4706 19 Mar 18 olle 3225     textFontSize = 10.0f;
4706 19 Mar 18 olle 3226     float xTextUpperLeft = 93.0f;
4706 19 Mar 18 olle 3227     float yTextUpperLeft = yBarcodeUpperLeft - 10.0f;
4706 19 Mar 18 olle 3228     pdfUtil.addText(text, textFontSize, align, xTextUpperLeft, yTextUpperLeft);
4706 19 Mar 18 olle 3229
4706 19 Mar 18 olle 3230     // Add blank extra page
4706 19 Mar 18 olle 3231     pdfUtil.newBlankPage();
4706 19 Mar 18 olle 3232
4706 19 Mar 18 olle 3233     return pdfUtil;
4706 19 Mar 18 olle 3234   }
4706 19 Mar 18 olle 3235
4908 12 Jul 18 olle 3236   private PdfUtil createClinChemBcBloodForm(DbControl dc, PdfUtil pdfUtil, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3237     throws IOException
4706 19 Mar 18 olle 3238   {    
4908 12 Jul 18 olle 3239     return createClinChemBcBloodForm(dc, pdfUtil, itemId, REF_FORM_TEMPLATE_CLIN_CHEM_BCBLOOD, formTemplateFileList);
4706 19 Mar 18 olle 3240   }
4706 19 Mar 18 olle 3241
4908 12 Jul 18 olle 3242   private PdfUtil createClinChemBcBloodForm(DbControl dc, PdfUtil pdfUtil, String itemId, String formTemplateName, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3243     throws IOException
4706 19 Mar 18 olle 3244   {    
4706 19 Mar 18 olle 3245     // Get PDF templates of same type
4908 12 Jul 18 olle 3246     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3247     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3248     List<File> pdfFiles = fetchReferralFormTemplateFileList(dc, itemIdDigits, formTemplateName);
4706 19 Mar 18 olle 3249     for (File pdfFile: pdfFiles)
4706 19 Mar 18 olle 3250     {
4908 12 Jul 18 olle 3251       pdfUtil = createClinChemBcBloodForm(dc, pdfUtil, pdfFile, itemId, formTemplateFileList);
4706 19 Mar 18 olle 3252     }
4706 19 Mar 18 olle 3253
4706 19 Mar 18 olle 3254     return pdfUtil;
4706 19 Mar 18 olle 3255   }
4706 19 Mar 18 olle 3256
4908 12 Jul 18 olle 3257   private PdfUtil createClinChemBcBloodForm(DbControl dc, PdfUtil pdfUtil, File pdfFile, String itemId, List<String> formTemplateFileList)
4706 19 Mar 18 olle 3258     throws IOException
4706 19 Mar 18 olle 3259   {    
4706 19 Mar 18 olle 3260     if (pdfFile == null)
4706 19 Mar 18 olle 3261     {
4706 19 Mar 18 olle 3262       return pdfUtil;
4706 19 Mar 18 olle 3263     }
4706 19 Mar 18 olle 3264     if (!formTemplateFileList.contains(pdfFile.getName()))
4706 19 Mar 18 olle 3265     {
4706 19 Mar 18 olle 3266       formTemplateFileList.add(pdfFile.getName());
4706 19 Mar 18 olle 3267     }
4706 19 Mar 18 olle 3268     InputStream pdfInStream = pdfFile.getDownloadStream(0);
4706 19 Mar 18 olle 3269     pdfUtil.newPage();
4706 19 Mar 18 olle 3270     // Import PDF document template
4706 19 Mar 18 olle 3271     float xOrig = 35.0f;
4706 19 Mar 18 olle 3272     float yOrig = 10.0f;
4706 19 Mar 18 olle 3273     float scaleX = 1.000f;
4706 19 Mar 18 olle 3274     float scaleY = 1.000f;
4706 19 Mar 18 olle 3275     int pageNo = 1;
4706 19 Mar 18 olle 3276     pdfUtil.importPdf(pdfInStream, pageNo, xOrig, yOrig, scaleX, scaleY);
4706 19 Mar 18 olle 3277
4908 12 Jul 18 olle 3278     // Item ID below bar code in upper right section
4724 29 Mar 18 olle 3279     // Add hyphen between site prefix and rest of index
4908 12 Jul 18 olle 3280     //String itemPrefix = Meludi.fetchSampleItemPrefix(dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3281     String itemPrefix = Meludi.fetchReferralPrefix(dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3282     //String itemIdDigits = Meludi.fetchRootDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3283     String itemIdDigits = Meludi.fetchReferralDigits(itemId, dc.getSessionControl().getActiveProjectId());
4908 12 Jul 18 olle 3284     String sitePrefix = itemIdDigits.substring(0,sitePrefixLength);
4908 12 Jul 18 olle 3285     String serialNumberResidue = itemIdDigits.substring(sitePrefixLength);
4908 12 Jul 18 olle 3286     String text = "BC-BLOD " + itemPrefix + sitePrefix + "-" + serialNumberResidue;
4706 19 Mar 18 olle 3287     float textFontSize = 9.7f;
4706 19 Mar 18 olle 3288     int align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 3289     float xTextUpperRight = 298.3f;
4706 19 Mar 18 olle 3290     float yTextUpperRight = 678.0f;
4706 19 Mar 18 olle 3291     pdfUtil.addBoldText(text, textFontSize, align, xTextUpperRight, yTextUpperRight);
4706 19 Mar 18 olle 3292
4908 12 Jul 18 olle 3293     // Item ID on both short sides of labels
4908 12 Jul 18 olle 3294     text = itemId;
4706 19 Mar 18 olle 3295     textFontSize = 9.5f;
4706 19 Mar 18 olle 3296     align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 3297     float xTextLowerLeft = 50.0f;
4706 19 Mar 18 olle 3298     float yTextLowerLeft = 141.0f;
4706 19 Mar 18 olle 3299     float yTextLowerLeft2 = yTextLowerLeft - 123.0f;
4706 19 Mar 18 olle 3300     float xLowerLeftDiff = 57.2f;
4706 19 Mar 18 olle 3301     // Right short side of label (label top in form)
4706 19 Mar 18 olle 3302     for (int i = 0; i < 5; i++)
4706 19 Mar 18 olle 3303     {
4706 19 Mar 18 olle 3304       float xTextLowerLeft2 = xTextLowerLeft + i*xLowerLeftDiff;
4706 19 Mar 18 olle 3305       pdfUtil.addBoldText(text, textFontSize, align, xTextLowerLeft2, yTextLowerLeft);
4706 19 Mar 18 olle 3306     }
4706 19 Mar 18 olle 3307     textFontSize = 7.0f;
4706 19 Mar 18 olle 3308     xTextLowerLeft = 57.0f;
4706 19 Mar 18 olle 3309     yTextLowerLeft = 148.0f;
4706 19 Mar 18 olle 3310     yTextLowerLeft2 = yTextLowerLeft - 123.0f;
4706 19 Mar 18 olle 3311     // Left short side of label (label bottom in form)
4706 19 Mar 18 olle 3312     for (int i = 0; i < 7; i++)
4706 19 Mar 18 olle 3313     {
4706 19 Mar 18 olle 3314       float xTextLowerLeft2 = xTextLowerLeft + i*xLowerLeftDiff;
4706 19 Mar 18 olle 3315       pdfUtil.addBoldText(text, textFontSize, align, xTextLowerLeft2, yTextLowerLeft2);
4706 19 Mar 18 olle 3316     }
4706 19 Mar 18 olle 3317     //
4706 19 Mar 18 olle 3318     //pdfUtil.drawGrid();
4706 19 Mar 18 olle 3319
4706 19 Mar 18 olle 3320     // Barcode at upper right
4706 19 Mar 18 olle 3321     float xBarcodeUpperRight = xTextUpperRight;
4706 19 Mar 18 olle 3322     float yBarcodeUpperRight = yTextUpperRight + 12.0f;
4706 19 Mar 18 olle 3323     float width = 88.0f;
4706 19 Mar 18 olle 3324     float height = 22.0f;
4706 19 Mar 18 olle 3325     align = Element.ALIGN_LEFT;
4908 12 Jul 18 olle 3326     pdfUtil.addBarcode(itemId, xBarcodeUpperRight, yBarcodeUpperRight, width, height, align);
4706 19 Mar 18 olle 3327
4706 19 Mar 18 olle 3328     // Barcode on labels
4706 19 Mar 18 olle 3329     float labelBarcodeWidth = 55.4f;
4706 19 Mar 18 olle 3330     float labelBarcodeHeight = 12.5f;
4706 19 Mar 18 olle 3331     float rotationDegrees = 90.0f;
4706 19 Mar 18 olle 3332     float xLabelBarcodeLeft = 62.0f;
4706 19 Mar 18 olle 3333     float yLabelBarcodeLeft = 38.3f;
4706 19 Mar 18 olle 3334     align = Element.ALIGN_LEFT;
4706 19 Mar 18 olle 3335     for (int i = 0; i < 9; i++)
4706 19 Mar 18 olle 3336     {
4706 19 Mar 18 olle 3337       float xLabelBarcodeLeft2 = xLabelBarcodeLeft + i*xLowerLeftDiff;
4908 12 Jul 18 olle 3338       pdfUtil.addBarcode(itemId, xLabelBarcodeLeft2, yLabelBarcodeLeft, labelBarcodeWidth, labelBarcodeHeight, align, rotationDegrees);
4706 19 Mar 18 olle 3339       // Barcode label text
4908 12 Jul 18 olle 3340       String labelText = "BC-BLOD " + itemPrefix + sitePrefix + "-" + serialNumberResidue;
4706 19 Mar 18 olle 3341       float xLabelTextLeft = xLabelBarcodeLeft2 + 24.0f;
4706 19 Mar 18 olle 3342       float yLabelTextLeft = yLabelBarcodeLeft + 5.0f;
4706 19 Mar 18 olle 3343       float labelTextFontSize = 10.0f;
4706 19 Mar 18 olle 3344       pdfUtil.addBoldText(labelText, labelTextFontSize, align, xLabelTextLeft, yLabelTextLeft, rotationDegrees);
4706 19 Mar 18 olle 3345     }
4706 19 Mar 18 olle 3346
4706 19 Mar 18 olle 3347     return pdfUtil;
4706 19 Mar 18 olle 3348   }
4706 19 Mar 18 olle 3349
4706 19 Mar 18 olle 3350   @SuppressWarnings("unchecked")
4706 19 Mar 18 olle 3351   @Override
4706 19 Mar 18 olle 3352   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
4706 19 Mar 18 olle 3353     throws ServletException, IOException 
4706 19 Mar 18 olle 3354   {
4706 19 Mar 18 olle 3355     String cmd = req.getParameter("cmd");
4706 19 Mar 18 olle 3356     JsonUtil.setJsonResponseHeaders(resp);
4706 19 Mar 18 olle 3357     
4706 19 Mar 18 olle 3358     JSONObject json = new JSONObject();
4706 19 Mar 18 olle 3359     json.put("status", "ok");
4706 19 Mar 18 olle 3360     
4706 19 Mar 18 olle 3361     JSONArray jsonMessages = new JSONArray();
4706 19 Mar 18 olle 3362
4706 19 Mar 18 olle 3363     final SessionControl sc = Meludi.getSessionControl(req);
4706 19 Mar 18 olle 3364     DbControl dc = null;
4706 19 Mar 18 olle 3365     String progressId = "";
4706 19 Mar 18 olle 3366   }
4706 19 Mar 18 olle 3367 }