extensions/net.sf.basedb.meludi/trunk/src/net/sf/basedb/meludi/Meludi.java

Code
Comments
Other
Rev Date Author Line
2933 14 Nov 14 olle 1 package net.sf.basedb.meludi;
2933 14 Nov 14 olle 2
4132 27 Sep 16 olle 3 import java.io.InputStream;
4132 27 Sep 16 olle 4 import java.net.URL;
2933 14 Nov 14 olle 5 import java.text.SimpleDateFormat;
2933 14 Nov 14 olle 6 import java.util.ArrayList;
2933 14 Nov 14 olle 7 import java.util.Collections;
4158 17 Oct 16 olle 8 import java.util.Date;
4127 26 Sep 16 olle 9 import java.util.HashMap;
2933 14 Nov 14 olle 10 import java.util.List;
2933 14 Nov 14 olle 11 import java.util.Set;
2933 14 Nov 14 olle 12 import java.util.regex.Pattern;
2933 14 Nov 14 olle 13
4706 19 Mar 18 olle 14 import javax.servlet.ServletRequest;
4706 19 Mar 18 olle 15
4132 27 Sep 16 olle 16 import org.jdom2.Document;
4142 30 Sep 16 olle 17 import org.jdom2.Element;
4142 30 Sep 16 olle 18 import org.json.simple.JSONObject;
4132 27 Sep 16 olle 19 import org.slf4j.Logger;
4132 27 Sep 16 olle 20 import org.slf4j.LoggerFactory;
4132 27 Sep 16 olle 21
2933 14 Nov 14 olle 22 import net.sf.basedb.core.AnnotationTypeCategory;
2933 14 Nov 14 olle 23 import net.sf.basedb.core.AnyToAny;
4706 19 Mar 18 olle 24 import net.sf.basedb.core.Application;
2933 14 Nov 14 olle 25 import net.sf.basedb.core.BasicItem;
4132 27 Sep 16 olle 26 import net.sf.basedb.core.ConfigurationException;
2933 14 Nov 14 olle 27 import net.sf.basedb.core.DbControl;
2933 14 Nov 14 olle 28 import net.sf.basedb.core.File;
2933 14 Nov 14 olle 29 import net.sf.basedb.core.FileSetMember;
2933 14 Nov 14 olle 30 import net.sf.basedb.core.FileStoreEnabled;
2933 14 Nov 14 olle 31 import net.sf.basedb.core.Include;
2933 14 Nov 14 olle 32 import net.sf.basedb.core.Item;
2933 14 Nov 14 olle 33 import net.sf.basedb.core.ItemQuery;
3100 21 Jan 15 olle 34 import net.sf.basedb.core.Project;
3100 21 Jan 15 olle 35 import net.sf.basedb.core.SessionControl;
3100 21 Jan 15 olle 36 import net.sf.basedb.core.SystemItems;
2933 14 Nov 14 olle 37 import net.sf.basedb.core.Type;
3100 21 Jan 15 olle 38 import net.sf.basedb.core.User;
2933 14 Nov 14 olle 39 import net.sf.basedb.core.query.Expressions;
2933 14 Nov 14 olle 40 import net.sf.basedb.core.query.Hql;
2933 14 Nov 14 olle 41 import net.sf.basedb.core.query.Restrictions;
2933 14 Nov 14 olle 42 import net.sf.basedb.meludi.converter.DateToStringConverter;
2933 14 Nov 14 olle 43 import net.sf.basedb.meludi.converter.StringToDateConverter;
4706 19 Mar 18 olle 44 import net.sf.basedb.meludi.dao.ClientApp;
4142 30 Sep 16 olle 45 import net.sf.basedb.meludi.dao.ProjectConfig;
4904 11 Jul 18 olle 46 import net.sf.basedb.meludi.dao.Subtype;
4132 27 Sep 16 olle 47 import net.sf.basedb.meludi.Meludi;
4132 27 Sep 16 olle 48 import net.sf.basedb.meludi.XmlConfig;
4132 27 Sep 16 olle 49 import net.sf.basedb.util.FileUtil;
4132 27 Sep 16 olle 50 import net.sf.basedb.util.XmlUtil2;
2933 14 Nov 14 olle 51
2933 14 Nov 14 olle 52 /**
2933 14 Nov 14 olle 53   Global constants for the MeLuDi package.
2933 14 Nov 14 olle 54   @since 1.0
2933 14 Nov 14 olle 55 */
2933 14 Nov 14 olle 56 public class Meludi
2933 14 Nov 14 olle 57 {
2933 14 Nov 14 olle 58   
4132 27 Sep 16 olle 59   private static final Logger logger = 
4132 27 Sep 16 olle 60     LoggerFactory.getLogger(Meludi.class);
4132 27 Sep 16 olle 61
4722 29 Mar 18 olle 62   private static HashMap<String,String> sitePrefixSiteKeyHashMap = null;
4722 29 Mar 18 olle 63   private static HashMap<String,String> siteKeySitePrefixHashMap = null;
4722 29 Mar 18 olle 64
2933 14 Nov 14 olle 65   /**
2933 14 Nov 14 olle 66     The current display title of this package.
2933 14 Nov 14 olle 67   */
3210 26 Mar 15 olle 68   public static final String TITLE = "MeLuDI";
2933 14 Nov 14 olle 69
2933 14 Nov 14 olle 70   /**
2933 14 Nov 14 olle 71     The current version of this package.
2933 14 Nov 14 olle 72   */
6805 24 Aug 22 olle 73   public static final String VERSION = "1.6.0";
2933 14 Nov 14 olle 74   
2933 14 Nov 14 olle 75   /**
2933 14 Nov 14 olle 76     Default converter for date values to string values: yyyyMMdd
2933 14 Nov 14 olle 77     @since 2.2
2933 14 Nov 14 olle 78   */
2933 14 Nov 14 olle 79   public static final DateToStringConverter CONVERTER_DATE_TO_STRING = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd"));
2933 14 Nov 14 olle 80   
2933 14 Nov 14 olle 81   /**
2933 14 Nov 14 olle 82     Default converter for string values to date values: yyyyMMdd
2933 14 Nov 14 olle 83     @since 2.2
2933 14 Nov 14 olle 84   */
2933 14 Nov 14 olle 85   public static final StringToDateConverter CONVERTER_STRING_TO_DATE = new StringToDateConverter(new SimpleDateFormat("yyyyMMdd"));
2933 14 Nov 14 olle 86   
2933 14 Nov 14 olle 87   /**
2933 14 Nov 14 olle 88     Default converter for date+time values to string values: yyyyMMdd HHmm
2933 14 Nov 14 olle 89     @since 2.2
2933 14 Nov 14 olle 90   */
2933 14 Nov 14 olle 91   public static final DateToStringConverter CONVERTER_DATETIME_TO_STRING = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd HHmm"));
2933 14 Nov 14 olle 92   
2933 14 Nov 14 olle 93   /**
2933 14 Nov 14 olle 94     Default converter for string values to date+time values: yyyyMMdd HHmm
2933 14 Nov 14 olle 95     @since 2.2
2933 14 Nov 14 olle 96   */
2933 14 Nov 14 olle 97   public static final StringToDateConverter CONVERTER_STRING_TO_DATETIME = new StringToDateConverter(new SimpleDateFormat("yyyyMMdd HHmm"));
2933 14 Nov 14 olle 98   
2933 14 Nov 14 olle 99   /**
2933 14 Nov 14 olle 100     Include options to make a query only return items in the currently active project.
2933 14 Nov 14 olle 101     Use with {@link ItemQuery#setIncludes(java.util.Collection)}
2933 14 Nov 14 olle 102     @since 2.4
2933 14 Nov 14 olle 103   */
2933 14 Nov 14 olle 104   public static final Set<Include> INCLUDE_IN_CURRENT_PROJECT = Collections.singleton(Include.IN_PROJECT);
4142 30 Sep 16 olle 105
2933 14 Nov 14 olle 106   /**
4142 30 Sep 16 olle 107     Path to the directory on the BASE file system where secondary analysis files
4142 30 Sep 16 olle 108     are saved. This directory maps some files to the "project-archive" directory 
4142 30 Sep 16 olle 109     on the file server.
4142 30 Sep 16 olle 110     @since 2.16
3416 23 Jun 15 olle 111   */
4706 19 Mar 18 olle 112   //public static final String SECONDARY_ANALYSIS_DIR = "/home/SCAN-B-rec/SecondaryAnalysis";
3416 23 Jun 15 olle 113
4706 19 Mar 18 olle 114   /**
4706 19 Mar 18 olle 115     Path to the directory on the BASE file system where referral form templates for
4706 19 Mar 18 olle 116     different sites are stored.
4706 19 Mar 18 olle 117     @since 1.6.0
4706 19 Mar 18 olle 118    */
4706 19 Mar 18 olle 119   //public static final String REFERRAL_FORM_TEMPLATES_DIR = "/home/SCAN-B-rec/ReferralFormTemplatesInUse";
4706 19 Mar 18 olle 120
4706 19 Mar 18 olle 121   /**
4706 19 Mar 18 olle 122     Path to the directory on the BASE file system where old referral form templates for
4706 19 Mar 18 olle 123     different sites are stored.
4706 19 Mar 18 olle 124     @since 1.6.0
4706 19 Mar 18 olle 125    */
4706 19 Mar 18 olle 126   //public static final String REFERRAL_FORM_TEMPLATES_OLD_DIR = "/home/SCAN-B-rec/ReferralFormTemplatesOld";
4706 19 Mar 18 olle 127
4142 30 Sep 16 olle 128   private MeludiConfiguration meludiConfiguration;
4142 30 Sep 16 olle 129   
4142 30 Sep 16 olle 130   public MeludiConfiguration getMeludiConfiguration()
4142 30 Sep 16 olle 131   {
4142 30 Sep 16 olle 132     if (this.meludiConfiguration == null)
4142 30 Sep 16 olle 133     {
4142 30 Sep 16 olle 134       this.meludiConfiguration = new MeludiConfiguration();
4142 30 Sep 16 olle 135     }
4142 30 Sep 16 olle 136     return this.meludiConfiguration;
4142 30 Sep 16 olle 137   }
4162 19 Oct 16 olle 138   
4142 30 Sep 16 olle 139 /*
4142 30 Sep 16 olle 140   public void setMeludiConfiguration(MeludiConfiguration meludiConfiguration)
4142 30 Sep 16 olle 141   {
4142 30 Sep 16 olle 142     this.meludiConfiguration = meludiConfiguration;
4142 30 Sep 16 olle 143   }
4142 30 Sep 16 olle 144 */
4142 30 Sep 16 olle 145
4162 19 Oct 16 olle 146   private static HashMap<Integer,String> projectIdNameHM;
4162 19 Oct 16 olle 147
4706 19 Mar 18 olle 148   /**
4706 19 Mar 18 olle 149     Returns project name for the desired project.
4706 19 Mar 18 olle 150     Returns empty string if no project has been selected.
4706 19 Mar 18 olle 151
4706 19 Mar 18 olle 152     @param activeProjectId int ID for the project to get project name for.
4706 19 Mar 18 olle 153     @return String Project name for the desired project.
4706 19 Mar 18 olle 154    */
4706 19 Mar 18 olle 155   private static String fetchProjectName(int activeProjectId)
4142 30 Sep 16 olle 156   {
4142 30 Sep 16 olle 157     String projectName = null;
4142 30 Sep 16 olle 158     if (activeProjectId > 0)
4142 30 Sep 16 olle 159     {
4162 19 Oct 16 olle 160       if (projectIdNameHM != null && projectIdNameHM.size() > 0)
4143 30 Sep 16 olle 161       {
4162 19 Oct 16 olle 162         projectName = projectIdNameHM.get(activeProjectId);
4143 30 Sep 16 olle 163       }
4142 30 Sep 16 olle 164     }
4706 19 Mar 18 olle 165     
4706 19 Mar 18 olle 166     return projectName;
4706 19 Mar 18 olle 167   }
4706 19 Mar 18 olle 168
4706 19 Mar 18 olle 169   /**
4706 19 Mar 18 olle 170     Path to the directory on the BASE file system where secondary analysis files
4706 19 Mar 18 olle 171     are saved. This directory maps some files to the "project-archive" directory 
4706 19 Mar 18 olle 172     on the file server.
4706 19 Mar 18 olle 173     Returns secondary analysis directory for the desired project.
4706 19 Mar 18 olle 174     Returns empty string if no project has been selected.
4706 19 Mar 18 olle 175
4706 19 Mar 18 olle 176     @param activeProjectId int ID for the project to get secondary analysis directory for.
4706 19 Mar 18 olle 177     @return String Secondary analysis directory name for the desired project.
4706 19 Mar 18 olle 178    */
4706 19 Mar 18 olle 179   public static String fetchSecondaryAnalysisDir(int activeProjectId)
4706 19 Mar 18 olle 180   {
4706 19 Mar 18 olle 181     String secondaryAnalysisDir = null;
4706 19 Mar 18 olle 182     String projectName = fetchProjectName(activeProjectId);
4706 19 Mar 18 olle 183     if (projectName != null)
4706 19 Mar 18 olle 184     {
4706 19 Mar 18 olle 185       secondaryAnalysisDir = "/home/" + projectName + "/SecondaryAnalysis";
4706 19 Mar 18 olle 186     }
4706 19 Mar 18 olle 187
4706 19 Mar 18 olle 188     return secondaryAnalysisDir;
4706 19 Mar 18 olle 189   }
4706 19 Mar 18 olle 190
4706 19 Mar 18 olle 191   /**
4706 19 Mar 18 olle 192     Returns referral templates directory for the desired project.
4706 19 Mar 18 olle 193     Returns empty string if no project has been selected.
4706 19 Mar 18 olle 194
4706 19 Mar 18 olle 195     @param activeProjectId int ID for the project to get referral templates directory for.
4706 19 Mar 18 olle 196     @return String Referral templates directory name for the desired project.
4706 19 Mar 18 olle 197    */
4706 19 Mar 18 olle 198   public static String fetchReferralFormTemplatesDir(int activeProjectId)
4706 19 Mar 18 olle 199   {
5271 29 Jan 19 olle 200     String projectName = fetchProjectName(activeProjectId);
5271 29 Jan 19 olle 201     String referralFormTemplatesDir = fetchReferralFormTemplatesDir(projectName);
5271 29 Jan 19 olle 202   
5271 29 Jan 19 olle 203     return referralFormTemplatesDir;
5271 29 Jan 19 olle 204   }
5271 29 Jan 19 olle 205
5271 29 Jan 19 olle 206   /**
5271 29 Jan 19 olle 207     Returns referral templates directory for the desired project.
5271 29 Jan 19 olle 208     Returns empty string if no project has been selected.
5271 29 Jan 19 olle 209
5271 29 Jan 19 olle 210     @param projectName String Name of the project to get referral templates directory for.
5271 29 Jan 19 olle 211     @return String Referral templates directory name for the desired project.
5271 29 Jan 19 olle 212   */
5271 29 Jan 19 olle 213   public static String fetchReferralFormTemplatesDir(String projectName)
5271 29 Jan 19 olle 214   {
4706 19 Mar 18 olle 215     String referralFormTemplatesDir = null;
4706 19 Mar 18 olle 216     if (projectName != null)
4706 19 Mar 18 olle 217     {
4706 19 Mar 18 olle 218       referralFormTemplatesDir = "/home/" + projectName + "/ReferralFormTemplatesInUse";
4706 19 Mar 18 olle 219     }
5271 29 Jan 19 olle 220
4706 19 Mar 18 olle 221     return referralFormTemplatesDir;
4706 19 Mar 18 olle 222   }
4706 19 Mar 18 olle 223
4706 19 Mar 18 olle 224   /**
4706 19 Mar 18 olle 225     Returns referral old templates directory for the desired project.
4706 19 Mar 18 olle 226     Returns empty string if no project has been selected.
4706 19 Mar 18 olle 227
4706 19 Mar 18 olle 228     @param activeProjectId int ID for the project to get referral old templates directory for.
4706 19 Mar 18 olle 229     @return String Referral old templates directory name for the desired project.
4706 19 Mar 18 olle 230    */
4706 19 Mar 18 olle 231   public static String fetchReferralFormTemplatesOldDir(int activeProjectId)
4706 19 Mar 18 olle 232   {
5271 29 Jan 19 olle 233     String projectName = fetchProjectName(activeProjectId);
5271 29 Jan 19 olle 234     String referralFormTemplatesOldDir = fetchReferralFormTemplatesOldDir(projectName);
5271 29 Jan 19 olle 235     
5271 29 Jan 19 olle 236     return referralFormTemplatesOldDir;
5271 29 Jan 19 olle 237   }
5271 29 Jan 19 olle 238
5271 29 Jan 19 olle 239   /**
5271 29 Jan 19 olle 240     Returns referral old templates directory for the desired project.
5271 29 Jan 19 olle 241     Returns empty string if no project has been selected.
5271 29 Jan 19 olle 242
5271 29 Jan 19 olle 243     @param projectName String Name of the project to get referral templates directory for.
5271 29 Jan 19 olle 244     @return String Referral old templates directory name for the desired project.
5271 29 Jan 19 olle 245   */
5271 29 Jan 19 olle 246   public static String fetchReferralFormTemplatesOldDir(String projectName)
5271 29 Jan 19 olle 247   {
4706 19 Mar 18 olle 248     String referralFormTemplatesOldDir = null;
4706 19 Mar 18 olle 249     if (projectName != null)
4706 19 Mar 18 olle 250     {
4706 19 Mar 18 olle 251       referralFormTemplatesOldDir = "/home/" + projectName + "/ReferralFormTemplatesOld";
4706 19 Mar 18 olle 252     }
5271 29 Jan 19 olle 253   
4706 19 Mar 18 olle 254     return referralFormTemplatesOldDir;
4706 19 Mar 18 olle 255   }
4706 19 Mar 18 olle 256
5271 29 Jan 19 olle 257   /**
5271 29 Jan 19 olle 258     Returns baseline files directory for the desired project.
5271 29 Jan 19 olle 259     Returns empty string if no project has been selected.
5271 29 Jan 19 olle 260
5271 29 Jan 19 olle 261     @param activeProjectId int ID for the project to get baseline files directory for.
5271 29 Jan 19 olle 262     @return String Baseline files directory name for the desired project.
5271 29 Jan 19 olle 263   */
5271 29 Jan 19 olle 264   public static String fetchBaselineFilesDir(int activeProjectId)
5271 29 Jan 19 olle 265   {
5271 29 Jan 19 olle 266     String projectName = fetchProjectName(activeProjectId);
5271 29 Jan 19 olle 267     String baselineFilesDir = fetchBaselineFilesDir(projectName);
5271 29 Jan 19 olle 268   
5271 29 Jan 19 olle 269     return baselineFilesDir;
5271 29 Jan 19 olle 270   }
5271 29 Jan 19 olle 271
5271 29 Jan 19 olle 272   /**
5271 29 Jan 19 olle 273     Returns baseline files directory for the desired project.
5271 29 Jan 19 olle 274     Returns empty string if no project has been selected.
5271 29 Jan 19 olle 275
5271 29 Jan 19 olle 276     @param projectName String Name of the project to get baseline files directory for.
5271 29 Jan 19 olle 277     @return String Baseline files directory name for the desired project.
5271 29 Jan 19 olle 278    */
5271 29 Jan 19 olle 279   public static String fetchBaselineFilesDir(String projectName)
5271 29 Jan 19 olle 280   {
5271 29 Jan 19 olle 281     String baselineFilesDir = null;
5271 29 Jan 19 olle 282     if (projectName != null)
5271 29 Jan 19 olle 283     {
5271 29 Jan 19 olle 284       baselineFilesDir = "/home/" + projectName + "/BaselineFiles";
5271 29 Jan 19 olle 285     }
5271 29 Jan 19 olle 286
5271 29 Jan 19 olle 287     return baselineFilesDir;
5271 29 Jan 19 olle 288   }
5271 29 Jan 19 olle 289
4706 19 Mar 18 olle 290   private static ProjectConfig fetchProjectConfig(int activeProjectId)
4706 19 Mar 18 olle 291   {
4706 19 Mar 18 olle 292     String projectName = fetchProjectName(activeProjectId);
5271 29 Jan 19 olle 293     ProjectConfig projectConfig = fetchProjectConfig(projectName);
5271 29 Jan 19 olle 294     
5271 29 Jan 19 olle 295     return projectConfig;
5271 29 Jan 19 olle 296   }
5271 29 Jan 19 olle 297
5271 29 Jan 19 olle 298   private static ProjectConfig fetchProjectConfig(String projectName)
5271 29 Jan 19 olle 299   {
4142 30 Sep 16 olle 300     MeludiConfiguration meludiConfiguration = new MeludiConfiguration();
4142 30 Sep 16 olle 301     ProjectConfig projectConfig = meludiConfiguration.findByName(projectName);
4142 30 Sep 16 olle 302     
4142 30 Sep 16 olle 303     return projectConfig;
4142 30 Sep 16 olle 304   }
4142 30 Sep 16 olle 305
4176 25 Oct 16 olle 306   /**
4829 01 Jun 18 olle 307     Returns project name for the desired project.
4829 01 Jun 18 olle 308     Returns empty string if no project has been selected.
4829 01 Jun 18 olle 309
4829 01 Jun 18 olle 310     @param activeProjectId int ID for the project to get project name for.
4829 01 Jun 18 olle 311     @return String Project name for the desired project.
4829 01 Jun 18 olle 312   */
4829 01 Jun 18 olle 313   public static String fetchActiveProjectName(int activeProjectId)
4829 01 Jun 18 olle 314   {
4829 01 Jun 18 olle 315     String projectName = fetchProjectName(activeProjectId);
4829 01 Jun 18 olle 316     
4829 01 Jun 18 olle 317     return projectName;
4829 01 Jun 18 olle 318   }
4829 01 Jun 18 olle 319
4829 01 Jun 18 olle 320   /**
4839 08 Jun 18 olle 321     Returns referral prefix for the desired project.
4839 08 Jun 18 olle 322     Returns empty string if no referral prefix has been
4839 08 Jun 18 olle 323     configured for the project.
4839 08 Jun 18 olle 324
4839 08 Jun 18 olle 325     @param activeProjectId int ID for the project to get referral prefix for.
4839 08 Jun 18 olle 326     @return String Referral prefix for the desired project.
4839 08 Jun 18 olle 327   */
4839 08 Jun 18 olle 328   public static String fetchReferralPrefix(int activeProjectId)
4839 08 Jun 18 olle 329   {
4839 08 Jun 18 olle 330     String prefix = "";
4839 08 Jun 18 olle 331     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4839 08 Jun 18 olle 332     if (projectConfig != null)
4839 08 Jun 18 olle 333     {
4839 08 Jun 18 olle 334       prefix = projectConfig.getReferralPrefix();
4839 08 Jun 18 olle 335     }
4839 08 Jun 18 olle 336
4839 08 Jun 18 olle 337     return prefix;
4839 08 Jun 18 olle 338   }
4839 08 Jun 18 olle 339
4839 08 Jun 18 olle 340   /**
4839 08 Jun 18 olle 341     Returns referral number of digits for the desired project.
4839 08 Jun 18 olle 342     Returns `5` if no project configuration object could be found
4839 08 Jun 18 olle 343     for the project.
4839 08 Jun 18 olle 344
4839 08 Jun 18 olle 345     @param activeProjectId int ID for the project to get referral number of digits for.
4839 08 Jun 18 olle 346     @return Integer Referral number of digits for the desired project.
4839 08 Jun 18 olle 347    */
4839 08 Jun 18 olle 348   public static Integer fetchReferralNumDigits(int activeProjectId)
4839 08 Jun 18 olle 349   {
4839 08 Jun 18 olle 350     Integer numDigits = MeludiConfiguration.REFERRAL_NUM_DIGITS_DEFAULT;
4839 08 Jun 18 olle 351     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4839 08 Jun 18 olle 352     if (projectConfig != null)
4839 08 Jun 18 olle 353     {
4839 08 Jun 18 olle 354       numDigits = projectConfig.getReferralNumDigits();
4839 08 Jun 18 olle 355     }
4839 08 Jun 18 olle 356
4839 08 Jun 18 olle 357   return numDigits;
4839 08 Jun 18 olle 358   }
4839 08 Jun 18 olle 359
4839 08 Jun 18 olle 360   /**
4176 25 Oct 16 olle 361     Returns sample item prefix for the desired project.
4176 25 Oct 16 olle 362     Returns empty string if no sample item prefix has been
4176 25 Oct 16 olle 363     configured for the project.
4176 25 Oct 16 olle 364
4176 25 Oct 16 olle 365     @param activeProjectId int ID for the project to get sample item prefix for.
4176 25 Oct 16 olle 366     @return String Sample item prefix for the desired project.
4176 25 Oct 16 olle 367   */
4142 30 Sep 16 olle 368   public static String fetchSampleItemPrefix(int activeProjectId)
4142 30 Sep 16 olle 369   {
4176 25 Oct 16 olle 370     String prefix = "";
4142 30 Sep 16 olle 371     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4176 25 Oct 16 olle 372     if (projectConfig != null)
4176 25 Oct 16 olle 373     {
4176 25 Oct 16 olle 374       prefix = projectConfig.getSampleItemPrefix();
4176 25 Oct 16 olle 375     }
4142 30 Sep 16 olle 376
4142 30 Sep 16 olle 377     return prefix;
4142 30 Sep 16 olle 378   }
4142 30 Sep 16 olle 379
3416 23 Jun 15 olle 380   /**
5253 21 Jan 19 olle 381     Returns specimen item prefix for the desired project.
5253 21 Jan 19 olle 382     Returns empty string if no sample item prefix has been
5253 21 Jan 19 olle 383     configured for the project.
5253 21 Jan 19 olle 384
5253 21 Jan 19 olle 385     @param activeProjectId int ID for the project to get specimen item prefix for.
5254 21 Jan 19 olle 386     @return String Specimen item prefix for the desired project.
5253 21 Jan 19 olle 387   */
5253 21 Jan 19 olle 388   public static String fetchSpecimenItemPrefix(int activeProjectId)
5253 21 Jan 19 olle 389   {
5253 21 Jan 19 olle 390     String prefix = "";
5253 21 Jan 19 olle 391     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
5253 21 Jan 19 olle 392     if (projectConfig != null)
5253 21 Jan 19 olle 393     {
5253 21 Jan 19 olle 394       prefix = projectConfig.getSampleItemPrefix();
5253 21 Jan 19 olle 395       String ffpeBlockItemPrefix = projectConfig.getFfpeBlockItemPrefix();
5253 21 Jan 19 olle 396       if (prefix != null && !prefix.equals(ffpeBlockItemPrefix))
5253 21 Jan 19 olle 397       {
5253 21 Jan 19 olle 398         prefix = ffpeBlockItemPrefix;
5253 21 Jan 19 olle 399       }
5253 21 Jan 19 olle 400     }
5253 21 Jan 19 olle 401
5253 21 Jan 19 olle 402     return prefix;
5253 21 Jan 19 olle 403   }
5253 21 Jan 19 olle 404
5253 21 Jan 19 olle 405   /**
4213 08 Nov 16 olle 406     Returns sample item number of digits for the desired project.
4235 11 Nov 16 olle 407     Returns `5` if no project configuration object could be found
4213 08 Nov 16 olle 408     for the project.
4213 08 Nov 16 olle 409
4213 08 Nov 16 olle 410     @param activeProjectId int ID for the project to get sample item number of digits for.
4213 08 Nov 16 olle 411     @return Integer Sample item number of digits for the desired project.
4213 08 Nov 16 olle 412   */
4213 08 Nov 16 olle 413   public static Integer fetchSampleItemNumDigits(int activeProjectId)
4213 08 Nov 16 olle 414   {
4235 11 Nov 16 olle 415     Integer numDigits = MeludiConfiguration.SAMPLE_ITEM_NUM_DIGITS_DEFAULT;
4213 08 Nov 16 olle 416     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4213 08 Nov 16 olle 417     if (projectConfig != null)
4213 08 Nov 16 olle 418     {
4213 08 Nov 16 olle 419       numDigits = projectConfig.getSampleItemNumDigits();
4213 08 Nov 16 olle 420     }
4213 08 Nov 16 olle 421
4213 08 Nov 16 olle 422     return numDigits;
4213 08 Nov 16 olle 423   }
4213 08 Nov 16 olle 424
4213 08 Nov 16 olle 425   /**
4867 21 Jun 18 olle 426     Returns blood item prefix for the desired project.
4867 21 Jun 18 olle 427     Returns empty string if no blood item prefix has been
4867 21 Jun 18 olle 428     configured for the project.
4867 21 Jun 18 olle 429
4867 21 Jun 18 olle 430     @param activeProjectId int ID for the project to get blood item prefix for.
4867 21 Jun 18 olle 431     @return String Blood item prefix for the desired project.
4867 21 Jun 18 olle 432   */
4867 21 Jun 18 olle 433   public static String fetchBloodItemPrefix(int activeProjectId)
4867 21 Jun 18 olle 434   {
4867 21 Jun 18 olle 435     String prefix = "";
4867 21 Jun 18 olle 436     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4867 21 Jun 18 olle 437     if (projectConfig != null)
4867 21 Jun 18 olle 438     {
4867 21 Jun 18 olle 439       prefix = projectConfig.getBloodItemPrefix();
4867 21 Jun 18 olle 440     }
4867 21 Jun 18 olle 441
4867 21 Jun 18 olle 442     return prefix;
4867 21 Jun 18 olle 443   }
4867 21 Jun 18 olle 444
4867 21 Jun 18 olle 445   /**
4901 10 Jul 18 olle 446     Returns blood item number of digits for the desired project.
4901 10 Jul 18 olle 447     Returns `5` if no project configuration object could be found
4901 10 Jul 18 olle 448     for the project.
4901 10 Jul 18 olle 449
4901 10 Jul 18 olle 450     @param activeProjectId int ID for the project to get blood item number of digits for.
4901 10 Jul 18 olle 451     @return Integer Blood item number of digits for the desired project.
4901 10 Jul 18 olle 452   */
4901 10 Jul 18 olle 453   public static Integer fetchBloodItemNumDigits(int activeProjectId)
4901 10 Jul 18 olle 454   {
4901 10 Jul 18 olle 455     Integer numDigits = MeludiConfiguration.BLOOD_ITEM_NUM_DIGITS_DEFAULT;
4901 10 Jul 18 olle 456     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4901 10 Jul 18 olle 457     if (projectConfig != null)
4901 10 Jul 18 olle 458     {
4901 10 Jul 18 olle 459       numDigits = projectConfig.getBloodItemNumDigits();
4901 10 Jul 18 olle 460     }
4901 10 Jul 18 olle 461
4901 10 Jul 18 olle 462     return numDigits;
4901 10 Jul 18 olle 463   }
4901 10 Jul 18 olle 464
4901 10 Jul 18 olle 465   /**
4901 10 Jul 18 olle 466     Returns FFPE block item prefix for the desired project.
4901 10 Jul 18 olle 467     Returns empty string if no FFPE block item prefix has been
4901 10 Jul 18 olle 468     configured for the project.
4901 10 Jul 18 olle 469
4901 10 Jul 18 olle 470     @param activeProjectId int ID for the project to get FFPE block item prefix for.
4901 10 Jul 18 olle 471     @return String FFPE block item prefix for the desired project.
4901 10 Jul 18 olle 472   */
4901 10 Jul 18 olle 473   public static String fetchFfpeBlockItemPrefix(int activeProjectId)
4901 10 Jul 18 olle 474   {
4901 10 Jul 18 olle 475     String prefix = "";
4901 10 Jul 18 olle 476     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4901 10 Jul 18 olle 477     if (projectConfig != null)
4901 10 Jul 18 olle 478     {
4901 10 Jul 18 olle 479       prefix = projectConfig.getFfpeBlockItemPrefix();
4901 10 Jul 18 olle 480     }
4901 10 Jul 18 olle 481
4901 10 Jul 18 olle 482     return prefix;
4901 10 Jul 18 olle 483   }
4901 10 Jul 18 olle 484
4901 10 Jul 18 olle 485   /**
4901 10 Jul 18 olle 486     Returns FFPE block item number of digits for the desired project.
4901 10 Jul 18 olle 487     Returns `5` if no project configuration object could be found
4901 10 Jul 18 olle 488     for the project.
4901 10 Jul 18 olle 489
4901 10 Jul 18 olle 490     @param activeProjectId int ID for the project to get FFPE block item number of digits for.
4901 10 Jul 18 olle 491     @return Integer FFPE block item number of digits for the desired project.
4901 10 Jul 18 olle 492    */
4901 10 Jul 18 olle 493   public static Integer fetchFfpeBlockItemNumDigits(int activeProjectId)
4901 10 Jul 18 olle 494   {
4901 10 Jul 18 olle 495     Integer numDigits = MeludiConfiguration.FFPE_BLOCK_ITEM_NUM_DIGITS_DEFAULT;
4901 10 Jul 18 olle 496     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4901 10 Jul 18 olle 497     if (projectConfig != null)
4901 10 Jul 18 olle 498     {
4901 10 Jul 18 olle 499       numDigits = projectConfig.getFfpeBlockItemNumDigits();
4901 10 Jul 18 olle 500     }
4901 10 Jul 18 olle 501
4901 10 Jul 18 olle 502     return numDigits;
4901 10 Jul 18 olle 503   }
4901 10 Jul 18 olle 504
4901 10 Jul 18 olle 505   /**
4875 27 Jun 18 olle 506     Returns `true` if blood item prefix differs from
4875 27 Jun 18 olle 507     sample item prefix for the desired project, else `false`.
4875 27 Jun 18 olle 508     For this comparison a `null` value for a prefix is
4875 27 Jun 18 olle 509     treated as an empty string.
4875 27 Jun 18 olle 510
4875 27 Jun 18 olle 511     @param activeProjectId int ID for the project to check prefixes for.
4875 27 Jun 18 olle 512     @return boolean Returns `true` if blood and sample item prefixes differs for the desired project.
4875 27 Jun 18 olle 513   */
4875 27 Jun 18 olle 514   public static boolean bloodAndSampleItemPrefixesDiffer(int activeProjectId)
4875 27 Jun 18 olle 515   {
4875 27 Jun 18 olle 516     boolean prefixesDiffer = false;
4875 27 Jun 18 olle 517     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4875 27 Jun 18 olle 518     if (projectConfig != null)
4875 27 Jun 18 olle 519     {
4875 27 Jun 18 olle 520       String bloodItemPrefix = projectConfig.getBloodItemPrefix();
4875 27 Jun 18 olle 521       String sampleItemPrefix = projectConfig.getSampleItemPrefix();
4875 27 Jun 18 olle 522       if (bloodItemPrefix == null)
4875 27 Jun 18 olle 523       {
4875 27 Jun 18 olle 524         bloodItemPrefix = "";
4875 27 Jun 18 olle 525       }
4875 27 Jun 18 olle 526       if (sampleItemPrefix == null)
4875 27 Jun 18 olle 527       {
4875 27 Jun 18 olle 528         sampleItemPrefix = "";
4875 27 Jun 18 olle 529       }
4875 27 Jun 18 olle 530       if (!bloodItemPrefix.equals(sampleItemPrefix))
4875 27 Jun 18 olle 531       {
4875 27 Jun 18 olle 532         prefixesDiffer = true;
4875 27 Jun 18 olle 533       }
4875 27 Jun 18 olle 534     }
4875 27 Jun 18 olle 535     return prefixesDiffer;
4875 27 Jun 18 olle 536   }
4875 27 Jun 18 olle 537
4875 27 Jun 18 olle 538   /**
4184 27 Oct 16 olle 539     Returns storage box prefix for the desired project.
4184 27 Oct 16 olle 540     Returns empty string if no storage box prefix has been
4184 27 Oct 16 olle 541     configured for the project.
4184 27 Oct 16 olle 542
4184 27 Oct 16 olle 543     @param activeProjectId int ID for the project to get storage box prefix for.
4184 27 Oct 16 olle 544     @return String Storage box prefix for the desired project.
4184 27 Oct 16 olle 545   */
4184 27 Oct 16 olle 546   public static String fetchStorageBoxPrefix(int activeProjectId)
4184 27 Oct 16 olle 547   {
4184 27 Oct 16 olle 548     String prefix = "";
4184 27 Oct 16 olle 549     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4184 27 Oct 16 olle 550     if (projectConfig != null)
4184 27 Oct 16 olle 551     {
4184 27 Oct 16 olle 552       prefix = projectConfig.getStorageBoxPrefix();
4184 27 Oct 16 olle 553     }
4184 27 Oct 16 olle 554
4184 27 Oct 16 olle 555     return prefix;
4184 27 Oct 16 olle 556   }
4184 27 Oct 16 olle 557
4184 27 Oct 16 olle 558   /**
4213 08 Nov 16 olle 559     Returns storage box number of digits for the desired project.
4235 11 Nov 16 olle 560     Returns `4` if no project configuration object could be found
4213 08 Nov 16 olle 561     for the project.
4213 08 Nov 16 olle 562
4213 08 Nov 16 olle 563     @param activeProjectId int ID for the project to get storage box number of digits for.
4213 08 Nov 16 olle 564     @return Integer Storage box number of digits for the desired project.
4213 08 Nov 16 olle 565   */
4213 08 Nov 16 olle 566   public static Integer fetchStorageBoxNumDigits(int activeProjectId)
4213 08 Nov 16 olle 567   {
4235 11 Nov 16 olle 568     Integer numDigits = MeludiConfiguration.STORAGE_BOX_NUM_DIGITS_DEFAULT;
4213 08 Nov 16 olle 569     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4213 08 Nov 16 olle 570     if (projectConfig != null)
4213 08 Nov 16 olle 571     {
4213 08 Nov 16 olle 572       numDigits = projectConfig.getStorageBoxNumDigits();
4213 08 Nov 16 olle 573     }
4213 08 Nov 16 olle 574
4213 08 Nov 16 olle 575     return numDigits;
4213 08 Nov 16 olle 576   }
4213 08 Nov 16 olle 577
4213 08 Nov 16 olle 578   /**
4142 30 Sep 16 olle 579     Returns start list item prefix for the desired project.
4176 25 Oct 16 olle 580     Returns empty string if no start list item prefix has been
4176 25 Oct 16 olle 581     configured for the project.
4142 30 Sep 16 olle 582
4142 30 Sep 16 olle 583     @param activeProjectId int ID for the project to get start list item prefix for.
4142 30 Sep 16 olle 584     @return String Start list item prefix for the desired project.
2933 14 Nov 14 olle 585   */
4142 30 Sep 16 olle 586   public static String fetchStartListItemPrefix(int activeProjectId)
4142 30 Sep 16 olle 587   {
4176 25 Oct 16 olle 588     String prefix = "";
4142 30 Sep 16 olle 589     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4176 25 Oct 16 olle 590     if (projectConfig != null)
4176 25 Oct 16 olle 591     {
4176 25 Oct 16 olle 592       prefix = projectConfig.getStartListItemPrefix();
4176 25 Oct 16 olle 593     }
4142 30 Sep 16 olle 594
4142 30 Sep 16 olle 595     return prefix;
4142 30 Sep 16 olle 596   }
4142 30 Sep 16 olle 597
4176 25 Oct 16 olle 598   /**
4213 08 Nov 16 olle 599     Returns start list item number of digits for the desired project.
4235 11 Nov 16 olle 600     Returns `5` if no project configuration object could be found
4213 08 Nov 16 olle 601     for the project.
4213 08 Nov 16 olle 602
4213 08 Nov 16 olle 603     @param activeProjectId int ID for the project to get start list item number of digits for.
4213 08 Nov 16 olle 604     @return Integer Start list item number of digits for the desired project.
4213 08 Nov 16 olle 605   */
4213 08 Nov 16 olle 606   public static Integer fetchStartListItemNumDigits(int activeProjectId)
4213 08 Nov 16 olle 607   {
4235 11 Nov 16 olle 608     Integer numDigits = MeludiConfiguration.START_LIST_ITEM_NUM_DIGITS_DEFAULT;
4213 08 Nov 16 olle 609     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4213 08 Nov 16 olle 610     if (projectConfig != null)
4213 08 Nov 16 olle 611     {
4213 08 Nov 16 olle 612       numDigits = projectConfig.getStartListItemNumDigits();
4213 08 Nov 16 olle 613     }
4213 08 Nov 16 olle 614
4213 08 Nov 16 olle 615     return numDigits;
4213 08 Nov 16 olle 616   }
4213 08 Nov 16 olle 617
4213 08 Nov 16 olle 618   /**
4176 25 Oct 16 olle 619     Returns start plate item prefix for the desired project.
4176 25 Oct 16 olle 620     Returns empty string if no start plate item prefix has been
4176 25 Oct 16 olle 621     configured for the project.
4176 25 Oct 16 olle 622
4176 25 Oct 16 olle 623     @param activeProjectId int ID for the project to get start plate item prefix for.
4176 25 Oct 16 olle 624     @return String Start plate item prefix for the desired project.
4176 25 Oct 16 olle 625   */
4142 30 Sep 16 olle 626   public static String fetchStartPlateItemPrefix(int activeProjectId)
4142 30 Sep 16 olle 627   {
4176 25 Oct 16 olle 628     String prefix = "";
4142 30 Sep 16 olle 629     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4176 25 Oct 16 olle 630     if (projectConfig != null)
4176 25 Oct 16 olle 631     {
4176 25 Oct 16 olle 632       prefix = projectConfig.getStartPlateItemPrefix();
4176 25 Oct 16 olle 633     }
4142 30 Sep 16 olle 634
4142 30 Sep 16 olle 635     return prefix;
4142 30 Sep 16 olle 636   }
4142 30 Sep 16 olle 637
4176 25 Oct 16 olle 638   /**
4213 08 Nov 16 olle 639     Returns start plate item number of digits for the desired project.
4235 11 Nov 16 olle 640     Returns `5` if no project configuration object could be found
4213 08 Nov 16 olle 641     for the project.
4213 08 Nov 16 olle 642
4213 08 Nov 16 olle 643     @param activeProjectId int ID for the project to get start plate item number of digits for.
4213 08 Nov 16 olle 644     @return Integer Start plate item number of digits for the desired project.
4213 08 Nov 16 olle 645    */
4213 08 Nov 16 olle 646   public static Integer fetchStartPlateItemNumDigits(int activeProjectId)
4213 08 Nov 16 olle 647   {
4235 11 Nov 16 olle 648     Integer numDigits = MeludiConfiguration.START_PLATE_ITEM_NUM_DIGITS_DEFAULT;
4213 08 Nov 16 olle 649     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4213 08 Nov 16 olle 650     if (projectConfig != null)
4213 08 Nov 16 olle 651     {
4213 08 Nov 16 olle 652       numDigits = projectConfig.getStartPlateItemNumDigits();
4213 08 Nov 16 olle 653     }
4213 08 Nov 16 olle 654
4213 08 Nov 16 olle 655     return numDigits;
4213 08 Nov 16 olle 656   }
4213 08 Nov 16 olle 657
4213 08 Nov 16 olle 658   /**
4176 25 Oct 16 olle 659     Returns patient item prefix for the desired project.
4176 25 Oct 16 olle 660     Returns empty string if no patient item prefix has been
4176 25 Oct 16 olle 661     configured for the project.
4176 25 Oct 16 olle 662
4176 25 Oct 16 olle 663     @param activeProjectId int ID for the project to get patient item prefix for.
4176 25 Oct 16 olle 664     @return String Patient item prefix for the desired project.
4176 25 Oct 16 olle 665   */
4142 30 Sep 16 olle 666   public static String fetchPatientItemPrefix(int activeProjectId)
4142 30 Sep 16 olle 667   {
4176 25 Oct 16 olle 668     String prefix = "";
4142 30 Sep 16 olle 669     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4176 25 Oct 16 olle 670     if (projectConfig != null)
4176 25 Oct 16 olle 671     {
4176 25 Oct 16 olle 672       prefix = projectConfig.getPatientItemPrefix();
4176 25 Oct 16 olle 673     }
4142 30 Sep 16 olle 674
4142 30 Sep 16 olle 675     return prefix;
4142 30 Sep 16 olle 676   }
4142 30 Sep 16 olle 677
4176 25 Oct 16 olle 678   /**
4213 08 Nov 16 olle 679     Returns patient item number of digits for the desired project.
4235 11 Nov 16 olle 680     Returns `6` if no project configuration object could be found
4213 08 Nov 16 olle 681     for the project.
4213 08 Nov 16 olle 682
4213 08 Nov 16 olle 683     @param activeProjectId int ID for the project to get patient item number of digits for.
4213 08 Nov 16 olle 684     @return Integer Patient item number of digits for the desired project.
4213 08 Nov 16 olle 685   */
4213 08 Nov 16 olle 686   public static Integer fetchPatientItemNumDigits(int activeProjectId)
4213 08 Nov 16 olle 687   {
4235 11 Nov 16 olle 688     Integer numDigits = MeludiConfiguration.PATIENT_ITEM_NUM_DIGITS_DEFAULT;
4213 08 Nov 16 olle 689     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4213 08 Nov 16 olle 690     if (projectConfig != null)
4213 08 Nov 16 olle 691     {
4213 08 Nov 16 olle 692       numDigits = projectConfig.getPatientItemNumDigits();
4213 08 Nov 16 olle 693     }
4213 08 Nov 16 olle 694
4213 08 Nov 16 olle 695     return numDigits;
4213 08 Nov 16 olle 696   }
4213 08 Nov 16 olle 697
4213 08 Nov 16 olle 698   /**
4184 27 Oct 16 olle 699     Returns library preparation kit item prefix for the desired project.
4184 27 Oct 16 olle 700     Returns empty string if no library preparation kit item prefix has been
4184 27 Oct 16 olle 701     configured for the project.
4184 27 Oct 16 olle 702
4184 27 Oct 16 olle 703     @param activeProjectId int ID for the project to get library preparation kit item prefix for.
4184 27 Oct 16 olle 704     @return String Library preparation kit item prefix for the desired project.
4184 27 Oct 16 olle 705   */
4184 27 Oct 16 olle 706   public static String fetchLibraryPreparationKitItemPrefix(int activeProjectId)
4184 27 Oct 16 olle 707   {
4184 27 Oct 16 olle 708     String prefix = "";
4184 27 Oct 16 olle 709     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4184 27 Oct 16 olle 710     if (projectConfig != null)
4184 27 Oct 16 olle 711     {
4184 27 Oct 16 olle 712       prefix = projectConfig.getLibraryPreparationKitItemPrefix();
4184 27 Oct 16 olle 713     }
4184 27 Oct 16 olle 714
4184 27 Oct 16 olle 715     return prefix;
4184 27 Oct 16 olle 716   }
4184 27 Oct 16 olle 717
4184 27 Oct 16 olle 718   /**
4227 10 Nov 16 olle 719     Returns library preparation kit item number of digits for the desired project.
4235 11 Nov 16 olle 720     Returns `3` if no project configuration object could be found
4227 10 Nov 16 olle 721     for the project.
4227 10 Nov 16 olle 722
4227 10 Nov 16 olle 723     @param activeProjectId int ID for the project to get library preparation kit item number of digits for.
4227 10 Nov 16 olle 724     @return String Library preparation kit item number of digits for the desired project.
4227 10 Nov 16 olle 725   */
4227 10 Nov 16 olle 726   public static Integer fetchLibraryPreparationKitItemNumDigits(int activeProjectId)
4227 10 Nov 16 olle 727   {
4235 11 Nov 16 olle 728     Integer numDigits = MeludiConfiguration.LIBPREP_KIT_ITEM_NUM_DIGITS_DEFAULT;
4227 10 Nov 16 olle 729     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4227 10 Nov 16 olle 730     if (projectConfig != null)
4227 10 Nov 16 olle 731     {
4227 10 Nov 16 olle 732       numDigits = projectConfig.getLibraryPreparationKitItemNumDigits();
4227 10 Nov 16 olle 733     }
4227 10 Nov 16 olle 734
4227 10 Nov 16 olle 735     return numDigits;
4227 10 Nov 16 olle 736 }
4227 10 Nov 16 olle 737
4227 10 Nov 16 olle 738   /**
4716 26 Mar 18 olle 739     Returns uses site prefix flag for the desired project.
4716 26 Mar 18 olle 740     Returns `null` if no uses site prefix flag has been
4716 26 Mar 18 olle 741     configured for the project.
4716 26 Mar 18 olle 742
4716 26 Mar 18 olle 743     @param activeProjectId int ID for the project to get uses site prefix flag for.
4716 26 Mar 18 olle 744     @return Boolean Uses site prefix flag for the desired project.
4716 26 Mar 18 olle 745    */
4716 26 Mar 18 olle 746   public static Boolean fetchUsesSitePrefix(int activeProjectId)
4716 26 Mar 18 olle 747   {
4716 26 Mar 18 olle 748     Boolean usesSitePrefix = null;
4716 26 Mar 18 olle 749     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4716 26 Mar 18 olle 750     if (projectConfig != null)
4716 26 Mar 18 olle 751     {
4716 26 Mar 18 olle 752       usesSitePrefix = projectConfig.getUsesSitePrefix();
4716 26 Mar 18 olle 753     }
4716 26 Mar 18 olle 754
4716 26 Mar 18 olle 755     return usesSitePrefix;
4716 26 Mar 18 olle 756   }
4716 26 Mar 18 olle 757
4716 26 Mar 18 olle 758   /**
4722 29 Mar 18 olle 759     Returns site prefix hash list for the desired project.
4722 29 Mar 18 olle 760     Returns `null` if no site prefix hash list has been
4722 29 Mar 18 olle 761     configured for the project.
4722 29 Mar 18 olle 762
4722 29 Mar 18 olle 763     @param activeProjectId int ID for the project to get site prefix hash list for.
4722 29 Mar 18 olle 764     @return String Site prefix hash list for the desired project.
4722 29 Mar 18 olle 765    */
4722 29 Mar 18 olle 766   public static String fetchSitePrefixHashList(int activeProjectId)
4722 29 Mar 18 olle 767   {
4722 29 Mar 18 olle 768     String sitePrefixHashList = null;
4722 29 Mar 18 olle 769     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4722 29 Mar 18 olle 770     if (projectConfig != null)
4722 29 Mar 18 olle 771     {
4722 29 Mar 18 olle 772       sitePrefixHashList = projectConfig.getSitePrefixHashList();
4722 29 Mar 18 olle 773     }
4722 29 Mar 18 olle 774
4722 29 Mar 18 olle 775     return sitePrefixHashList;
4722 29 Mar 18 olle 776   }
4722 29 Mar 18 olle 777
4722 29 Mar 18 olle 778   /**
5271 29 Jan 19 olle 779     Returns site prefix hash list for the desired project.
5271 29 Jan 19 olle 780     Returns `null` if no site prefix hash list has been
5271 29 Jan 19 olle 781     configured for the project.
5271 29 Jan 19 olle 782
5271 29 Jan 19 olle 783     @param projectName String Name of the project to get site prefix hash list for.
5271 29 Jan 19 olle 784     @return String Site prefix hash list for the desired project.
5271 29 Jan 19 olle 785   */
5271 29 Jan 19 olle 786   public static String fetchSitePrefixHashList(String projectName)
5271 29 Jan 19 olle 787   {
5271 29 Jan 19 olle 788     String sitePrefixHashList = null;
5271 29 Jan 19 olle 789     ProjectConfig projectConfig = fetchProjectConfig(projectName);
5271 29 Jan 19 olle 790     if (projectConfig != null)
5271 29 Jan 19 olle 791     {
5271 29 Jan 19 olle 792       sitePrefixHashList = projectConfig.getSitePrefixHashList();
5271 29 Jan 19 olle 793     }
5271 29 Jan 19 olle 794
5271 29 Jan 19 olle 795     return sitePrefixHashList;
5271 29 Jan 19 olle 796   }
5271 29 Jan 19 olle 797
5271 29 Jan 19 olle 798   /**
4722 29 Mar 18 olle 799     Returns site prefix/site key hash map for the desired project.
4722 29 Mar 18 olle 800     Returns `null` if no site prefix/site key hash map has been
4722 29 Mar 18 olle 801     configured for the project.
4722 29 Mar 18 olle 802
4722 29 Mar 18 olle 803     @param activeProjectId int ID for the project to get site prefix/site key hash map for.
4722 29 Mar 18 olle 804     @return HashMap<String,String> Site prefix/site key hash map for the desired project.
4722 29 Mar 18 olle 805    */
4722 29 Mar 18 olle 806   public static HashMap<String,String> fetchSitePrefixSiteKeyHashMap(int activeProjectId)
4722 29 Mar 18 olle 807   {
4722 29 Mar 18 olle 808     if (sitePrefixSiteKeyHashMap == null)
4722 29 Mar 18 olle 809     {
4722 29 Mar 18 olle 810       String sitePrefixHashList = fetchSitePrefixHashList(activeProjectId);
4722 29 Mar 18 olle 811       System.out.println("Meludi::fetchSitePrefixSiteKeyHashMap(): sitePrefixHashList = " + sitePrefixHashList);
4722 29 Mar 18 olle 812       if (sitePrefixHashList != null && !sitePrefixHashList.equals(""))
4722 29 Mar 18 olle 813       {
4722 29 Mar 18 olle 814         sitePrefixSiteKeyHashMap = new HashMap<String,String>();
4722 29 Mar 18 olle 815         String[] sitePrefixHashListArray = sitePrefixHashList.split(",");
4722 29 Mar 18 olle 816         int sitePrefixHashLength = sitePrefixHashListArray.length;
4722 29 Mar 18 olle 817         int sitePrefixLength = sitePrefixHashLength/2;
4722 29 Mar 18 olle 818         System.out.println("Meludi::fetchSitePrefixSiteKeyHashMap(): sitePrefixHashLength = " + sitePrefixHashLength);
4722 29 Mar 18 olle 819         for (int i = 0; i < sitePrefixLength; i++)
4722 29 Mar 18 olle 820         {
4722 29 Mar 18 olle 821           String sitePrefix = sitePrefixHashListArray[2*i];
4722 29 Mar 18 olle 822           String siteKey = sitePrefixHashListArray[2*i + 1];
4722 29 Mar 18 olle 823           sitePrefixSiteKeyHashMap.put(sitePrefix, siteKey);
4722 29 Mar 18 olle 824         }
4722 29 Mar 18 olle 825         System.out.println("Meludi::fetchSitePrefixSiteKeyHashMap(): sitePrefixSiteKeyHashMap = " + sitePrefixSiteKeyHashMap);
4722 29 Mar 18 olle 826       }
4722 29 Mar 18 olle 827     }
4722 29 Mar 18 olle 828
4722 29 Mar 18 olle 829     return sitePrefixSiteKeyHashMap;
4722 29 Mar 18 olle 830   }
4722 29 Mar 18 olle 831
4722 29 Mar 18 olle 832   /**
4722 29 Mar 18 olle 833     Returns site key/site prefix hash map for the desired project.
4722 29 Mar 18 olle 834     Returns `null` if no site key/site prefix hash map has been
4722 29 Mar 18 olle 835     configured for the project.
4722 29 Mar 18 olle 836
4722 29 Mar 18 olle 837     @param activeProjectId int ID for the project to get site key/site prefix hash map for.
4722 29 Mar 18 olle 838     @return HashMap<String,String> Site key/site prefix hash map for the desired project.
4722 29 Mar 18 olle 839    */
4722 29 Mar 18 olle 840   public static HashMap<String,String> fetchSiteKeySitePrefixHashMap(int activeProjectId)
4722 29 Mar 18 olle 841   {
4722 29 Mar 18 olle 842     if (siteKeySitePrefixHashMap == null)
4722 29 Mar 18 olle 843     {
5271 29 Jan 19 olle 844       String projectName = fetchProjectName(activeProjectId);
5271 29 Jan 19 olle 845       siteKeySitePrefixHashMap = fetchSiteKeySitePrefixHashMap(projectName);
5271 29 Jan 19 olle 846     }
5271 29 Jan 19 olle 847
5271 29 Jan 19 olle 848     return siteKeySitePrefixHashMap;
5271 29 Jan 19 olle 849   }
5271 29 Jan 19 olle 850
5271 29 Jan 19 olle 851   /**
5271 29 Jan 19 olle 852     Returns site key/site prefix hash map for the desired project.
5271 29 Jan 19 olle 853     Returns `null` if no site key/site prefix hash map has been
5271 29 Jan 19 olle 854     configured for the project.
5271 29 Jan 19 olle 855
5271 29 Jan 19 olle 856     @param projectName String Name of the project to get site key/site prefix hash map for.
5271 29 Jan 19 olle 857     @return HashMap<String,String> Site key/site prefix hash map for the desired project.
5271 29 Jan 19 olle 858   */
5271 29 Jan 19 olle 859   public static HashMap<String,String> fetchSiteKeySitePrefixHashMap(String projectName)
5271 29 Jan 19 olle 860   {
5271 29 Jan 19 olle 861     if (siteKeySitePrefixHashMap == null)
5271 29 Jan 19 olle 862     {
5271 29 Jan 19 olle 863       String sitePrefixHashList = fetchSitePrefixHashList(projectName);
4722 29 Mar 18 olle 864       System.out.println("Meludi::fetchSiteKeySitePrefixKeyHashMap(): sitePrefixHashList = " + sitePrefixHashList);
4722 29 Mar 18 olle 865       if (sitePrefixHashList != null && !sitePrefixHashList.equals(""))
4722 29 Mar 18 olle 866       {
4722 29 Mar 18 olle 867         siteKeySitePrefixHashMap = new HashMap<String,String>();
4722 29 Mar 18 olle 868         String[] sitePrefixHashListArray = sitePrefixHashList.split(",");
4722 29 Mar 18 olle 869         int sitePrefixHashLength = sitePrefixHashListArray.length;
4722 29 Mar 18 olle 870         int sitePrefixLength = sitePrefixHashLength/2;
4722 29 Mar 18 olle 871         System.out.println("Meludi::fetchSiteKeySitePrefixHashMap(): sitePrefixHashLength = " + sitePrefixHashLength);
4722 29 Mar 18 olle 872         for (int i = 0; i < sitePrefixLength; i++)
4722 29 Mar 18 olle 873         {
4722 29 Mar 18 olle 874           String sitePrefix = sitePrefixHashListArray[2*i];
4722 29 Mar 18 olle 875           String siteKey = sitePrefixHashListArray[2*i + 1];
4722 29 Mar 18 olle 876           siteKeySitePrefixHashMap.put(siteKey, sitePrefix);
4722 29 Mar 18 olle 877         }
4722 29 Mar 18 olle 878         System.out.println("Meludi::fetchSiteKeySitePrefixHashMap(): siteKeySitePrefixHashMap = " + siteKeySitePrefixHashMap);
4722 29 Mar 18 olle 879       }
4722 29 Mar 18 olle 880     }
4722 29 Mar 18 olle 881
4722 29 Mar 18 olle 882     return siteKeySitePrefixHashMap;
4722 29 Mar 18 olle 883   }
4722 29 Mar 18 olle 884
4722 29 Mar 18 olle 885   /**
4807 15 May 18 olle 886     Returns uses referral creation flag for the desired project.
4807 15 May 18 olle 887     Returns `null` if no uses referral creation flag has been
4807 15 May 18 olle 888     configured for the project.
4807 15 May 18 olle 889
4807 15 May 18 olle 890     @param activeProjectId int ID for the project to get uses referral creation flag for.
4807 15 May 18 olle 891     @return Boolean Uses referral creation flag for the desired project.
4807 15 May 18 olle 892   */
4807 15 May 18 olle 893   public static Boolean fetchUsesReferralCreation(int activeProjectId)
4807 15 May 18 olle 894   {
4807 15 May 18 olle 895     Boolean usesReferralCreation = null;
4807 15 May 18 olle 896     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4807 15 May 18 olle 897     if (projectConfig != null)
4807 15 May 18 olle 898     {
4807 15 May 18 olle 899       usesReferralCreation = projectConfig.getUsesReferralCreation();
4807 15 May 18 olle 900     }
4807 15 May 18 olle 901
4807 15 May 18 olle 902     return usesReferralCreation;
4807 15 May 18 olle 903   }
4807 15 May 18 olle 904
4807 15 May 18 olle 905   /**
4807 15 May 18 olle 906     Returns uses site specific referral templates flag for the desired project.
4807 15 May 18 olle 907     Returns `null` if no uses site specific referral templates flag has been
4807 15 May 18 olle 908     configured for the project.
4807 15 May 18 olle 909
4807 15 May 18 olle 910     @param activeProjectId int ID for the project to get uses site specific referral templates flag for.
4807 15 May 18 olle 911     @return Boolean Uses site specific referral templates flag for the desired project.
4807 15 May 18 olle 912   */
4807 15 May 18 olle 913   public static Boolean fetchUsesSiteSpecificReferralTemplates(int activeProjectId)
4807 15 May 18 olle 914   {
4807 15 May 18 olle 915     Boolean usesSiteSpecificReferralTemplates = null;
4807 15 May 18 olle 916     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4807 15 May 18 olle 917     if (projectConfig != null)
4807 15 May 18 olle 918     {
4807 15 May 18 olle 919       usesSiteSpecificReferralTemplates = projectConfig.getUsesSiteSpecificReferralTemplates();
4807 15 May 18 olle 920     }
4807 15 May 18 olle 921
4807 15 May 18 olle 922     return usesSiteSpecificReferralTemplates;
4807 15 May 18 olle 923   }
4807 15 May 18 olle 924
4807 15 May 18 olle 925   /**
4176 25 Oct 16 olle 926     Returns project focus default for the desired project.
4176 25 Oct 16 olle 927     Returns `null` if no project focus default has been
4176 25 Oct 16 olle 928     configured for the project.
4176 25 Oct 16 olle 929
4176 25 Oct 16 olle 930     @param activeProjectId int ID for the project to get project focus default for.
4176 25 Oct 16 olle 931     @return String Project focus default for the desired project.
4176 25 Oct 16 olle 932   */
4160 18 Oct 16 olle 933   public static String fetchProjectFocusDefault(int activeProjectId)
4160 18 Oct 16 olle 934   {
4176 25 Oct 16 olle 935     String projectFocusDefault = null;
4160 18 Oct 16 olle 936     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4176 25 Oct 16 olle 937     if (projectConfig != null)
4176 25 Oct 16 olle 938     {
4176 25 Oct 16 olle 939       projectFocusDefault = projectConfig.getProjectFocusDefault();
4176 25 Oct 16 olle 940     }
4160 18 Oct 16 olle 941
4160 18 Oct 16 olle 942     return projectFocusDefault;
4160 18 Oct 16 olle 943   }
4160 18 Oct 16 olle 944
4176 25 Oct 16 olle 945   /**
4176 25 Oct 16 olle 946     Returns specimen type default for the desired project.
4176 25 Oct 16 olle 947     Returns `null` if no specimen type default has been
4176 25 Oct 16 olle 948     configured for the project.
4176 25 Oct 16 olle 949
4176 25 Oct 16 olle 950     @param activeProjectId int ID for the project to get specimen type default for.
4176 25 Oct 16 olle 951     @return String Specimen type default for the desired project.
4176 25 Oct 16 olle 952   */
4160 18 Oct 16 olle 953   public static String fetchSpecimenTypeDefault(int activeProjectId)
4160 18 Oct 16 olle 954   {
4176 25 Oct 16 olle 955     String specimenTypeDefault = null;
4160 18 Oct 16 olle 956     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4176 25 Oct 16 olle 957     if (projectConfig != null)
4176 25 Oct 16 olle 958     {
4176 25 Oct 16 olle 959       specimenTypeDefault = projectConfig.getSpecimenTypeDefault();
4176 25 Oct 16 olle 960     }
4160 18 Oct 16 olle 961
4160 18 Oct 16 olle 962     return specimenTypeDefault;
4160 18 Oct 16 olle 963   }
4160 18 Oct 16 olle 964
2933 14 Nov 14 olle 965   /**
4184 27 Oct 16 olle 966     Returns specimen input type default for the desired project.
4184 27 Oct 16 olle 967     Returns `null` if no specimen input type default has been
4184 27 Oct 16 olle 968     configured for the project.
4184 27 Oct 16 olle 969
4184 27 Oct 16 olle 970     @param activeProjectId int ID for the project to get specimen input type default for.
4184 27 Oct 16 olle 971     @return String Specimen input type default for the desired project.
4184 27 Oct 16 olle 972   */
4184 27 Oct 16 olle 973   public static String fetchSpecimenInputTypeDefault(int activeProjectId)
4184 27 Oct 16 olle 974   {
4184 27 Oct 16 olle 975     String specimenInputTypeDefault = null;
4184 27 Oct 16 olle 976     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4184 27 Oct 16 olle 977     if (projectConfig != null)
4184 27 Oct 16 olle 978     {
4184 27 Oct 16 olle 979       specimenInputTypeDefault = projectConfig.getSpecimenInputTypeDefault();
4184 27 Oct 16 olle 980     }
4184 27 Oct 16 olle 981
4184 27 Oct 16 olle 982     return specimenInputTypeDefault;
4184 27 Oct 16 olle 983   }
4184 27 Oct 16 olle 984
4184 27 Oct 16 olle 985   /**
4248 22 Nov 16 olle 986     Returns extracts from first specimen only default for the desired project.
4248 22 Nov 16 olle 987     Returns `null` if no extracts from first specimen only default has been
4248 22 Nov 16 olle 988     configured for the project.
4248 22 Nov 16 olle 989
4248 22 Nov 16 olle 990     @param activeProjectId int ID for the project to get extracts from first specimen only default for.
4248 22 Nov 16 olle 991     @return Boolean Extracts for first specimen only default for the desired project.
4248 22 Nov 16 olle 992   */
4248 22 Nov 16 olle 993   public static Boolean fetchExtractsFromFirstSpecimenOnlyDefault(int activeProjectId)
4248 22 Nov 16 olle 994   {
4248 22 Nov 16 olle 995     Boolean extractsFromFirstSpecimenOnlyDefault = null;
4248 22 Nov 16 olle 996     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4248 22 Nov 16 olle 997     if (projectConfig != null)
4248 22 Nov 16 olle 998     {
4248 22 Nov 16 olle 999       extractsFromFirstSpecimenOnlyDefault = projectConfig.getExtractsFromFirstSpecimenOnlyDefault();
4248 22 Nov 16 olle 1000     }
4248 22 Nov 16 olle 1001
4248 22 Nov 16 olle 1002     return extractsFromFirstSpecimenOnlyDefault;
4248 22 Nov 16 olle 1003   }
4248 22 Nov 16 olle 1004
4248 22 Nov 16 olle 1005   /**
4249 23 Nov 16 olle 1006     Returns deparaffinization RNA volume (µl) default for the desired project.
4249 23 Nov 16 olle 1007     Returns `null` if no deparaffinization RNA volume (µl) default has been
4249 23 Nov 16 olle 1008     configured for the project.
4249 23 Nov 16 olle 1009
4249 23 Nov 16 olle 1010     @param activeProjectId int ID for the project to get deparaffinization RNA volume (µl) default for.
4249 23 Nov 16 olle 1011     @return Float Deparaffinization RNA volume (µl) default for the desired project.
4249 23 Nov 16 olle 1012   */
4249 23 Nov 16 olle 1013   public static Float fetchDeparaffinizationRnaVolumeMicroLitersDefault(int activeProjectId)
4249 23 Nov 16 olle 1014   {
4249 23 Nov 16 olle 1015     Float deparaffinizationRnaVolumeMicroLitersDefault = null;
4249 23 Nov 16 olle 1016     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4249 23 Nov 16 olle 1017     if (projectConfig != null)
4249 23 Nov 16 olle 1018     {
4249 23 Nov 16 olle 1019       deparaffinizationRnaVolumeMicroLitersDefault = projectConfig.getDeparaffinizationRnaVolumeMicroLitersDefault();
4249 23 Nov 16 olle 1020     }
4249 23 Nov 16 olle 1021
4249 23 Nov 16 olle 1022     return deparaffinizationRnaVolumeMicroLitersDefault;
4249 23 Nov 16 olle 1023   }
4249 23 Nov 16 olle 1024
4249 23 Nov 16 olle 1025   /**
4249 23 Nov 16 olle 1026     Returns deparaffinization DNA volume (µl) default for the desired project.
4249 23 Nov 16 olle 1027     Returns `null` if no deparaffinization DNA volume (µl) default has been
4249 23 Nov 16 olle 1028     configured for the project.
4249 23 Nov 16 olle 1029
4249 23 Nov 16 olle 1030     @param activeProjectId int ID for the project to get deparaffinization DNA volume (µl) default for.
4249 23 Nov 16 olle 1031     @return Float Deparaffinization DNA volume (µl) default for the desired project.
4249 23 Nov 16 olle 1032   */
4249 23 Nov 16 olle 1033   public static Float fetchDeparaffinizationDnaVolumeMicroLitersDefault(int activeProjectId)
4249 23 Nov 16 olle 1034   {
4249 23 Nov 16 olle 1035     Float deparaffinizationDnaVolumeMicroLitersDefault = null;
4249 23 Nov 16 olle 1036     ProjectConfig projectConfig = fetchProjectConfig(activeProjectId);
4249 23 Nov 16 olle 1037     if (projectConfig != null)
4249 23 Nov 16 olle 1038     {
4249 23 Nov 16 olle 1039       deparaffinizationDnaVolumeMicroLitersDefault = projectConfig.getDeparaffinizationDnaVolumeMicroLitersDefault();
4249 23 Nov 16 olle 1040     }
4249 23 Nov 16 olle 1041
4249 23 Nov 16 olle 1042     return deparaffinizationDnaVolumeMicroLitersDefault;
4249 23 Nov 16 olle 1043   }
4249 23 Nov 16 olle 1044
4249 23 Nov 16 olle 1045   /**
4142 30 Sep 16 olle 1046     Returns regular expression pattern that can be match against a string
4142 30 Sep 16 olle 1047     to see if it contains a case name, e.g. it starts with the sample name
4216 09 Nov 16 olle 1048     prefix followed by a number of digits, optionally followed by a dot.
4142 30 Sep 16 olle 1049     @since 1.5
4142 30 Sep 16 olle 1050     
4142 30 Sep 16 olle 1051     @param activeProjectId int ID value of project to get case name pattern for.
4142 30 Sep 16 olle 1052     @return Pattern A regular expression pattern for case names.
3431 29 Jun 15 olle 1053   */
4142 30 Sep 16 olle 1054   public static Pattern fetchCaseNamePattern(int activeProjectId)
4142 30 Sep 16 olle 1055   {
4142 30 Sep 16 olle 1056     String sampleItemPrefix = fetchSampleItemPrefix(activeProjectId);
4216 09 Nov 16 olle 1057     Integer sampleItemNumDigits = fetchSampleItemNumDigits(activeProjectId);
4216 09 Nov 16 olle 1058     String patternString = sampleItemPrefix + "\\d{" + sampleItemNumDigits + "}(\\..*)?";
4142 30 Sep 16 olle 1059     Pattern caseNamePattern = Pattern.compile(patternString);
4142 30 Sep 16 olle 1060     return caseNamePattern;
4142 30 Sep 16 olle 1061   }
3431 29 Jun 15 olle 1062
3431 29 Jun 15 olle 1063   /**
4142 30 Sep 16 olle 1064      Obtains root item name by removing part of name after
4216 09 Nov 16 olle 1065      prefix + digit characters.
4142 30 Sep 16 olle 1066      
4142 30 Sep 16 olle 1067      @param itemName String The item name to find root item name for.
4142 30 Sep 16 olle 1068      @param activeProjectId int ID value of project to get case name pattern for.
4142 30 Sep 16 olle 1069      @return String The root item name.
2933 14 Nov 14 olle 1070   */
4142 30 Sep 16 olle 1071   public static String fetchRootItemName(String itemName, int activeProjectId)
4142 30 Sep 16 olle 1072   {
4142 30 Sep 16 olle 1073     String rootItemName = "" + itemName;
4142 30 Sep 16 olle 1074     String sampleItemPrefix = fetchSampleItemPrefix(activeProjectId);
4216 09 Nov 16 olle 1075     Integer sampleItemNumDigits = fetchSampleItemNumDigits(activeProjectId);
4216 09 Nov 16 olle 1076     int stdNameLength = sampleItemPrefix.length() + sampleItemNumDigits;
4142 30 Sep 16 olle 1077     if (rootItemName.length() > stdNameLength)
4142 30 Sep 16 olle 1078     {
4142 30 Sep 16 olle 1079       rootItemName = rootItemName.substring(0, stdNameLength);
4142 30 Sep 16 olle 1080     }
4142 30 Sep 16 olle 1081     return rootItemName;
4142 30 Sep 16 olle 1082   }
2933 14 Nov 14 olle 1083
4716 26 Mar 18 olle 1084   /**
4716 26 Mar 18 olle 1085      Obtains root digits by removing item prefix and part of name after
4716 26 Mar 18 olle 1086      prefix + digit characters.
4716 26 Mar 18 olle 1087    
4716 26 Mar 18 olle 1088      @param itemName String The item name to find root item digits for.
4716 26 Mar 18 olle 1089      @param activeProjectId int ID value of project to get case name pattern for.
4716 26 Mar 18 olle 1090      @return String The root item digits.
4716 26 Mar 18 olle 1091    */
4716 26 Mar 18 olle 1092   public static String fetchRootDigits(String itemName, int activeProjectId)
4716 26 Mar 18 olle 1093   {
4716 26 Mar 18 olle 1094     String rootItemDigits = fetchRootItemName(itemName, activeProjectId);
4716 26 Mar 18 olle 1095     String sampleItemPrefix = fetchSampleItemPrefix(activeProjectId);
4716 26 Mar 18 olle 1096     int itemPrefixLength = sampleItemPrefix.length();
4716 26 Mar 18 olle 1097     if (rootItemDigits.length() > itemPrefixLength)
4716 26 Mar 18 olle 1098     {
4716 26 Mar 18 olle 1099       rootItemDigits = rootItemDigits.substring(itemPrefixLength, rootItemDigits.length());
4716 26 Mar 18 olle 1100     }
4716 26 Mar 18 olle 1101     return rootItemDigits;
4716 26 Mar 18 olle 1102   }
4716 26 Mar 18 olle 1103
4865 20 Jun 18 olle 1104   /**
4904 11 Jul 18 olle 1105      Obtains root item name by removing part of name after
4904 11 Jul 18 olle 1106      prefix + digit characters.
4904 11 Jul 18 olle 1107    
4904 11 Jul 18 olle 1108      @param itemName String The item name to find root item name for.
4904 11 Jul 18 olle 1109      @param subtype Subtype The subtype for the item to find root item name for.
4904 11 Jul 18 olle 1110      @param activeProjectId int ID value of project to get case name pattern for.
4904 11 Jul 18 olle 1111      @return String The root item name.
4904 11 Jul 18 olle 1112   */
4904 11 Jul 18 olle 1113   public static String fetchRootItemName(String itemName, Subtype subtype, int activeProjectId)
4904 11 Jul 18 olle 1114   {
4904 11 Jul 18 olle 1115     String rootItemName = "" + itemName;
4904 11 Jul 18 olle 1116     String prefix = fetchItemPrefix(subtype, activeProjectId);
4904 11 Jul 18 olle 1117     Integer numDigits = fetchItemNumDigits(subtype, activeProjectId);
4904 11 Jul 18 olle 1118     int stdNameLength = prefix.length() + numDigits;
4904 11 Jul 18 olle 1119     if (rootItemName.length() > stdNameLength)
4904 11 Jul 18 olle 1120     {
4904 11 Jul 18 olle 1121       rootItemName = rootItemName.substring(0, stdNameLength);
4904 11 Jul 18 olle 1122     }
4904 11 Jul 18 olle 1123     return rootItemName;
4904 11 Jul 18 olle 1124   }
4904 11 Jul 18 olle 1125
4904 11 Jul 18 olle 1126   /**
4904 11 Jul 18 olle 1127     Obtains root digits by removing item prefix and part of name after
4904 11 Jul 18 olle 1128     prefix + digit characters.
4904 11 Jul 18 olle 1129   
4904 11 Jul 18 olle 1130     @param itemName String The item name to find root item digits for.
4904 11 Jul 18 olle 1131      @param subtype Subtype The subtype for the item to find root item name for.
4904 11 Jul 18 olle 1132     @param activeProjectId int ID value of project to get case name pattern for.
4904 11 Jul 18 olle 1133     @return String The root item digits.
4904 11 Jul 18 olle 1134    */
4904 11 Jul 18 olle 1135   public static String fetchRootDigits(String itemName, Subtype subtype, int activeProjectId)
4904 11 Jul 18 olle 1136   {
4904 11 Jul 18 olle 1137     String rootItemDigits = fetchRootItemName(itemName, subtype, activeProjectId);
4904 11 Jul 18 olle 1138     String prefix = fetchItemPrefix(subtype, activeProjectId);
4904 11 Jul 18 olle 1139     int itemPrefixLength = prefix.length();
4904 11 Jul 18 olle 1140     if (rootItemDigits.length() > itemPrefixLength)
4904 11 Jul 18 olle 1141     {
4904 11 Jul 18 olle 1142       rootItemDigits = rootItemDigits.substring(itemPrefixLength, rootItemDigits.length());
4904 11 Jul 18 olle 1143     }
4904 11 Jul 18 olle 1144     return rootItemDigits;
4904 11 Jul 18 olle 1145   }
4904 11 Jul 18 olle 1146
4904 11 Jul 18 olle 1147   /**
4904 11 Jul 18 olle 1148     Obtains item prefix for a subtype. Returns `null` if
4904 11 Jul 18 olle 1149     no item prefix defined for the subtype.
4904 11 Jul 18 olle 1150   
4904 11 Jul 18 olle 1151     @param subtype Subtype The subtype to find item prefix for.
4904 11 Jul 18 olle 1152     @param activeProjectId int ID value of project to get item prefix for.
4904 11 Jul 18 olle 1153     @return String The item prefix.
4904 11 Jul 18 olle 1154   */
4904 11 Jul 18 olle 1155   public static String fetchItemPrefix(Subtype subtype, int activeProjectId)
4904 11 Jul 18 olle 1156   {
4904 11 Jul 18 olle 1157     String prefix = null;
4904 11 Jul 18 olle 1158     if (subtype == Subtype.CASE)
4904 11 Jul 18 olle 1159     {
4904 11 Jul 18 olle 1160       prefix = fetchSampleItemPrefix(activeProjectId);
4904 11 Jul 18 olle 1161     }
4904 11 Jul 18 olle 1162     else if (subtype == Subtype.BLOOD)
4904 11 Jul 18 olle 1163     {
4904 11 Jul 18 olle 1164       prefix = fetchBloodItemPrefix(activeProjectId);
4904 11 Jul 18 olle 1165     }
4904 11 Jul 18 olle 1166     else if (subtype == Subtype.FFPE_BLOCK)
4904 11 Jul 18 olle 1167     {
4904 11 Jul 18 olle 1168       prefix = fetchFfpeBlockItemPrefix(activeProjectId);
4904 11 Jul 18 olle 1169     }
4904 11 Jul 18 olle 1170     return prefix;
4904 11 Jul 18 olle 1171   }
4904 11 Jul 18 olle 1172
4904 11 Jul 18 olle 1173   /**
4904 11 Jul 18 olle 1174     Obtains item number of digits for a subtype. Returns `null` if
4904 11 Jul 18 olle 1175     no item number of digits defined for the subtype.
4904 11 Jul 18 olle 1176
4904 11 Jul 18 olle 1177     @param subtype Subtype The subtype to find item number of digits for.
4904 11 Jul 18 olle 1178     @param activeProjectId int ID value of project to get item number of digits for.
4904 11 Jul 18 olle 1179     @return Integer The item number of digits.
4904 11 Jul 18 olle 1180   */
4904 11 Jul 18 olle 1181   public static Integer fetchItemNumDigits(Subtype subtype, int activeProjectId)
4904 11 Jul 18 olle 1182   {
4904 11 Jul 18 olle 1183     Integer numDigits = null;
4904 11 Jul 18 olle 1184     if (subtype == Subtype.CASE)
4904 11 Jul 18 olle 1185     {
4904 11 Jul 18 olle 1186       numDigits = fetchSampleItemNumDigits(activeProjectId);
4904 11 Jul 18 olle 1187     }
4904 11 Jul 18 olle 1188     else if (subtype == Subtype.BLOOD)
4904 11 Jul 18 olle 1189     {
4904 11 Jul 18 olle 1190       numDigits = fetchBloodItemNumDigits(activeProjectId);
4904 11 Jul 18 olle 1191     }
4904 11 Jul 18 olle 1192     else if (subtype == Subtype.FFPE_BLOCK)
4904 11 Jul 18 olle 1193     {
4904 11 Jul 18 olle 1194       numDigits = fetchFfpeBlockItemNumDigits(activeProjectId);
4904 11 Jul 18 olle 1195     }
4904 11 Jul 18 olle 1196     return numDigits;
4904 11 Jul 18 olle 1197   }
4904 11 Jul 18 olle 1198
4904 11 Jul 18 olle 1199   /**
4904 11 Jul 18 olle 1200     Obtains main subtype for an item from the item name.
4904 11 Jul 18 olle 1201     Returns `null` if no main subtype can be found for the
4904 11 Jul 18 olle 1202     item name. The "main" subtype is the one corresponding
4904 11 Jul 18 olle 1203     to the item name prefix, while a suffix might indicate
4904 11 Jul 18 olle 1204     another subtype, e.g. for DNA the main subtype might be
4904 11 Jul 18 olle 1205     Subtype.CASE and the item subtype Subtype.EXTRACT.
4904 11 Jul 18 olle 1206
4904 11 Jul 18 olle 1207     @param itemName String The item name to get main subtype for.
4904 11 Jul 18 olle 1208     @param activeProjectId int ID value of project to get main subtype for.
4904 11 Jul 18 olle 1209     @return Subtype The main subtype for the item name.
4904 11 Jul 18 olle 1210   */
4904 11 Jul 18 olle 1211   public static Subtype fetchMainSubtypeFromItemName(String itemName, int activeProjectId)
4904 11 Jul 18 olle 1212   {
4904 11 Jul 18 olle 1213     Subtype subtype = null;
4904 11 Jul 18 olle 1214     if (itemName == null)
4904 11 Jul 18 olle 1215     {
4904 11 Jul 18 olle 1216       return subtype;
4904 11 Jul 18 olle 1217     }
4904 11 Jul 18 olle 1218     if (itemName.startsWith(Meludi.fetchSampleItemPrefix(activeProjectId)))
4904 11 Jul 18 olle 1219     {
4904 11 Jul 18 olle 1220       subtype = Subtype.CASE;
4904 11 Jul 18 olle 1221     }
4904 11 Jul 18 olle 1222     else if (itemName.startsWith(Meludi.fetchBloodItemPrefix(activeProjectId)))
4904 11 Jul 18 olle 1223     {
4904 11 Jul 18 olle 1224       subtype = Subtype.BLOOD;
4904 11 Jul 18 olle 1225     }
4904 11 Jul 18 olle 1226     else if (itemName.startsWith(Meludi.fetchFfpeBlockItemPrefix(activeProjectId)))
4904 11 Jul 18 olle 1227     {
4904 11 Jul 18 olle 1228       subtype = Subtype.FFPE_BLOCK;
4904 11 Jul 18 olle 1229     }
4904 11 Jul 18 olle 1230     return subtype;
4904 11 Jul 18 olle 1231   }
4904 11 Jul 18 olle 1232
4904 11 Jul 18 olle 1233   /**
4865 20 Jun 18 olle 1234     Obtains referral digits by removing referral prefix.
4865 20 Jun 18 olle 1235
4865 20 Jun 18 olle 1236     @param referralId String The referral ID to find digits for.
4865 20 Jun 18 olle 1237     @param activeProjectId int ID value of project to get case name pattern for.
4865 20 Jun 18 olle 1238     @return String The referral digits.
4865 20 Jun 18 olle 1239   */
4865 20 Jun 18 olle 1240   public static String fetchReferralDigits(String referralId, int activeProjectId)
4865 20 Jun 18 olle 1241   {
4865 20 Jun 18 olle 1242     String referralDigits = "" + referralId;
4865 20 Jun 18 olle 1243     String referralPrefix = Meludi.fetchReferralPrefix(activeProjectId);
4865 20 Jun 18 olle 1244     int referralPrefixLength = referralPrefix.length();
4865 20 Jun 18 olle 1245     if (referralId.length() > referralPrefixLength)
4865 20 Jun 18 olle 1246     {
4865 20 Jun 18 olle 1247       referralDigits = referralDigits.substring(referralPrefixLength, referralDigits.length());
4865 20 Jun 18 olle 1248     }
4865 20 Jun 18 olle 1249     return referralDigits;
4865 20 Jun 18 olle 1250   }
4865 20 Jun 18 olle 1251
4132 27 Sep 16 olle 1252   private static volatile XmlConfig config;
4132 27 Sep 16 olle 1253   
4132 27 Sep 16 olle 1254   /**
4132 27 Sep 16 olle 1255     Get MeLuDI configuration.
4132 27 Sep 16 olle 1256   */
4132 27 Sep 16 olle 1257   public static XmlConfig getConfig()
4132 27 Sep 16 olle 1258   {
4132 27 Sep 16 olle 1259     if (config == null) loadConfig(false);
4132 27 Sep 16 olle 1260     return config;
4132 27 Sep 16 olle 1261   }
4132 27 Sep 16 olle 1262   
4132 27 Sep 16 olle 1263   /**
4132 27 Sep 16 olle 1264     Force reload the MeLuDI configuration.
4132 27 Sep 16 olle 1265   */
4132 27 Sep 16 olle 1266   public static synchronized XmlConfig reloadConfig()
4132 27 Sep 16 olle 1267   {
4132 27 Sep 16 olle 1268     loadConfig(true);
4132 27 Sep 16 olle 1269     return config;
4132 27 Sep 16 olle 1270   }
4132 27 Sep 16 olle 1271   
4132 27 Sep 16 olle 1272   public static synchronized void unloadConfig()
4132 27 Sep 16 olle 1273   {
4132 27 Sep 16 olle 1274     config = null;
4132 27 Sep 16 olle 1275   }
4132 27 Sep 16 olle 1276   
4132 27 Sep 16 olle 1277   private static synchronized void loadConfig(boolean force)
4132 27 Sep 16 olle 1278   {
4132 27 Sep 16 olle 1279     URL configUrl = Meludi.class.getResource("/meludi-config.xml");
4132 27 Sep 16 olle 1280     if (config != null && !force) return;
4132 27 Sep 16 olle 1281     InputStream is = null;
4132 27 Sep 16 olle 1282     try
4132 27 Sep 16 olle 1283     {
4132 27 Sep 16 olle 1284       logger.debug("Loading configuration from; " + configUrl);
4132 27 Sep 16 olle 1285       is = configUrl == null ? null : configUrl.openStream();
4132 27 Sep 16 olle 1286       if (is == null)
4132 27 Sep 16 olle 1287       {
4132 27 Sep 16 olle 1288         throw new ConfigurationException("Can't find the configuration file. " +
4132 27 Sep 16 olle 1289             "Make sure 'meludi-config.xml' is in the CLASSPATH.");
4132 27 Sep 16 olle 1290       }
4132 27 Sep 16 olle 1291       
4132 27 Sep 16 olle 1292       Document dom = XmlUtil2.getValidatedXml(configUrl, null);
4132 27 Sep 16 olle 1293       config = new XmlConfig(dom.getRootElement(), "[meludi]");
4132 27 Sep 16 olle 1294     }
4132 27 Sep 16 olle 1295     catch (Exception ex)
4132 27 Sep 16 olle 1296     {
4132 27 Sep 16 olle 1297       logger.error("Could not load configuration", ex);
4132 27 Sep 16 olle 1298       throw new RuntimeException(ex);
4132 27 Sep 16 olle 1299     }
4132 27 Sep 16 olle 1300     finally
4132 27 Sep 16 olle 1301     {
4132 27 Sep 16 olle 1302       FileUtil.close(is);
4132 27 Sep 16 olle 1303     }
4132 27 Sep 16 olle 1304   }
4706 19 Mar 18 olle 1305
4706 19 Mar 18 olle 1306   /**
4706 19 Mar 18 olle 1307     Get an existing session control for MeLuDI and check that a user is logged in.
4706 19 Mar 18 olle 1308     @since 1.6.0
4706 19 Mar 18 olle 1309    */
4706 19 Mar 18 olle 1310   public static SessionControl getSessionControl(ServletRequest req)
4706 19 Mar 18 olle 1311   {
4706 19 Mar 18 olle 1312     return getSessionControl(req, ClientApp.MELUDI, true);
4706 19 Mar 18 olle 1313   }
4706 19 Mar 18 olle 1314
4706 19 Mar 18 olle 1315   /**
4706 19 Mar 18 olle 1316     Get an existing session control and optionally check that a user is logged in.
4706 19 Mar 18 olle 1317     @since 1.6.0
4706 19 Mar 18 olle 1318    */
4706 19 Mar 18 olle 1319
4706 19 Mar 18 olle 1320   public static SessionControl getSessionControl(ServletRequest req, ClientApp client, boolean checkLoggedIn)
4706 19 Mar 18 olle 1321   {
4706 19 Mar 18 olle 1322     //return Application.getSessionControl(req.getParameter("ID"), client.getExternalId(), req.getRemoteAddr(), checkLoggedIn);
5468 04 Jun 19 olle 1323     //return Application.getSessionControl(req.getParameter("ID"), client.getExternalId());
5468 04 Jun 19 olle 1324     //  public static SessionControl getSessionControl(String sessionControlId, String externalClientId, String remoteId, boolean checkLoggedIn)
5744 20 Nov 19 olle 1325     //return Application.getSessionControl(req.getParameter("ID"), client.getExternalId(), req.getRemoteAddr(), checkLoggedIn);
5744 20 Nov 19 olle 1326     return Application.getSessionControl(req.getParameter("ID"), null, req.getRemoteAddr(), checkLoggedIn);
4706 19 Mar 18 olle 1327   }
4706 19 Mar 18 olle 1328
3100 21 Jan 15 olle 1329   private static SessionControl rootSc;
4127 26 Sep 16 olle 1330
2933 14 Nov 14 olle 1331   /**
3100 21 Jan 15 olle 1332     Get a session control where the root user is logged in and the MeLuDi project
3100 21 Jan 15 olle 1333     is active. This method must be given a system session control given out by
3100 21 Jan 15 olle 1334     BASE when starting services. The service session control only has permission
3100 21 Jan 15 olle 1335     to impersonate another user. This method should be used whenever a service
3100 21 Jan 15 olle 1336     needs to access the database. Do not keep the returned session control for 
3100 21 Jan 15 olle 1337     a long time since it may be closed by other operations. 
4127 26 Sep 16 olle 1338   
3100 21 Jan 15 olle 1339     Services should  not close the returned session control after use, but
3100 21 Jan 15 olle 1340     should call {@link #closeRootSessionControl()} when they are stopped.
3100 21 Jan 15 olle 1341     @since 3.0
4127 26 Sep 16 olle 1342    */
3100 21 Jan 15 olle 1343   public static synchronized SessionControl getRootSessionControl(SessionControl systemSc)
3100 21 Jan 15 olle 1344   {
3100 21 Jan 15 olle 1345     if (rootSc == null || rootSc.isClosed())
3100 21 Jan 15 olle 1346     {
3100 21 Jan 15 olle 1347       SessionControl tmp = systemSc.impersonateLogin(SystemItems.getId(User.ROOT), "Meludi services");
3100 21 Jan 15 olle 1348       DbControl dc = tmp.newDbControl();
3100 21 Jan 15 olle 1349       try
3100 21 Jan 15 olle 1350       {
3100 21 Jan 15 olle 1351         ItemQuery<Project> q = Project.getQuery();
3100 21 Jan 15 olle 1352         q.setIncludes(Include.ALL);
3100 21 Jan 15 olle 1353         q.restrict(Restrictions.eq(Hql.property("name"), Expressions.string("MeLuDi")));
3100 21 Jan 15 olle 1354         List<Project> projects = q.list(dc);
3100 21 Jan 15 olle 1355         if (projects.size() == 1)
3100 21 Jan 15 olle 1356         {
3100 21 Jan 15 olle 1357           tmp.setActiveProject(projects.get(0));
3100 21 Jan 15 olle 1358         }
3100 21 Jan 15 olle 1359       }
3100 21 Jan 15 olle 1360       finally
3100 21 Jan 15 olle 1361       {
3100 21 Jan 15 olle 1362         if (dc != null) dc.close();
3100 21 Jan 15 olle 1363       }
4127 26 Sep 16 olle 1364     
3100 21 Jan 15 olle 1365       // Set active project
3100 21 Jan 15 olle 1366       rootSc = tmp;
3100 21 Jan 15 olle 1367     }
3100 21 Jan 15 olle 1368     else
3100 21 Jan 15 olle 1369     {
3100 21 Jan 15 olle 1370       rootSc.updateLastAccess();
3100 21 Jan 15 olle 1371     }
3100 21 Jan 15 olle 1372     return rootSc;
3100 21 Jan 15 olle 1373   }
4127 26 Sep 16 olle 1374
4127 26 Sep 16 olle 1375   /**
4127 26 Sep 16 olle 1376     Get a hash map of session controls where the root user is logged in
4127 26 Sep 16 olle 1377     and each of the projects is active, using project id values as keys.
4127 26 Sep 16 olle 1378     This method must be given a system session control given out by
4127 26 Sep 16 olle 1379     BASE when starting services. The service session control only has permission
4127 26 Sep 16 olle 1380     to impersonate another user. This method should be used whenever a service
4127 26 Sep 16 olle 1381     needs to access the database. Do not keep a returned session control for 
4127 26 Sep 16 olle 1382     a long time since it may be closed by other operations. 
4127 26 Sep 16 olle 1383     
4127 26 Sep 16 olle 1384     Services should  not close a returned session control after use, but
4127 26 Sep 16 olle 1385     should call {@link #closeRootSessionControl()} when they are stopped.
4127 26 Sep 16 olle 1386     @since MeLuDI 1.5.
4127 26 Sep 16 olle 1387     
4162 19 Oct 16 olle 1388     Side effects: Will update static HashMap<Integer,String> projectIdNameHM
4162 19 Oct 16 olle 1389     with project names for project ID values.
4162 19 Oct 16 olle 1390
4127 26 Sep 16 olle 1391     @param systemSc SessionControl A system session control given out by BASE when starting services.
4127 26 Sep 16 olle 1392     @return HashMap<Integer,SessionControl> A hash map of session controls, where each project is active, with project id as key.
4127 26 Sep 16 olle 1393   */
4127 26 Sep 16 olle 1394   public static synchronized HashMap<Integer,SessionControl> getRootSessionControlHM(SessionControl systemSc)
4127 26 Sep 16 olle 1395   {
4127 26 Sep 16 olle 1396     HashMap<Integer,SessionControl> projectIdScHM = new HashMap<Integer,SessionControl>();
4127 26 Sep 16 olle 1397     SessionControl tmp = systemSc.impersonateLogin(SystemItems.getId(User.ROOT), "Meludi services");
4127 26 Sep 16 olle 1398     DbControl dc = tmp.newDbControl();
4162 19 Oct 16 olle 1399     if (projectIdNameHM == null)
4162 19 Oct 16 olle 1400     {      
4162 19 Oct 16 olle 1401       projectIdNameHM = new HashMap<Integer,String>();
4162 19 Oct 16 olle 1402     }
4127 26 Sep 16 olle 1403     try
4127 26 Sep 16 olle 1404     {
4127 26 Sep 16 olle 1405       // Get list of projects
4127 26 Sep 16 olle 1406       ItemQuery<Project> q = Project.getQuery();
4127 26 Sep 16 olle 1407       q.setIncludes(Include.ALL);
4127 26 Sep 16 olle 1408       List<Project> projects = q.list(dc);
4127 26 Sep 16 olle 1409       // Get session control for each project as active, and store in hash map with project id as key
4127 26 Sep 16 olle 1410       for (int i = 0; i < projects.size(); i++)
4127 26 Sep 16 olle 1411       {
4127 26 Sep 16 olle 1412         Project project = (Project)projects.get(i);
4127 26 Sep 16 olle 1413         int projectId = project.getId();
4127 26 Sep 16 olle 1414         SessionControl tmp2 = systemSc.impersonateLogin(SystemItems.getId(User.ROOT), "Meludi services");
4127 26 Sep 16 olle 1415         tmp2.setActiveProject(project);
4127 26 Sep 16 olle 1416         projectIdScHM.put(projectId, tmp2);
4162 19 Oct 16 olle 1417         projectIdNameHM.put(projectId, project.getName());
4127 26 Sep 16 olle 1418       }
4127 26 Sep 16 olle 1419     }
4127 26 Sep 16 olle 1420     finally
4127 26 Sep 16 olle 1421     {
4127 26 Sep 16 olle 1422       if (dc != null) dc.close();
4127 26 Sep 16 olle 1423     }
4127 26 Sep 16 olle 1424       
4127 26 Sep 16 olle 1425     // Set session control to input argument
4127 26 Sep 16 olle 1426     rootSc = tmp;
4127 26 Sep 16 olle 1427     // If no projects found, store session control for input argument
4127 26 Sep 16 olle 1428     if (projectIdScHM.size() == 0)
4127 26 Sep 16 olle 1429     {
4127 26 Sep 16 olle 1430       projectIdScHM.put(0, rootSc);
4127 26 Sep 16 olle 1431     }
4127 26 Sep 16 olle 1432     return projectIdScHM;
4127 26 Sep 16 olle 1433   }
3100 21 Jan 15 olle 1434   
3100 21 Jan 15 olle 1435   /**
3100 21 Jan 15 olle 1436     Close the root session control if it is active. This method should be
3100 21 Jan 15 olle 1437     called from services when they are stopped.
3100 21 Jan 15 olle 1438     @since 3.0
3100 21 Jan 15 olle 1439   */
3100 21 Jan 15 olle 1440   public static synchronized void closeRootSessionControl()
3100 21 Jan 15 olle 1441   {
3100 21 Jan 15 olle 1442     if (rootSc != null) 
3100 21 Jan 15 olle 1443     {
3100 21 Jan 15 olle 1444       rootSc.close();
3100 21 Jan 15 olle 1445       rootSc = null;
3100 21 Jan 15 olle 1446     }
3100 21 Jan 15 olle 1447   }
3100 21 Jan 15 olle 1448   
3100 21 Jan 15 olle 1449   /**
2933 14 Nov 14 olle 1450     List all annotation types with the given name.
2933 14 Nov 14 olle 1451     @since 2.0
2933 14 Nov 14 olle 1452   */
2933 14 Nov 14 olle 1453   public static List<AnnotationTypeCategory> listAnnotationTypeCategories(DbControl dc, String name)
2933 14 Nov 14 olle 1454   {
2933 14 Nov 14 olle 1455     ItemQuery<AnnotationTypeCategory> query = AnnotationTypeCategory.getQuery();
2933 14 Nov 14 olle 1456     query.restrict(Restrictions.eq(Hql.property("name"), Expressions.parameter("name", name, Type.STRING)));
2933 14 Nov 14 olle 1457     query.include(Include.ALL);
2933 14 Nov 14 olle 1458     return query.list(dc);
2933 14 Nov 14 olle 1459   }
2933 14 Nov 14 olle 1460   
2933 14 Nov 14 olle 1461   /**
2933 14 Nov 14 olle 1462     Compare two comparable objects with null safety. Two null objects
2933 14 Nov 14 olle 1463     are considered equal and all null objects are sorted after all non-null
2933 14 Nov 14 olle 1464     objects.
2933 14 Nov 14 olle 1465     @return A negative value if the first argument should be sorted before the second,
2933 14 Nov 14 olle 1466       zero if they are equal, a positive value if the second argument should be sorted 
2933 14 Nov 14 olle 1467       before the first
2933 14 Nov 14 olle 1468   */
2933 14 Nov 14 olle 1469   public static <T extends Comparable<T>> int nullSafeCompare(T o1, T o2)
2933 14 Nov 14 olle 1470   {
2933 14 Nov 14 olle 1471     int result = 0;
2933 14 Nov 14 olle 1472     if (o1 == o2)
2933 14 Nov 14 olle 1473     {}
2933 14 Nov 14 olle 1474     else if (o1 == null)
2933 14 Nov 14 olle 1475     {
2933 14 Nov 14 olle 1476       result = 1;
2933 14 Nov 14 olle 1477     }
2933 14 Nov 14 olle 1478     else if (o2 == null)
2933 14 Nov 14 olle 1479     {
2933 14 Nov 14 olle 1480       result = -1;
2933 14 Nov 14 olle 1481     }
2933 14 Nov 14 olle 1482     else
2933 14 Nov 14 olle 1483     {
2933 14 Nov 14 olle 1484       result = o1.compareTo(o2);
2933 14 Nov 14 olle 1485     }
2933 14 Nov 14 olle 1486     return result;
2933 14 Nov 14 olle 1487   }
2933 14 Nov 14 olle 1488   
2933 14 Nov 14 olle 1489   /**
2933 14 Nov 14 olle 1490     Round a floating point value to a given number of decimals.
2933 14 Nov 14 olle 1491     @since 2.12
2933 14 Nov 14 olle 1492   */
2933 14 Nov 14 olle 1493   public static float round(float value, int decimals)
2933 14 Nov 14 olle 1494   {
2933 14 Nov 14 olle 1495     double factor = Math.pow(10, decimals);
2933 14 Nov 14 olle 1496     return (float)(Math.round(factor * value) / factor);
2933 14 Nov 14 olle 1497   }
2933 14 Nov 14 olle 1498   
2933 14 Nov 14 olle 1499   /**
2933 14 Nov 14 olle 1500     Convert to lower case and remove all characters not in [a-z0-9_]
2933 14 Nov 14 olle 1501     @since 2.16
2933 14 Nov 14 olle 1502   */
2933 14 Nov 14 olle 1503   public static String makeSafeProjectName(String name)
2933 14 Nov 14 olle 1504   {
2933 14 Nov 14 olle 1505     return name.toLowerCase().replaceAll("[^a-z0-9_]", "");
2933 14 Nov 14 olle 1506   }
2933 14 Nov 14 olle 1507
2933 14 Nov 14 olle 1508   /**
2933 14 Nov 14 olle 1509     Convert a data files folder to a BASE file system folder. 
2933 14 Nov 14 olle 1510     We want to use a prefix in the BASE file system to prevent
2933 14 Nov 14 olle 1511     several thousands of subfolders inside a single folder.
2933 14 Nov 14 olle 1512     The prefix should only be used if the data folder starts with
2933 14 Nov 14 olle 1513     digits and is two levels deep. The first level is the first two
2933 14 Nov 14 olle 1514     digits and the second level is the first four digits.
2933 14 Nov 14 olle 1515     
2933 14 Nov 14 olle 1516     
2933 14 Nov 14 olle 1517     Examples:
2933 14 Nov 14 olle 1518       /1234567.1/l.r.m.c.lib.g --> /12/1234/1234567.1/l.r.m.c.lib.g
2933 14 Nov 14 olle 1519       /debug/1234567.1/l.r.m.c.lib.g --> /debug/1234567.1/l.r.m.c.lib.g
2933 14 Nov 14 olle 1520       /Stratagene/r.m.c.lib.g --> /Stratagene/r.m.c.lib.g
2933 14 Nov 14 olle 1521       /SKBR3/m.c.lib.g --> /SKBR3/m.c.lib.g
2933 14 Nov 14 olle 1522     @since 2.16.1
2933 14 Nov 14 olle 1523   */
2933 14 Nov 14 olle 1524   public static String convertDataFilesFolderToBaseFolder(String dataFilesFolder)
2933 14 Nov 14 olle 1525   {
2933 14 Nov 14 olle 1526     // If the path starts with '/'+7 digits+'.'+at least one more digit +'/'
2933 14 Nov 14 olle 1527     // insert prefix based on first 2+4 digits
2933 14 Nov 14 olle 1528     return dataFilesFolder.replaceFirst("^((/\\d{2})\\d{2})\\d{3}\\.\\d+/", "$2$1$0");
2933 14 Nov 14 olle 1529   }
2933 14 Nov 14 olle 1530   
2933 14 Nov 14 olle 1531   /**
2933 14 Nov 14 olle 1532     Mark all related files for removal.
2933 14 Nov 14 olle 1533     @since 2.16
2933 14 Nov 14 olle 1534   */
2933 14 Nov 14 olle 1535   public static List<File> removeAttachedFiles(DbControl dc, FileStoreEnabled item)
2933 14 Nov 14 olle 1536   {
2933 14 Nov 14 olle 1537     List<File> files = new ArrayList<File>();
2933 14 Nov 14 olle 1538     
2933 14 Nov 14 olle 1539     if (item.hasFileSet())
2933 14 Nov 14 olle 1540     {
2933 14 Nov 14 olle 1541       ItemQuery<FileSetMember> query = item.getFileSet().getMembers();
2933 14 Nov 14 olle 1542       for (FileSetMember fsm : query.list(dc))
2933 14 Nov 14 olle 1543       {
2933 14 Nov 14 olle 1544         File f = fsm.getFile();
2933 14 Nov 14 olle 1545         f.setRemoved(true);
2933 14 Nov 14 olle 1546         files.add(f);
2933 14 Nov 14 olle 1547       }
2933 14 Nov 14 olle 1548     }
2933 14 Nov 14 olle 1549
2933 14 Nov 14 olle 1550     ItemQuery<AnyToAny> query = AnyToAny.getLinksFrom((BasicItem)item);
2933 14 Nov 14 olle 1551     for (AnyToAny ata : query.list(dc))
2933 14 Nov 14 olle 1552     {
2933 14 Nov 14 olle 1553       if (ata.getToType() == Item.FILE)
2933 14 Nov 14 olle 1554       {
2933 14 Nov 14 olle 1555         File f = (File)ata.getTo();
2933 14 Nov 14 olle 1556         f.setRemoved(true);
2933 14 Nov 14 olle 1557         files.add(f);
2933 14 Nov 14 olle 1558       }
2933 14 Nov 14 olle 1559     }
2933 14 Nov 14 olle 1560     
2933 14 Nov 14 olle 1561     return files;
2933 14 Nov 14 olle 1562   }
2933 14 Nov 14 olle 1563
3440 17 Jul 15 olle 1564   /**
3440 17 Jul 15 olle 1565    * Checks if name of item indicates that the item is a
3440 17 Jul 15 olle 1566    * specimen item. The test relies on specimen items having names
3440 17 Jul 15 olle 1567    * not ending with ".r" or ".d". NOTE! This test works for
3440 17 Jul 15 olle 1568    * extract source items only.
3440 17 Jul 15 olle 1569    * 
3440 17 Jul 15 olle 1570    * @param itemName String Name of item to check
3440 17 Jul 15 olle 1571    * @return boolean returns `true` if item is specimen item, else `false`
3440 17 Jul 15 olle 1572    */
3433 29 Jun 15 olle 1573   public static boolean itemIsSpecimen(String itemName)
3433 29 Jun 15 olle 1574   {
3433 29 Jun 15 olle 1575     boolean result = false;
3433 29 Jun 15 olle 1576     if (itemName != null)
3433 29 Jun 15 olle 1577     {
3433 29 Jun 15 olle 1578       if (!itemName.endsWith(".r") && !itemName.endsWith(".d"))
3433 29 Jun 15 olle 1579       {
3433 29 Jun 15 olle 1580         result = true;
3433 29 Jun 15 olle 1581       }
3433 29 Jun 15 olle 1582     }
3433 29 Jun 15 olle 1583     return result;
3433 29 Jun 15 olle 1584   }
3433 29 Jun 15 olle 1585
3440 17 Jul 15 olle 1586   /**
3440 17 Jul 15 olle 1587    * Checks if name of item indicates that the item is an
3440 17 Jul 15 olle 1588    * RNA item. The test relies on RNA items having names
3440 17 Jul 15 olle 1589    * ending with ".r".
3440 17 Jul 15 olle 1590    * 
3440 17 Jul 15 olle 1591    * @param itemName String Name of item to check
3440 17 Jul 15 olle 1592    * @return boolean returns `true` if item is RNA item, else `false`
3440 17 Jul 15 olle 1593    */
3433 29 Jun 15 olle 1594   public static boolean itemIsRna(String itemName)
3433 29 Jun 15 olle 1595   {
3433 29 Jun 15 olle 1596     boolean result = false;
3433 29 Jun 15 olle 1597     if (itemName != null)
3433 29 Jun 15 olle 1598     {
3433 29 Jun 15 olle 1599       if (itemName.endsWith(".r"))
3433 29 Jun 15 olle 1600       {
3433 29 Jun 15 olle 1601         result = true;
3433 29 Jun 15 olle 1602       }
3433 29 Jun 15 olle 1603     }
3433 29 Jun 15 olle 1604     return result;
3433 29 Jun 15 olle 1605   }
3433 29 Jun 15 olle 1606
3440 17 Jul 15 olle 1607   /**
3440 17 Jul 15 olle 1608    * Checks if name of item indicates that the item is a
3440 17 Jul 15 olle 1609    * DNA item. The test relies on DNA items having names
3440 17 Jul 15 olle 1610    * ending with ".d".
3440 17 Jul 15 olle 1611    * 
3440 17 Jul 15 olle 1612    * @param itemName String Name of item to check
3440 17 Jul 15 olle 1613    * @return boolean returns `true` if item is DNA item, else `false`
3440 17 Jul 15 olle 1614    */
3433 29 Jun 15 olle 1615   public static boolean itemIsDna(String itemName)
3433 29 Jun 15 olle 1616   {
3433 29 Jun 15 olle 1617     boolean result = false;
3433 29 Jun 15 olle 1618     if (itemName != null)
3433 29 Jun 15 olle 1619     {
3433 29 Jun 15 olle 1620       if (itemName.endsWith(".d"))
3433 29 Jun 15 olle 1621       {
3433 29 Jun 15 olle 1622         result = true;
3433 29 Jun 15 olle 1623       }
3433 29 Jun 15 olle 1624     }
3433 29 Jun 15 olle 1625     return result;
3433 29 Jun 15 olle 1626   }
3433 29 Jun 15 olle 1627
3440 17 Jul 15 olle 1628   /**
3440 17 Jul 15 olle 1629    * Checks if name of item indicates that the item is an
3440 17 Jul 15 olle 1630    * extract source item, i.e. specimens, input DNA, input RNA.
3440 17 Jul 15 olle 1631    * The test relies on extract source items having names
3440 17 Jul 15 olle 1632    * containing exactly one dot.
3440 17 Jul 15 olle 1633    * 
3440 17 Jul 15 olle 1634    * @param itemName String Name of item to check
3440 17 Jul 15 olle 1635    * @return boolean returns `true` if item is extract source item, else `false`
3440 17 Jul 15 olle 1636    */
3433 29 Jun 15 olle 1637   public static boolean itemIsExtractSourceItem(String itemName)
3433 29 Jun 15 olle 1638   {
3433 29 Jun 15 olle 1639     boolean result = false;
3433 29 Jun 15 olle 1640     if (itemName != null)
3433 29 Jun 15 olle 1641     {
3433 29 Jun 15 olle 1642       // Extract source items contain exactly one dot in the name
3433 29 Jun 15 olle 1643       // e.g. Specimens, input DNA, input RNA
3433 29 Jun 15 olle 1644       int numDots = numberOfSpecificCharacters(itemName, '.');
3433 29 Jun 15 olle 1645       if (numDots == 1)
3433 29 Jun 15 olle 1646       {
3433 29 Jun 15 olle 1647         result = true;
3433 29 Jun 15 olle 1648       }
3433 29 Jun 15 olle 1649     }
3433 29 Jun 15 olle 1650     return result;
3433 29 Jun 15 olle 1651   }
3433 29 Jun 15 olle 1652
3433 29 Jun 15 olle 1653   /**
3440 17 Jul 15 olle 1654    * Checks if name of item indicates that the item is a
3433 29 Jun 15 olle 1655    * MeLuDI project item. The test relies on MeLuDI project
3433 29 Jun 15 olle 1656    * items having names starting with prefix Meludi.ITEM_NAME_PREFIX.
3433 29 Jun 15 olle 1657    * 
3440 17 Jul 15 olle 1658    * @param itemName String Name of item to check
4142 30 Sep 16 olle 1659    * @param activeProjectId int ID of project to check item name for.
3433 29 Jun 15 olle 1660    * @return boolean returns `true` if item is MeLuDI project item, else `false`
3433 29 Jun 15 olle 1661    */
4142 30 Sep 16 olle 1662   public static boolean itemIsMeludiItem(String itemName, int activeProjectId)
3433 29 Jun 15 olle 1663   {
3433 29 Jun 15 olle 1664     boolean result = false;
3433 29 Jun 15 olle 1665     if (itemName != null)
3433 29 Jun 15 olle 1666     {
5216 09 Jan 19 olle 1667       boolean bloodAndSampleItemPrefixesDiffer = Meludi.bloodAndSampleItemPrefixesDiffer(activeProjectId);
5216 09 Jan 19 olle 1668       String meludiItemPrefix = Meludi.fetchSampleItemPrefix(activeProjectId);
5216 09 Jan 19 olle 1669       if (bloodAndSampleItemPrefixesDiffer)
3433 29 Jun 15 olle 1670       {
5216 09 Jan 19 olle 1671         meludiItemPrefix = Meludi.fetchFfpeBlockItemPrefix(activeProjectId);
5216 09 Jan 19 olle 1672       }
5216 09 Jan 19 olle 1673       if (itemName.startsWith(meludiItemPrefix))
5216 09 Jan 19 olle 1674       {
3433 29 Jun 15 olle 1675         result = true;
3433 29 Jun 15 olle 1676       }
3433 29 Jun 15 olle 1677     }
3433 29 Jun 15 olle 1678     return result;
3433 29 Jun 15 olle 1679   }
3433 29 Jun 15 olle 1680
3433 29 Jun 15 olle 1681   private static int numberOfSpecificCharacters(String str, char c)
3433 29 Jun 15 olle 1682   {
3433 29 Jun 15 olle 1683     int result = 0;
3433 29 Jun 15 olle 1684     if (str != null)
3433 29 Jun 15 olle 1685     {
3433 29 Jun 15 olle 1686       int num = 0;
3433 29 Jun 15 olle 1687       char[] charArr = str.toCharArray();
3433 29 Jun 15 olle 1688       for (int i = 0; i < charArr.length; i++)
3433 29 Jun 15 olle 1689       {
3433 29 Jun 15 olle 1690         if (charArr[i] == c)
3433 29 Jun 15 olle 1691         {
3433 29 Jun 15 olle 1692           num++;
3433 29 Jun 15 olle 1693         }
3433 29 Jun 15 olle 1694       }
3433 29 Jun 15 olle 1695       result = num;
3433 29 Jun 15 olle 1696     }
3433 29 Jun 15 olle 1697     return result;
3433 29 Jun 15 olle 1698   }  
2933 14 Nov 14 olle 1699 }