package org.phoenixctms.ctsms.web.util;

import java.io.ByteArrayInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TimeZone;
import java.util.regex.Pattern;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.el.MethodBinding;
import javax.faces.event.ActionEvent;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.phoenixctms.ctsms.ServiceLocator;
import org.phoenixctms.ctsms.enumeration.AuthenticationType;
import org.phoenixctms.ctsms.enumeration.Color;
import org.phoenixctms.ctsms.enumeration.DBModule;
import org.phoenixctms.ctsms.enumeration.ECRFFieldStatusQueue;
import org.phoenixctms.ctsms.enumeration.FileModule;
import org.phoenixctms.ctsms.enumeration.HyperlinkModule;
import org.phoenixctms.ctsms.enumeration.JournalModule;
import org.phoenixctms.ctsms.enumeration.PaymentMethod;
import org.phoenixctms.ctsms.enumeration.VariablePeriod;
import org.phoenixctms.ctsms.exception.AuthenticationException;
import org.phoenixctms.ctsms.exception.AuthorisationException;
import org.phoenixctms.ctsms.exception.ServiceException;
import org.phoenixctms.ctsms.js.JsUtil;
import org.phoenixctms.ctsms.util.CommonUtil;
import org.phoenixctms.ctsms.util.CommonUtil.EllipsisPlacement;
import org.phoenixctms.ctsms.vo.AddressTypeVO;
import org.phoenixctms.ctsms.vo.AlphaIdVO;
import org.phoenixctms.ctsms.vo.AspAtcCodeVO;
import org.phoenixctms.ctsms.vo.AspSubstanceVO;
import org.phoenixctms.ctsms.vo.AspVO;
import org.phoenixctms.ctsms.vo.AuthenticationTypeVO;
import org.phoenixctms.ctsms.vo.AuthenticationVO;
import org.phoenixctms.ctsms.vo.BooleanVO;
import org.phoenixctms.ctsms.vo.CalendarWeekVO;
import org.phoenixctms.ctsms.vo.ContactDetailTypeVO;
import org.phoenixctms.ctsms.vo.CourseCategoryVO;
import org.phoenixctms.ctsms.vo.CourseInVO;
import org.phoenixctms.ctsms.vo.CourseOutVO;
import org.phoenixctms.ctsms.vo.CourseParticipationStatusEntryOutVO;
import org.phoenixctms.ctsms.vo.CourseParticipationStatusTypeVO;
import org.phoenixctms.ctsms.vo.CriteriaOutVO;
import org.phoenixctms.ctsms.vo.CriterionPropertyVO;
import org.phoenixctms.ctsms.vo.CvPDFVO;
import org.phoenixctms.ctsms.vo.CvPositionOutVO;
import org.phoenixctms.ctsms.vo.CvSectionVO;
import org.phoenixctms.ctsms.vo.DBModuleVO;
import org.phoenixctms.ctsms.vo.DepartmentVO;
import org.phoenixctms.ctsms.vo.DiagnosisOutVO;
import org.phoenixctms.ctsms.vo.DutyRosterTurnOutVO;
import org.phoenixctms.ctsms.vo.ECRFFieldOutVO;
import org.phoenixctms.ctsms.vo.ECRFFieldStatusEntryOutVO;
import org.phoenixctms.ctsms.vo.ECRFFieldStatusTypeVO;
import org.phoenixctms.ctsms.vo.ECRFFieldValueOutVO;
import org.phoenixctms.ctsms.vo.ECRFOutVO;
import org.phoenixctms.ctsms.vo.ECRFProgressSummaryVO;
import org.phoenixctms.ctsms.vo.ECRFProgressVO;
import org.phoenixctms.ctsms.vo.ECRFStatusEntryVO;
import org.phoenixctms.ctsms.vo.ECRFStatusTypeVO;
import org.phoenixctms.ctsms.vo.EventImportanceVO;
import org.phoenixctms.ctsms.vo.HyperlinkCategoryVO;
import org.phoenixctms.ctsms.vo.InputFieldOutVO;
import org.phoenixctms.ctsms.vo.InputFieldSelectionSetValueOutVO;
import org.phoenixctms.ctsms.vo.InputFieldTypeVO;
import org.phoenixctms.ctsms.vo.InquiryOutVO;
import org.phoenixctms.ctsms.vo.InquiryValueOutVO;
import org.phoenixctms.ctsms.vo.InventoryBookingOutVO;
import org.phoenixctms.ctsms.vo.InventoryCategoryVO;
import org.phoenixctms.ctsms.vo.InventoryOutVO;
import org.phoenixctms.ctsms.vo.InventoryStatusEntryOutVO;
import org.phoenixctms.ctsms.vo.InventoryStatusTypeVO;
import org.phoenixctms.ctsms.vo.JournalCategoryVO;
import org.phoenixctms.ctsms.vo.JournalEntryOutVO;
import org.phoenixctms.ctsms.vo.JournalModuleVO;
import org.phoenixctms.ctsms.vo.LecturerCompetenceVO;
import org.phoenixctms.ctsms.vo.LecturerOutVO;
import org.phoenixctms.ctsms.vo.MaintenanceScheduleItemOutVO;
import org.phoenixctms.ctsms.vo.MaintenanceTypeVO;
import org.phoenixctms.ctsms.vo.MoneyTransferSummaryVO;
import org.phoenixctms.ctsms.vo.NotificationTypeVO;
import org.phoenixctms.ctsms.vo.OpsCodeVO;
import org.phoenixctms.ctsms.vo.PasswordInVO;
import org.phoenixctms.ctsms.vo.PasswordOutVO;
import org.phoenixctms.ctsms.vo.PaymentMethodVO;
import org.phoenixctms.ctsms.vo.PrivacyConsentStatusTypeVO;
import org.phoenixctms.ctsms.vo.ProbandCategoryVO;
import org.phoenixctms.ctsms.vo.ProbandGroupOutVO;
import org.phoenixctms.ctsms.vo.ProbandLetterPDFVO;
import org.phoenixctms.ctsms.vo.ProbandListEntryOutVO;
import org.phoenixctms.ctsms.vo.ProbandListEntryTagOutVO;
import org.phoenixctms.ctsms.vo.ProbandListEntryTagValueOutVO;
import org.phoenixctms.ctsms.vo.ProbandListStatusTypeVO;
import org.phoenixctms.ctsms.vo.ProbandOutVO;
import org.phoenixctms.ctsms.vo.ProbandStatusEntryOutVO;
import org.phoenixctms.ctsms.vo.ProbandStatusTypeVO;
import org.phoenixctms.ctsms.vo.ProcedureOutVO;
import org.phoenixctms.ctsms.vo.SexVO;
import org.phoenixctms.ctsms.vo.SponsoringTypeVO;
import org.phoenixctms.ctsms.vo.StaffCategoryVO;
import org.phoenixctms.ctsms.vo.StaffOutVO;
import org.phoenixctms.ctsms.vo.StaffStatusEntryOutVO;
import org.phoenixctms.ctsms.vo.StaffStatusTypeVO;
import org.phoenixctms.ctsms.vo.SurveyStatusTypeVO;
import org.phoenixctms.ctsms.vo.TeamMemberOutVO;
import org.phoenixctms.ctsms.vo.TeamMemberRoleVO;
import org.phoenixctms.ctsms.vo.TimelineEventOutVO;
import org.phoenixctms.ctsms.vo.TimelineEventTypeVO;
import org.phoenixctms.ctsms.vo.TrialOutVO;
import org.phoenixctms.ctsms.vo.TrialStatusTypeVO;
import org.phoenixctms.ctsms.vo.TrialTypeVO;
import org.phoenixctms.ctsms.vo.UserOutVO;
import org.phoenixctms.ctsms.vo.VariablePeriodVO;
import org.phoenixctms.ctsms.vo.VisitOutVO;
import org.phoenixctms.ctsms.vo.VisitScheduleItemOutVO;
import org.phoenixctms.ctsms.vo.VisitTypeVO;
import org.phoenixctms.ctsms.web.model.ApplicationScopeBean;
import org.phoenixctms.ctsms.web.model.SessionScopeBean;
import org.phoenixctms.ctsms.web.util.Settings.Bundle;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.sun.faces.application.view.ViewScopeManager;

public final class WebUtil {

	public enum ColorOpacity {
		ALPHA100(""),
		ALPHA50("-50"),
		ALPHA25("-25");

		private final String suffix;

		private ColorOpacity(final String suffix) {
			this.suffix = suffix;
		}

		// public float alpha() {
		// if (this.equals(ALPHA100)) {
		// return 1.0f;
		// }
		// return Float.parseFloat(suffix.substring(1))/100.0f;
		// }


		@Override
		public String toString() {
			return suffix;
		}

		public String value() {
			return suffix;
		}
	}
	public static final String REST_API_PATH = "rest";
	public static final int IMAGE_STORE_MAX_SIZE = 2;
	private final static String COLOR_STYLECLASS_PREFIX = "ctsms-color-";
	private static final String INPUT_FIELD_TYPE_ICON_STYLECLASS_PREFIX = "ctsms-inputfieldtype-";
	public static final String SCHEDULE_EVENT_ICON_STYLECLASS = "ctsms-event-icon";
	public static final String COLLISION_ICON_STYLECLASS = "ctsms-icon-warning";
	public static final String MENUBAR_ICON_STYLECLASS = "ctsms-menubar-icon";
	public static final String MENU_BOLD_STYLECLASS = "ctsms-menu-bold";
	public final static String ID_SEPARATOR_STRING = ",";
	public final static Pattern ID_SEPARATOR_REGEXP = Pattern.compile(Pattern.quote(ID_SEPARATOR_STRING));


	public final static int FACES_INITIAL_ROW_INDEX = 0;
	// private final static String BASE64_CHARSET = "UTF8";
	private final static String REFERER_HEADER_NAME = "Referer";
	public final static String EVENT_CONTEXT_VIEW_ID = "viewId";
	public final static String FILE_TITLE_PSF_PROPERTY_NAME = "title";
	public final static String FILE_ID_PSF_PROPERTY_NAME = "id";
	public final static String FILE_TITLE_HASH_PSF_PROPERTY_NAME = "titleHash";
	public final static String FILE_NAME_PSF_PROPERTY_NAME = "fileName";
	public final static String FILE_LOGICAL_PATH_PSF_PROPERTY_NAME = "logicalPath";
	public final static String FILE_NAME_HASH_PSF_PROPERTY_NAME = "fileNameHash";
	public final static String FILE_ACTIVE_PSF_PROPERTY_NAME = "active";
	public final static String TRIAL_STATUS_PSF_PROPERTY_NAME = "status";
	public final static String INQUIRY_POSITION_PSF_PROPERTY_NAME = "position";
	public final static String PROBAND_LIST_ENTRY_TAG_POSITION_PSF_PROPERTY_NAME = "position";
	public final static String JOURNAL_ENTRY_ID_PSF_PROPERTY_NAME = "id";
	public final static String ECRF_FIELD_VALUE_ID_PSF_PROPERTY_NAME = "id";
	public final static String ECRF_FIELD_STATUS_ENTRY_ID_PSF_PROPERTY_NAME = "id";
	public static final String FOLDER_NODE_TYPE = "folder";
	public static final String FILE_NODE_TYPE = "file";
	public static final String PARENT_NODE_TYPE = "parent";
	public static final String LEAF_NODE_TYPE = "leaf";
	public static final String JS_NULL = "null";
	// public final static String VISIT_SCHEDULE_ITEM_GROUP_TOKEN_PSF_PROPERTY_NAME = "group.token";
	// public final static String VISIT_SCHEDULE_ITEM_VISIT_TOKEN_PSF_PROPERTY_NAME = "visit.token";
	// public static final GsonExclusionStrategy[] GSON_EXCLUSION_STRATEGIES = new GsonExclusionStrategy[] {
	// new GsonExclusionStrategy(UserOutVO.class, "modifiedUser"),
	// new GsonExclusionStrategy(StaffOutVO.class, "modifiedUser"),
	// new GsonExclusionStrategy(InventoryOutVO.class, "children"),
	// new GsonExclusionStrategy(StaffOutVO.class, "children"),
	// new GsonExclusionStrategy(CourseOutVO.class, "renewals"),
	// new GsonExclusionStrategy(CourseOutVO.class, "precedingCourses"),
	// // new GsonExclusionStrategy(InputFieldOutVO.class, "selectionSetValues"),
	// new GsonExclusionStrategy(InputFieldSelectionSetValueOutVO.class, "field"),
	// // new GsonExclusionStrategy(CriteriaOutVO.class, "criterions"),
	// new GsonExclusionStrategy(CriterionOutVO.class, "criteria"),
	// // new GsonExclusionStrategy(ProbandListEntryOutVO.class, "lastStatus"),
	// new GsonExclusionStrategy(ProbandListStatusEntryOutVO.class, "listEntry"),
	// new GsonExclusionStrategy(ProbandOutVO.class, "children"),
	// new GsonExclusionStrategy(ProbandOutVO.class, "parents"),
	// };
	// public static final HashMap<Class, JsonSerializer> GSON_SHORTCUT_SERIALISATIONS = new HashMap<Class, JsonSerializer>();
	// static {
	// GSON_SHORTCUT_SERIALISATIONS.put(UserOutVO.class, new JsonSerializer<UserOutVO>() {
	//
	// @Override
	// public JsonElement serialize(UserOutVO src, Type typeOfSrc, JsonSerializationContext context) {
	// JsonObject object = new JsonObject();
	// object.addProperty("id", src.getId());
	// object.addProperty("userName", src.getName());
	// object.addProperty("staffName", src.getIdentity() != null ? src.getIdentity().getName() : null);
	// object.addProperty("staffInitials", src.getIdentity() != null ? src.getIdentity().getInitials() : null);
	// return object;
	// }
	// }
	// );
	// }
	// private final static Gson INPUT_FIELD_VARIABLE_VALUE_JSON_SERIALIZER = registerGsonTypeAdapters(new GsonBuilder(),
	// GSON_SHORTCUT_SERIALISATIONS
	// ).setExclusionStrategies(GSON_EXCLUSION_STRATEGIES)
	// .serializeNulls()
	// .setDateFormat(CommonUtil.INPUT_DATETIME_PATTERN)
	// .create();
	// private final static Gson VO_JSON_SERIALIZER = JsUtil.registerGsonTypeAdapters(new GsonBuilder(),
	// JsUtil.GSON_SHORTCUT_SERIALISATIONS
	// ).setExclusionStrategies(JsUtil.GSON_EXCLUSION_STRATEGIES)
	// .serializeNulls()
	// .setDateFormat(DateUtil.JSON_DATETIME_PATTERN)
	// .create();
	// https://groups.google.com/forum/#!topic/google-gson/u6hq2OVpszc
	// Yes, it's threadsafe. But then again, a JsonParser has no fields at all, so it doesn't use much memory, anyway. So if it's more work to reuse the same instance, it might not
	// be worth it.
	private final static Gson JSON_BEAUTIFIER = new GsonBuilder().setPrettyPrinting()
			.serializeNulls()
			.setDateFormat(JsUtil.JSON_DATETIME_PATTERN)
			.create();
	private final static Gson JSON_COMPRESSOR = new GsonBuilder().serializeNulls()
			.setDateFormat(JsUtil.JSON_DATETIME_PATTERN)
			.create();

	private final static JsonParser JSON_PARSER = new JsonParser();

	private final static String EL_ENUM_LIST_DEFAULT_SEPARATOR = ",";

	private final static Pattern EL_ENUM_LIST_REGEXP = Pattern.compile(Pattern.quote(EL_ENUM_LIST_DEFAULT_SEPARATOR));

	public static Date addIntervals(Date date, VariablePeriod period, Long explicitDays, int n) {
		try {
			return getServiceLocator().getToolsService().addIntervals(date, period, explicitDays, n);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return null;
	}

	public static void appendRefererParameter(StringBuilder url, HttpServletRequest request, String separator) {
		if (url != null && request != null) {
			if (separator != null && separator.length() > 0) {
				url.append(separator);
			}
			url.append(GetParamNames.REFERER);
			url.append("=");
			url.append(getRefererBase64(request));
		}
	}

	public static RequestContext appendRequestContextCallbackTabTitleArgs(RequestContext context,
			JSValues tabTitleBase64Arg,
			JSValues countJsVar,
			String tabTitleMsgCode,
			String tabTitleWithCountMsgCode,
			Long count, Object... args) {
		RequestContext requestContext = context == null ? RequestContext.getCurrentInstance() : context;
		if (requestContext != null) {
			requestContext.addCallbackParam(tabTitleBase64Arg.toString(), JsUtil.encodeBase64(getTabTitleString(tabTitleMsgCode, tabTitleWithCountMsgCode, count, args), false));
			requestContext.addCallbackParam(countJsVar.toString(), count != null ? count : -1l);
		}
		return requestContext;
	}

	public static String beautifyJson(String json) {
		try {
			JsonElement je = JSON_PARSER.parse(json);
			return JSON_BEAUTIFIER.toJson(je);
		} catch (JsonSyntaxException e) {
		} catch (NullPointerException e) {
		}
		return json;
	}

	public static String clipStringPicker(String string) {
		return CommonUtil.clipString(string, Settings.getInt(SettingCodes.PICKER_CLIP_MAX_LENGTH, Bundle.SETTINGS, DefaultSettings.PICKER_CLIP_MAX_LENGTH),
				CommonUtil.DEFAULT_ELLIPSIS, EllipsisPlacement.MID);
	}

	public static String colorToStyleClass(Color color) {
		return colorToStyleClass(color, null);
	}
	public static String colorToStyleClass(Color color, ColorOpacity alpha) {
		StringBuilder result = new StringBuilder();
		if (color != null) {
			result.append(COLOR_STYLECLASS_PREFIX);
			result.append(color.name().toLowerCase(Locale.ENGLISH));
			if (alpha != null) {
				result.append(alpha.value());
			}
		}
		return result.toString();
	}

	public static List<String> completeBankCodeNumber(String query, String bicPrefix, String bankNameInfix) {
		Collection<String> bankCodeNumbers = null;
		try {
			bankCodeNumbers = getServiceLocator().getToolsService().completeBankCodeNumber(getAuthentication(), query, bicPrefix, bankNameInfix, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (bankCodeNumbers != null) {
			try {
				return ((List<String>) bankCodeNumbers);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeBankName(String bankCodeNumberPrefix, String bicPrefix, String query) {
		Collection<String> bankNames = null;
		try {
			bankNames = getServiceLocator().getToolsService().completeBankName(getAuthentication(), query, bankCodeNumberPrefix, bicPrefix, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (bankNames != null) {
			try {
				return ((List<String>) bankNames);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeBic(String bankCodeNumberPrefix, String query, String bankNameInfix) {
		Collection<String> bics = null;
		try {
			bics = getServiceLocator().getToolsService().completeBic(getAuthentication(), query, bankCodeNumberPrefix, bankNameInfix, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (bics != null) {
			try {
				return ((List<String>) bics);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeCityName(String countryName, String zipCode, String query) {
		Collection<String> cityNames = null;
		try {
			cityNames = getServiceLocator().getToolsService().completeCityName(getAuthentication(), query, countryName, zipCode, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (cityNames != null) {
			try {
				return ((List<String>) cityNames);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeCountryName(String query) {
		Collection<String> countryNames = null;
		try {
			countryNames = getServiceLocator().getToolsService().completeCountryName(getAuthentication(), query, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (countryNames != null) {
			try {
				return ((List<String>) countryNames);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeLogicalPath(FileModule module, Long entityId, String query) {
		Collection<String> folders = null;
		try {
			folders = getServiceLocator().getFileService().getFileFolders(getAuthentication(), module, entityId, query, true, null, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (folders != null) {
			try {
				return ((List<String>) folders);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeStreetName(String countryName, String zipCode, String cityName, String query) {
		Collection<String> streetNames = null;
		try {
			streetNames = getServiceLocator().getToolsService().completeStreetName(getAuthentication(), query, countryName, zipCode, cityName, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (streetNames != null) {
			try {
				return ((List<String>) streetNames);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeTitle(String query) {
		Collection<String> titles = null;
		try {
			titles = getServiceLocator().getToolsService().completeTitle(getAuthentication(), query, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (titles != null) {
			try {
				return ((List<String>) titles);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static List<String> completeZipCode(String countryName, String query, String cityName, String streetName) {
		Collection<String> zipCodes = null;
		try {
			if (!CommonUtil.isEmptyString(countryName) && !CommonUtil.isEmptyString(cityName) && !CommonUtil.isEmptyString(streetName)) {
				zipCodes = getServiceLocator().getToolsService().completeZipCodeByStreetName(getAuthentication(), query, countryName, cityName, streetName, null);
			} else {
				zipCodes = getServiceLocator().getToolsService().completeZipCode(getAuthentication(), query, countryName, cityName, null);
			}
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (zipCodes != null) {
			try {
				return ((List<String>) zipCodes);
			} catch (ClassCastException e) {
			}
		}
		return new ArrayList<String>();
	}

	public static String compressJson(String json) {
		try {
			JsonElement je = JSON_PARSER.parse(json);
			return JSON_COMPRESSOR.toJson(je);
		} catch (JsonSyntaxException e) {
		} catch (NullPointerException e) {
		}
		return json;
	}

	public static String courseIdToName(Long id) {
		if (id == null) {
			return getNoCoursePickedMessage();
		}
		try {
			CourseOutVO course = getServiceLocator().getCourseService().getCourse(getAuthentication(), id, null, null, null);
			return courseOutVOToString(course);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return Messages.getString(MessageCodes.INVALID_COURSE_PICKED);
	}

	public static String courseOutVOToString(CourseOutVO course) {
		String result = CommonUtil.courseOutVOToString(course);
		if (result != null) {
			return result;
		} else {
			return getNoCoursePickedMessage();
		}
	}

	// public static String decodeBase64(String base64String) {
	// if (base64String != null && base64String.length() > 0 && Base64.isBase64(base64String)) {
	// try {
	// return new String(Base64.decodeBase64(base64String), BASE64_CHARSET);
	// } catch (UnsupportedEncodingException e) {
	// }
	// }
	// return "";
	// }
	//
	// public static String encodeBase64(String string, boolean urlSafe) { // url safe
	// if (string != null && string.length() > 0) {
	// try {
	// return new String(Base64.encodeBase64(string.getBytes(BASE64_CHARSET), false, urlSafe, Integer.MAX_VALUE));
	// } catch (UnsupportedEncodingException e) {
	// }
	// }
	// return "";
	// }

	public static MethodBinding createActionListenerMethodBinding(String actionListenerString) {
		return FacesContext.getCurrentInstance().getApplication()
				.createMethodBinding(actionListenerString, new Class[] { ActionEvent.class });
	}

	public static Converter createConverter(String converterId) {
		FacesContext context = FacesContext.getCurrentInstance();
		return context.getApplication().createConverter(converterId);
	}

	public static String createViewUrl(Urls view, boolean relative, GetParamNames param, Long value) {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
		StringBuffer url = new StringBuffer();
		if (!relative) {
			url.append(request.getScheme()).append("://").append(request.getServerName()).append(":").append(request.getServerPort());
		}
		url.append(view.toString(request));
		if (param != null && value != null) {
			url.append('?').append(param.toString()).append("=").append(Long.toString(value));
		}
		return url.toString();
	}

	public static String escapeHtml(String string) {
		return org.apache.commons.lang.StringEscapeUtils.escapeHtml(string);
	}

	public static String escapeJSStringArray(List<String> valueList, boolean brackets, boolean quotes) {
		StringBuilder sb;
		if (brackets) {
			sb = new StringBuilder("[");
		} else {
			sb = new StringBuilder();
		}
		if (valueList != null && valueList.size() > 0) {
			Iterator<String> it = valueList.iterator();
			while (it.hasNext()) {
				sb.append(quoteJSString(it.next(), brackets || quotes));
				if (it.hasNext()) {
					sb.append(",");
				}
			}
		}
		if (brackets) {
			sb.append("]");
		}
		return sb.toString();
	}

	public static String expirationToColor(Date today, Date expiration, Map<Long, Color> dueInColorMap, Color overdueColor) {
		Boolean expired = getExpired(today, expiration);
		if (expired != null) {
			if (expired) {
				return colorToStyleClass(overdueColor);
			} else {
				long delta = CommonUtil.dateDeltaSecs(today, expiration);
				Iterator<Long> it = dueInColorMap.keySet().iterator();
				Long limitKey = null;
				while (it.hasNext()) {
					Long limit = it.next();
					if (delta < limit) {
						if (limitKey == null || limit < limitKey) {
							limitKey = limit;
						}
					}
				}
				if (limitKey != null) {
					return colorToStyleClass(dueInColorMap.get(limitKey));
				}
			}
		}
		return "";
	}

	public static UIComponent findComponentById(String id) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (context != null) {
			return findComponentById(context.getViewRoot(), id);
		}
		return null;
	}

	public static UIComponent findComponentById(UIComponent parent, String id) {
		if (id.equals(parent.getId())) {
			return parent;
		}
		Iterator<UIComponent> kids = parent.getFacetsAndChildren();
		while (kids.hasNext()) {
			UIComponent kid = kids.next();
			UIComponent found = findComponentById(kid, id);
			if (found != null) {
				return found;
			}
		}
		return null;
	}

	public static AddressTypeVO getAddressType(Long addressTypeId) {
		// AddressTypeVO type = null; // (AddressTypeVO) getSelectionSetServiceCache(AddressTypeVO.class, id);
		// if (type == null) {
		if (addressTypeId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getAddressType(getAuthentication(), addressTypeId);
				// putSelectionSetServiceCache(id, type);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return type;
	}

	public static ArrayList<SelectItem> getAllContactDetailTypes() {
		ArrayList<SelectItem> types;
		Collection<ContactDetailTypeVO> typeVOs = null;
		try {
			typeVOs = getServiceLocator().getSelectionSetService().getAllContactDetailTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (typeVOs != null) {
			types = new ArrayList<SelectItem>(typeVOs.size());
			Iterator<ContactDetailTypeVO> it = typeVOs.iterator();
			while (it.hasNext()) {
				ContactDetailTypeVO typeVO = it.next();
				types.add(new SelectItem(typeVO.getId().toString(), typeVO.getName()));
				// putSelectionSetServiceCache(typeVO.getId(), typeVO);
			}
		} else {
			types = new ArrayList<SelectItem>();
		}
		return types;
	}

	public static ArrayList<SelectItem> getAllCourseCategories() {
		ArrayList<SelectItem> categories;
		Collection<CourseCategoryVO> categoryVOs = null;
		try {
			categoryVOs = getServiceLocator().getSelectionSetService().getAllCourseCategories(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (categoryVOs != null) {
			categories = new ArrayList<SelectItem>(categoryVOs.size());
			Iterator<CourseCategoryVO> it = categoryVOs.iterator();
			while (it.hasNext()) {
				CourseCategoryVO categoryVO = it.next();
				categories.add(new SelectItem(categoryVO.getId().toString(), categoryVO.getName()));
				// putSelectionSetServiceCache(categoryVO.getId(), categoryVO);
			}
		} else {
			categories = new ArrayList<SelectItem>();
		}
		return categories;
	}

	public static ArrayList<SelectItem> getAllCourseParticipationStatusTypes() {
		ArrayList<SelectItem> statusTypes;
		Collection<CourseParticipationStatusTypeVO> statusTypeVOs = null;
		try {
			statusTypeVOs = getServiceLocator().getSelectionSetService().getAllCourseParticipationStatusTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (statusTypeVOs != null) {
			statusTypes = new ArrayList<SelectItem>(statusTypeVOs.size());
			Iterator<CourseParticipationStatusTypeVO> it = statusTypeVOs.iterator();
			while (it.hasNext()) {
				CourseParticipationStatusTypeVO statusTypeVO = it.next();
				statusTypes.add(new SelectItem(statusTypeVO.getId().toString(), statusTypeVO.getName()));
				// putSelectionSetServiceCache(statusTypeVO.getId(), statusTypeVO);
			}
		} else {
			statusTypes = new ArrayList<SelectItem>();
		}
		return statusTypes;
	}

	public static ArrayList<SelectItem> getAllCvSections() {
		ArrayList<SelectItem> cvSections;
		Collection<CvSectionVO> sectionVOs = null;
		try {
			sectionVOs = getServiceLocator().getSelectionSetService().getAllCvSections(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (sectionVOs != null) {
			cvSections = new ArrayList<SelectItem>(sectionVOs.size());
			Iterator<CvSectionVO> it = sectionVOs.iterator();
			while (it.hasNext()) {
				CvSectionVO sectionVO = it.next();
				cvSections.add(new SelectItem(sectionVO.getId().toString(), sectionVO.getName()));
				// putSelectionSetServiceCache(sectionVO.getId(), sectionVO);
			}
		} else {
			cvSections = new ArrayList<SelectItem>();
		}
		return cvSections;
	}

	public static ArrayList<SelectItem> getAllDepartments() {
		ArrayList<SelectItem> departments;
		Collection<DepartmentVO> departmentVOs = null;
		try {
			departmentVOs = getServiceLocator().getSelectionSetService().getAllDepartments(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (departmentVOs != null) {
			departments = new ArrayList<SelectItem>(departmentVOs.size());
			Iterator<DepartmentVO> it = departmentVOs.iterator();
			while (it.hasNext()) {
				DepartmentVO departmentVO = it.next();
				departments.add(new SelectItem(departmentVO.getId().toString(), departmentVO.getName()));
				// putSelectionSetServiceCache(departmentVO.getId(), departmentVO);
			}
		} else {
			departments = new ArrayList<SelectItem>();
		}
		return departments;
	}

	public static ArrayList<SelectItem> getAllEcrfFieldStatusTypes() {
		ArrayList<SelectItem> types;
		Collection<ECRFFieldStatusTypeVO> typeVOs = null;
		try {
			typeVOs = getServiceLocator().getSelectionSetService().getAllEcrfFieldStatusTypes(getAuthentication(), null, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (typeVOs != null) {
			types = new ArrayList<SelectItem>(typeVOs.size());
			Iterator<ECRFFieldStatusTypeVO> it = typeVOs.iterator();
			while (it.hasNext()) {
				ECRFFieldStatusTypeVO typeVO = it.next();
				types.add(new SelectItem(typeVO.getId().toString(), typeVO.getName()));
				// putSelectionSetServiceCache(typeVO.getId(), typeVO);
			}
		} else {
			types = new ArrayList<SelectItem>();
		}
		return types;
	}

	public static ArrayList<SelectItem> getAllInventoryCategories() {
		ArrayList<SelectItem> categories;
		Collection<InventoryCategoryVO> categoryVOs = null;
		try {
			categoryVOs = getServiceLocator().getSelectionSetService().getAllInventoryCategories(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (categoryVOs != null) {
			categories = new ArrayList<SelectItem>(categoryVOs.size());
			Iterator<InventoryCategoryVO> it = categoryVOs.iterator();
			while (it.hasNext()) {
				InventoryCategoryVO categoryVO = it.next();
				categories.add(new SelectItem(categoryVO.getId().toString(), categoryVO.getName()));
				// putSelectionSetServiceCache(categoryVO.getId(), categoryVO);
			}
		} else {
			categories = new ArrayList<SelectItem>();
		}
		return categories;
	}

	public static ArrayList<SelectItem> getAllInventoryStatusTypes() {
		ArrayList<SelectItem> statusTypes;
		Collection<InventoryStatusTypeVO> statusTypeVOs = null;
		try {
			statusTypeVOs = getServiceLocator().getSelectionSetService().getAllInventoryStatusTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (statusTypeVOs != null) {
			statusTypes = new ArrayList<SelectItem>(statusTypeVOs.size());
			Iterator<InventoryStatusTypeVO> it = statusTypeVOs.iterator();
			while (it.hasNext()) {
				InventoryStatusTypeVO statusTypeVO = it.next();
				statusTypes.add(new SelectItem(statusTypeVO.getId().toString(), statusTypeVO.getName()));
				// putSelectionSetServiceCache(statusTypeVO.getId(), statusTypeVO);
			}
		} else {
			statusTypes = new ArrayList<SelectItem>();
		}
		return statusTypes;
	}

	public static ArrayList<SelectItem> getAllLecturerCompetences() {
		ArrayList<SelectItem> competences;
		Collection<LecturerCompetenceVO> competenceVOs = null;
		try {
			competenceVOs = getServiceLocator().getSelectionSetService().getAllLecturerCompetences(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (competenceVOs != null) {
			competences = new ArrayList<SelectItem>(competenceVOs.size());
			Iterator<LecturerCompetenceVO> it = competenceVOs.iterator();
			while (it.hasNext()) {
				LecturerCompetenceVO competenceVO = it.next();
				competences.add(new SelectItem(competenceVO.getId().toString(), competenceVO.getName()));
				// putSelectionSetServiceCache(competenceVO.getId(), competenceVO);
			}
		} else {
			competences = new ArrayList<SelectItem>();
		}
		return competences;
	}

	public static ArrayList<SelectItem> getAllMaintenanceTypes() {
		ArrayList<SelectItem> maintenanceTypes;
		Collection<MaintenanceTypeVO> maintenanceTypeVOs = null;
		try {
			maintenanceTypeVOs = getServiceLocator().getSelectionSetService().getAllMaintenanceTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (maintenanceTypeVOs != null) {
			maintenanceTypes = new ArrayList<SelectItem>(maintenanceTypeVOs.size());
			Iterator<MaintenanceTypeVO> it = maintenanceTypeVOs.iterator();
			while (it.hasNext()) {
				MaintenanceTypeVO maintenanceTypeVO = it.next();
				maintenanceTypes.add(new SelectItem(maintenanceTypeVO.getId().toString(), maintenanceTypeVO.getName()));
				// putSelectionSetServiceCache(maintenanceTypeVO.getId(), maintenanceTypeVO);
			}
		} else {
			maintenanceTypes = new ArrayList<SelectItem>();
		}
		return maintenanceTypes;
	}

	public static ArrayList<SelectItem> getAllNotificationTypes() {
		ArrayList<SelectItem> notificationTypes;
		Collection<NotificationTypeVO> notificationTypeVOs = null;
		try {
			notificationTypeVOs = getServiceLocator().getSelectionSetService().getAllNotificationTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (notificationTypeVOs != null) {
			notificationTypes = new ArrayList<SelectItem>(notificationTypeVOs.size());
			Iterator<NotificationTypeVO> it = notificationTypeVOs.iterator();
			while (it.hasNext()) {
				NotificationTypeVO notificationTypeVO = it.next();
				notificationTypes.add(new SelectItem(notificationTypeVO.getId().toString(), notificationTypeVO.getName()));
			}
		} else {
			notificationTypes = new ArrayList<SelectItem>();
		}
		return notificationTypes;
	}

	public static String getAllowedFileExtensionsPattern(FileModule module, Boolean image) {
		String allowTypes = null;
		if (module != null) {
			try {
				allowTypes = getServiceLocator().getToolsService().getAllowedFileExtensionsPattern(module, image);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
			if (CommonUtil.isEmptyString(allowTypes)) {
				publishException(new FacesException("Cannot find file extensions from mime-types (" + module.toString() + ")"));
			}
		} else {
			return CommonUtil.DEFAULT_FILE_EXTENSION_PATTERN;
		}
		return allowTypes;
	}

	public static ArrayList<SelectItem> getAllPrivacyConsentStatusTypes() {
		ArrayList<SelectItem> statusTypes;
		Collection<PrivacyConsentStatusTypeVO> statusTypeVOs = null;
		try {
			statusTypeVOs = getServiceLocator().getSelectionSetService().getAllPrivacyConsentStatusTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (statusTypeVOs != null) {
			statusTypes = new ArrayList<SelectItem>(statusTypeVOs.size());
			Iterator<PrivacyConsentStatusTypeVO> it = statusTypeVOs.iterator();
			while (it.hasNext()) {
				PrivacyConsentStatusTypeVO statusTypeVO = it.next();
				statusTypes.add(new SelectItem(statusTypeVO.getId().toString(), statusTypeVO.getName()));
				// putSelectionSetServiceCache(statusTypeVO.getId(), statusTypeVO);
			}
		} else {
			statusTypes = new ArrayList<SelectItem>();
		}
		return statusTypes;
	}

	public static ArrayList<SelectItem> getAllProbandCategories() {
		ArrayList<SelectItem> categories;
		Collection<ProbandCategoryVO> categoryVOs = null;
		try {
			categoryVOs = getServiceLocator().getSelectionSetService().getAllProbandCategories(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (categoryVOs != null) {
			categories = new ArrayList<SelectItem>(categoryVOs.size());
			Iterator<ProbandCategoryVO> it = categoryVOs.iterator();
			while (it.hasNext()) {
				ProbandCategoryVO categoryVO = it.next();
				categories.add(new SelectItem(categoryVO.getId().toString(), categoryVO.getName()));
				// putSelectionSetServiceCache(categoryVO.getId(), categoryVO);
			}
		} else {
			categories = new ArrayList<SelectItem>();
		}
		return categories;
	}

	public static ArrayList<SelectItem> getAllProbandListStatusTypes(Boolean person) {
		ArrayList<SelectItem> probandListStatusTypes;
		Collection<ProbandListStatusTypeVO> probandListStatusTypeVOs = null;
		try {
			probandListStatusTypeVOs = getServiceLocator().getSelectionSetService().getAllProbandListStatusTypes(getAuthentication(), person);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (probandListStatusTypeVOs != null) {
			probandListStatusTypes = new ArrayList<SelectItem>(probandListStatusTypeVOs.size());
			Iterator<ProbandListStatusTypeVO> it = probandListStatusTypeVOs.iterator();
			while (it.hasNext()) {
				ProbandListStatusTypeVO probandListStatusTypeVO = it.next();
				probandListStatusTypes.add(new SelectItem(probandListStatusTypeVO.getId().toString(), probandListStatusTypeVO.getName()));
				// putSelectionSetServiceCache(probandListStatusTypeVO.getId(), probandListStatusTypeVO);
			}
		} else {
			probandListStatusTypes = new ArrayList<SelectItem>();
		}
		return probandListStatusTypes;
	}

	public static ArrayList<SelectItem> getAllProbandStatusTypes() {
		ArrayList<SelectItem> statusTypes;
		Collection<ProbandStatusTypeVO> statusTypeVOs = null;
		try {
			statusTypeVOs = getServiceLocator().getSelectionSetService().getAllProbandStatusTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (statusTypeVOs != null) {
			statusTypes = new ArrayList<SelectItem>(statusTypeVOs.size());
			Iterator<ProbandStatusTypeVO> it = statusTypeVOs.iterator();
			while (it.hasNext()) {
				ProbandStatusTypeVO statusTypeVO = it.next();
				statusTypes.add(new SelectItem(statusTypeVO.getId().toString(), statusTypeVO.getName()));
				// putSelectionSetServiceCache(statusTypeVO.getId(), statusTypeVO);
			}
		} else {
			statusTypes = new ArrayList<SelectItem>();
		}
		return statusTypes;
	}

	public static ArrayList<SelectItem> getAllSponsoringTypes() {
		ArrayList<SelectItem> sponsoringTypes;
		Collection<SponsoringTypeVO> sponsoringTypeVOs = null;
		try {
			sponsoringTypeVOs = getServiceLocator().getSelectionSetService().getAllSponsoringTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (sponsoringTypeVOs != null) {
			sponsoringTypes = new ArrayList<SelectItem>(sponsoringTypeVOs.size());
			Iterator<SponsoringTypeVO> it = sponsoringTypeVOs.iterator();
			while (it.hasNext()) {
				SponsoringTypeVO sponsoringTypeVO = it.next();
				sponsoringTypes.add(new SelectItem(sponsoringTypeVO.getId().toString(), sponsoringTypeVO.getName()));
				// putSelectionSetServiceCache(sponsoringTypeVO.getId(), sponsoringTypeVO);
			}
		} else {
			sponsoringTypes = new ArrayList<SelectItem>();
		}
		return sponsoringTypes;
	}

	public static ArrayList<SelectItem> getAllStaffCategories() {
		ArrayList<SelectItem> categories;
		Collection<StaffCategoryVO> categoryVOs = null;
		try {
			categoryVOs = getServiceLocator().getSelectionSetService().getAllStaffCategories(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (categoryVOs != null) {
			categories = new ArrayList<SelectItem>(categoryVOs.size());
			Iterator<StaffCategoryVO> it = categoryVOs.iterator();
			while (it.hasNext()) {
				StaffCategoryVO categoryVO = it.next();
				categories.add(new SelectItem(categoryVO.getId().toString(), categoryVO.getName()));
				// putSelectionSetServiceCache(categoryVO.getId(), categoryVO);
			}
		} else {
			categories = new ArrayList<SelectItem>();
		}
		return categories;
	}

	public static ArrayList<SelectItem> getAllStaffStatusTypes() {
		ArrayList<SelectItem> statusTypes;
		Collection<StaffStatusTypeVO> statusTypeVOs = null;
		try {
			statusTypeVOs = getServiceLocator().getSelectionSetService().getAllStaffStatusTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (statusTypeVOs != null) {
			statusTypes = new ArrayList<SelectItem>(statusTypeVOs.size());
			Iterator<StaffStatusTypeVO> it = statusTypeVOs.iterator();
			while (it.hasNext()) {
				StaffStatusTypeVO statusTypeVO = it.next();
				statusTypes.add(new SelectItem(statusTypeVO.getId().toString(), statusTypeVO.getName()));
				// putSelectionSetServiceCache(statusTypeVO.getId(), statusTypeVO);
			}
		} else {
			statusTypes = new ArrayList<SelectItem>();
		}
		return statusTypes;
	}

	public static ArrayList<SelectItem> getAllSurveyStatusTypes() {
		ArrayList<SelectItem> surveyStatusTypes;
		Collection<SurveyStatusTypeVO> surveyStatusTypeVOs = null;
		try {
			surveyStatusTypeVOs = getServiceLocator().getSelectionSetService().getAllSurveyStatusTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (surveyStatusTypeVOs != null) {
			surveyStatusTypes = new ArrayList<SelectItem>(surveyStatusTypeVOs.size());
			Iterator<SurveyStatusTypeVO> it = surveyStatusTypeVOs.iterator();
			while (it.hasNext()) {
				SurveyStatusTypeVO surveyStatusTypeVO = it.next();
				surveyStatusTypes.add(new SelectItem(surveyStatusTypeVO.getId().toString(), surveyStatusTypeVO.getName()));
				// putSelectionSetServiceCache(surveyStatusTypeVO.getId(), surveyStatusTypeVO);
			}
		} else {
			surveyStatusTypes = new ArrayList<SelectItem>();
		}
		return surveyStatusTypes;
	}

	public static ArrayList<SelectItem> getAllTeamMemberRoles() {
		ArrayList<SelectItem> roles;
		Collection<TeamMemberRoleVO> roleVOs = null;
		try {
			roleVOs = getServiceLocator().getSelectionSetService().getAllTeamMemberRoles(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (roleVOs != null) {
			roles = new ArrayList<SelectItem>(roleVOs.size());
			Iterator<TeamMemberRoleVO> it = roleVOs.iterator();
			while (it.hasNext()) {
				TeamMemberRoleVO roleVO = it.next();
				roles.add(new SelectItem(roleVO.getId().toString(), roleVO.getName()));
				// putSelectionSetServiceCache(roleVO.getId(), roleVO);
			}
		} else {
			roles = new ArrayList<SelectItem>();
		}
		return roles;
	}

	public static ArrayList<SelectItem> getAllTimelineEventTypes() {
		ArrayList<SelectItem> timelineEventTypes;
		Collection<TimelineEventTypeVO> eventTypeVOs = null;
		try {
			eventTypeVOs = getServiceLocator().getSelectionSetService().getAllTimelineEventTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (eventTypeVOs != null) {
			timelineEventTypes = new ArrayList<SelectItem>(eventTypeVOs.size());
			Iterator<TimelineEventTypeVO> it = eventTypeVOs.iterator();
			while (it.hasNext()) {
				TimelineEventTypeVO eventTypeVO = it.next();
				timelineEventTypes.add(new SelectItem(eventTypeVO.getId().toString(), eventTypeVO.getName()));
				// putSelectionSetServiceCache(eventTypeVO.getId(), eventTypeVO);
			}
		} else {
			timelineEventTypes = new ArrayList<SelectItem>();
		}
		return timelineEventTypes;
	}

	public static ArrayList<SelectItem> getAllTrialStatusTypes() {
		ArrayList<SelectItem> statusTypes;
		Collection<TrialStatusTypeVO> statusTypeVOs = null;
		try {
			statusTypeVOs = getServiceLocator().getSelectionSetService().getAllTrialStatusTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (statusTypeVOs != null) {
			statusTypes = new ArrayList<SelectItem>(statusTypeVOs.size());
			Iterator<TrialStatusTypeVO> it = statusTypeVOs.iterator();
			while (it.hasNext()) {
				TrialStatusTypeVO statusTypeVO = it.next();
				statusTypes.add(new SelectItem(statusTypeVO.getId().toString(), statusTypeVO.getName()));
				// putSelectionSetServiceCache(statusTypeVO.getId(), statusTypeVO);
			}
		} else {
			statusTypes = new ArrayList<SelectItem>();
		}
		return statusTypes;
	}

	public static ArrayList<SelectItem> getAllTrialTypes() {
		ArrayList<SelectItem> trialTypes;
		Collection<TrialTypeVO> trialTypeVOs = null;
		try {
			trialTypeVOs = getServiceLocator().getSelectionSetService().getAllTrialTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (trialTypeVOs != null) {
			trialTypes = new ArrayList<SelectItem>(trialTypeVOs.size());
			Iterator<TrialTypeVO> it = trialTypeVOs.iterator();
			while (it.hasNext()) {
				TrialTypeVO trialTypeVO = it.next();
				trialTypes.add(new SelectItem(trialTypeVO.getId().toString(), trialTypeVO.getName()));
				// putSelectionSetServiceCache(trialTypeVO.getId(), trialTypeVO);
			}
		} else {
			trialTypes = new ArrayList<SelectItem>();
		}
		return trialTypes;
	}

	public static ArrayList<SelectItem> getAllVisitTypes() {
		ArrayList<SelectItem> visitTypes;
		Collection<VisitTypeVO> visitTypeVOs = null;
		try {
			visitTypeVOs = getServiceLocator().getSelectionSetService().getAllVisitTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (visitTypeVOs != null) {
			visitTypes = new ArrayList<SelectItem>(visitTypeVOs.size());
			Iterator<VisitTypeVO> it = visitTypeVOs.iterator();
			while (it.hasNext()) {
				VisitTypeVO visitTypeVO = it.next();
				visitTypes.add(new SelectItem(visitTypeVO.getId().toString(), visitTypeVO.getName()));
				// putSelectionSetServiceCache(visitTypeVO.getId(), visitTypeVO);
			}
		} else {
			visitTypes = new ArrayList<SelectItem>();
		}
		return visitTypes;
	}

	public static AlphaIdVO getAlphaId(Long alphaIdId) {
		if (alphaIdId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getAlphaId(getAuthentication(), alphaIdId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getApplicationRootUrl() {
		try {
			HttpServletRequest request = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
			URL reconstructedUrl = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), "");
			return reconstructedUrl.toString();
		} catch (Exception e) {
			return getHttpBaseUrl();
		}
	}

	public static ApplicationScopeBean getApplicationScopeBean(HttpServletRequest request) {
		if (request != null) {
			return getApplicationScopeBean(request.getSession());
		}
		return null;
	}

	public static ApplicationScopeBean getApplicationScopeBean(HttpSession session) {
		if (session != null) {
			return (ApplicationScopeBean) session.getServletContext().getAttribute("applicationScopeBean");
		}
		return null;
	}


	public static AspVO getAsp(Long aspId) {
		if (aspId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getAsp(getAuthentication(), aspId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static AspAtcCodeVO getAspAtcCode(Long aspAtcCodeId) {
		if (aspAtcCodeId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getAspAtcCode(getAuthentication(), aspAtcCodeId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static AspSubstanceVO getAspSubstance(Long aspSubstanceId) {
		if (aspSubstanceId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getAspSubstance(getAuthentication(), aspSubstanceId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static AuthenticationVO getAuthentication() {
		SessionScopeBean sessionScopeBean = getSessionScopeBean();
		if (sessionScopeBean != null) {
			return sessionScopeBean.getAuthentication();
		}
		return null;
	}

	public static ArrayList<SelectItem> getAuthenticationTypes() {
		ArrayList<SelectItem> methods;
		Collection<AuthenticationTypeVO> methodVOs = null;
		try {
			methodVOs = getServiceLocator().getSelectionSetService().getAuthenticationTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (methodVOs != null) {
			methods = new ArrayList<SelectItem>(methodVOs.size());
			Iterator<AuthenticationTypeVO> it = methodVOs.iterator();
			while (it.hasNext()) {
				AuthenticationTypeVO methodVO = it.next();
				methods.add(new SelectItem(methodVO.getMethod().name(), methodVO.getName()));
				// putSelectionSetServiceCache(methodVO.getMethod(), methodVO);
			}
		} else {
			methods = new ArrayList<SelectItem>();
		}
		return methods;
	}

	public static ArrayList<SelectItem> getAvailableHyperlinkCategories(HyperlinkModule module, Long categoryId) {
		ArrayList<SelectItem> categories;
		Collection<HyperlinkCategoryVO> categoryVOs = null;
		try {
			categoryVOs = getServiceLocator().getSelectionSetService().getHyperlinkCategories(getAuthentication(), module, categoryId);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (categoryVOs != null) {
			categories = new ArrayList<SelectItem>(categoryVOs.size());
			Iterator<HyperlinkCategoryVO> it = categoryVOs.iterator();
			while (it.hasNext()) {
				HyperlinkCategoryVO categoryVO = it.next();
				categories.add(new SelectItem(categoryVO.getId().toString(), categoryVO.getName()));
				// putSelectionSetServiceCache(categoryVO.getId(), categoryVO);
			}
		} else {
			categories = new ArrayList<SelectItem>();
		}
		return categories;
	}

	public static ArrayList<SelectItem> getAvailableJournalCategories(JournalModule module, Long categoryId) {
		ArrayList<SelectItem> categories;
		Collection<JournalCategoryVO> categoryVOs = null;
		try {
			categoryVOs = getServiceLocator().getSelectionSetService().getJournalCategories(getAuthentication(), module, categoryId);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (categoryVOs != null) {
			categories = new ArrayList<SelectItem>(categoryVOs.size());
			Iterator<JournalCategoryVO> it = categoryVOs.iterator();
			while (it.hasNext()) {
				JournalCategoryVO categoryVO = it.next();
				categories.add(new SelectItem(categoryVO.getId().toString(), categoryVO.getName()));
				// putSelectionSetServiceCache(categoryVO.getId(), categoryVO);
			}
		} else {
			categories = new ArrayList<SelectItem>();
		}
		return categories;
	}

	public static ArrayList<SelectItem> getAvailableProbandAddressTypes(Long probandId, Long typeId) {
		ArrayList<SelectItem> types;
		Collection<AddressTypeVO> typeVOs = null;
		try {
			typeVOs = getServiceLocator().getSelectionSetService().getAvailableProbandAddressTypes(getAuthentication(), null, null, probandId, typeId);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (typeVOs != null) {
			types = new ArrayList<SelectItem>(typeVOs.size());
			Iterator<AddressTypeVO> it = typeVOs.iterator();
			while (it.hasNext()) {
				AddressTypeVO typeVO = it.next();
				types.add(new SelectItem(typeVO.getId().toString(), typeVO.getName()));
				// putSelectionSetServiceCache(typeVO.getId(), typeVO);
			}
		} else {
			types = new ArrayList<SelectItem>();
		}
		return types;
	}

	public static ArrayList<SelectItem> getAvailableProbandContactDetailTypes(Long probandId, Long typeId) {
		ArrayList<SelectItem> types;
		Collection<ContactDetailTypeVO> typeVOs = null;
		try {
			typeVOs = getServiceLocator().getSelectionSetService().getAvailableProbandContactDetailTypes(getAuthentication(), null, null, probandId, typeId);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (typeVOs != null) {
			types = new ArrayList<SelectItem>(typeVOs.size());
			Iterator<ContactDetailTypeVO> it = typeVOs.iterator();
			while (it.hasNext()) {
				ContactDetailTypeVO typeVO = it.next();
				types.add(new SelectItem(typeVO.getId().toString(), typeVO.getName()));
			}
		} else {
			types = new ArrayList<SelectItem>();
		}
		return types;
	}

	public static ArrayList<SelectItem> getAvailableStaffAddressTypes(Long staffId, Long typeId) {
		ArrayList<SelectItem> types;
		Collection<AddressTypeVO> typeVOs = null;
		try {
			typeVOs = getServiceLocator().getSelectionSetService().getAvailableStaffAddressTypes(getAuthentication(), staffId, typeId);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (typeVOs != null) {
			types = new ArrayList<SelectItem>(typeVOs.size());
			Iterator<AddressTypeVO> it = typeVOs.iterator();
			while (it.hasNext()) {
				AddressTypeVO typeVO = it.next();
				types.add(new SelectItem(typeVO.getId().toString(), typeVO.getName()));
				// putSelectionSetServiceCache(typeVO.getId(), typeVO);
			}
		} else {
			types = new ArrayList<SelectItem>();
		}
		return types;
	}

	public static ArrayList<SelectItem> getAvailableStaffContactDetailTypes(Long staffId, Long typeId) {
		ArrayList<SelectItem> types;
		Collection<ContactDetailTypeVO> typeVOs = null;
		try {
			typeVOs = getServiceLocator().getSelectionSetService().getAvailableStaffContactDetailTypes(getAuthentication(), staffId, typeId);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (typeVOs != null) {
			types = new ArrayList<SelectItem>(typeVOs.size());
			Iterator<ContactDetailTypeVO> it = typeVOs.iterator();
			while (it.hasNext()) {
				ContactDetailTypeVO typeVO = it.next();
				types.add(new SelectItem(typeVO.getId().toString(), typeVO.getName()));
			}
		} else {
			types = new ArrayList<SelectItem>();
		}
		return types;
	}

	public static Long getBankAccountCount(Long probandId) {
		if (probandId != null) {
			try {
				return WebUtil.getServiceLocator().getProbandService().getBankAccountCount(WebUtil.getAuthentication(), probandId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				WebUtil.publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Boolean getBooleanParamValue(GetParamNames paramName) {
		return stringToBoolean(getParamValue(paramName));
	}

	public static ArrayList<SelectItem> getBooleans(boolean inverse, boolean triState) {
		ArrayList<SelectItem> booleans;
		Collection<BooleanVO> booleanVOs = null;
		try {
			booleanVOs = getServiceLocator().getSelectionSetService().getBooleans(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (booleanVOs != null) {
			booleans = new ArrayList<SelectItem>(booleanVOs.size());
			Iterator<BooleanVO> it = booleanVOs.iterator();
			while (it.hasNext()) {
				BooleanVO booleanVO = it.next();
				booleans.add(new SelectItem(((Boolean) (inverse ? !booleanVO.getValue() : booleanVO.getValue())).toString(), booleanVO.getName()));
				// putSelectionSetServiceCache(booleanVO.getValue(), booleanVO);
			}
			if (triState) {
				booleans.add(0, new SelectItem(CommonUtil.NO_SELECTION_VALUE, ""));
			}
		} else {
			booleans = new ArrayList<SelectItem>();
		}
		return booleans;
	}

	public static CalendarWeekVO getCalendarWeek(Date date) {
		try {
			return getServiceLocator().getToolsService().getCalendarWeek(date);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return null;
	}

	public static String getCalendarWeekString(Date date) {
		if (date != null) {
			CalendarWeekVO calendarWeek = getCalendarWeek(date);
			if (calendarWeek != null) {
				return Messages.getMessage(MessageCodes.CALENDAR_WEEK, Integer.toString(calendarWeek.getWeek()), Integer.toString(calendarWeek.getYear()));
			}
		}
		return null;
	}

	public static ContactDetailTypeVO getContactDetailType(Long detailTypeId) {
		// ContactDetailTypeVO type = null; // (ContactDetailTypeVO) getSelectionSetServiceCache(ContactDetailTypeVO.class, id);
		// if (type == null) {
		if (detailTypeId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getContactDetailType(getAuthentication(), detailTypeId);
				// putSelectionSetServiceCache(id, type);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return type;
	}

	public static CourseOutVO getCourse(Long courseId, Integer maxInstances, Integer maxPrecedingCoursesDepth, Integer maxRenewalsDepth) {
		if (courseId != null) {
			try {
				return getServiceLocator().getCourseService().getCourse(getAuthentication(), courseId, maxInstances, maxPrecedingCoursesDepth, maxRenewalsDepth);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static CourseCategoryVO getCourseCategory(Long categoryId) {
		// CourseCategoryVO category = null; // (CourseCategoryVO) getSelectionSetServiceCache(CourseCategoryVO.class, id);
		// if (category == null) {
		if (categoryId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getCourseCategory(getAuthentication(), categoryId);
				// putSelectionSetServiceCache(id, category);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return category;
	}

	public static Boolean getCourseExpired(Date today, CourseInVO course) {
		if (course != null && course.isExpires()) {
			return getExpired(today, course.getStop(), course.getValidityPeriod(), course.getValidityPeriodDays());
		}
		return null;
	}

	public static Boolean getCourseExpired(Date today, CourseOutVO course) {
		Date expiration;
		if (course != null && (expiration = course.getExpiration()) != null) {
			return getExpired(today, expiration);
		}
		return null;
	}

	public static Long getCourseInventoryBookingCount(Long courseId) {
		if (courseId != null) {
			try {
				return getServiceLocator().getCourseService().getCourseInventoryBookingCount(getAuthentication(), courseId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Collection<CourseParticipationStatusEntryOutVO> getCourseParticipationStatusEntries(Long staffId, Long courseId) {
		try {
			return getServiceLocator().getCourseService().getCourseParticipationStatusEntryList(getAuthentication(), staffId, courseId, null, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return new ArrayList<CourseParticipationStatusEntryOutVO>();
	}

	public static CourseParticipationStatusEntryOutVO getCourseParticipationStatusEntry(Long courseParticipationStatusEntryId) {
		if (courseParticipationStatusEntryId != null) {
			try {
				return getServiceLocator().getCourseService().getCourseParticipationStatusEntry(getAuthentication(), courseParticipationStatusEntryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static CourseParticipationStatusEntryOutVO getCourseParticipationStatusEntry(Long staffId, Long courseId) {
		try {
			return getCourseParticipationStatusEntries(staffId, courseId).iterator().next();
		} catch (NoSuchElementException e) {
		}
		return null;
	}

	public static Long getCourseParticipationStatusEntryCount(Long staffId, Long courseId) {
		try {
			return getServiceLocator().getCourseService().getCourseParticipationStatusEntryCount(getAuthentication(), staffId, courseId, null);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return null;
	}

	public static CriteriaOutVO getCriteria(Long criteriaId) {
		if (criteriaId != null) {
			try {
				return getServiceLocator().getSearchService().getCriteria(getAuthentication(), criteriaId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getCurrencySymbol() {
		try {
			return getServiceLocator().getToolsService().getCurrencySymbol();
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return "";
	}

	public static StreamedContent getCvPdfStreamedContent(Long staffId) throws Exception {
		if (staffId != null) {
			try {
				CvPDFVO cv = getServiceLocator().getStaffService().renderCvPDF(getAuthentication(), staffId);
				return new DefaultStreamedContent(new ByteArrayInputStream(cv.getDocumentDatas()), cv.getContentType().getMimeType(), cv.getFileName());
			} catch (ServiceException e) {
				throw e;
			} catch (AuthenticationException e) {
				publishException(e);
				throw e;
			} catch (AuthorisationException e) {
				throw e;
			} catch (IllegalArgumentException e) {
				throw e;
			}
		}
		return null;
	}

	public static CvPositionOutVO getCvPosition(Long cvPositionId) {
		if (cvPositionId != null) {
			try {
				return getServiceLocator().getStaffService().getCvPosition(getAuthentication(), cvPositionId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getCvPositionListCount(Long staffId) {
		if (staffId != null) {
			try {
				return getServiceLocator().getStaffService().getCvPositionCount(getAuthentication(), staffId, null);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static CvSectionVO getCvSection(Long sectionId) {
		// CvSectionVO cvSection = null; // (CvSectionVO) getSelectionSetServiceCache(CvSectionVO.class, id);
		// if (cvSection == null) {
		if (sectionId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getCvSection(getAuthentication(), sectionId);
				// putSelectionSetServiceCache(id, cvSection);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return cvSection;
	}

	public static ArrayList<SelectItem> getCvSections(Long sectionId) {
		ArrayList<SelectItem> cvSections;
		Collection<CvSectionVO> sectionVOs = null;
		try {
			sectionVOs = getServiceLocator().getSelectionSetService().getCvSections(getAuthentication(), sectionId);
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (sectionVOs != null) {
			cvSections = new ArrayList<SelectItem>(sectionVOs.size());
			Iterator<CvSectionVO> it = sectionVOs.iterator();
			while (it.hasNext()) {
				CvSectionVO sectionVO = it.next();
				cvSections.add(new SelectItem(sectionVO.getId().toString(), sectionVO.getName()));
			}
		} else {
			cvSections = new ArrayList<SelectItem>();
		}
		return cvSections;
	}

	public static ArrayList<SelectItem> getDBModules() {
		ArrayList<SelectItem> dbModules;
		Collection<DBModuleVO> dbModuleVOs = null;
		try {
			dbModuleVOs = getServiceLocator().getSelectionSetService().getDBModules(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (dbModuleVOs != null) {
			dbModules = new ArrayList<SelectItem>(dbModuleVOs.size());
			Iterator<DBModuleVO> it = dbModuleVOs.iterator();
			while (it.hasNext()) {
				DBModuleVO dbModuleVO = it.next();
				dbModules.add(new SelectItem(dbModuleVO.getModule().name(), dbModuleVO.getName()));
			}
		} else {
			dbModules = new ArrayList<SelectItem>();
		}
		return dbModules;
	}

	public static DBModuleVO getDBModuleVO(DBModule module) {
		if (module != null) {
			try {
				return getServiceLocator().getToolsService().getLocalizedDBModule(getAuthentication(), module);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Locale getDefaultLocale() {
		try {
			return CommonUtil.localeFromString(getServiceLocator().getToolsService().getDefaultLocale());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return Locale.getDefault();
	}

	public static TimeZone getDefaultTimezone() {
		try {
			return CommonUtil.timeZoneFromString(getServiceLocator().getToolsService().getDefaultTimeZone());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return TimeZone.getDefault();
	}

	public static ArrayList<SelectItem> getDiagnoses(Long probandId) {
		ArrayList<SelectItem> diagnoses;
		Collection<DiagnosisOutVO> diagnosisVOs = null;
		if (probandId != null) {
			try {
				diagnosisVOs = getServiceLocator().getProbandService().getDiagnosisList(getAuthentication(), probandId, null);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		if (diagnosisVOs != null) {
			diagnoses = new ArrayList<SelectItem>(diagnosisVOs.size());
			Iterator<DiagnosisOutVO> it = diagnosisVOs.iterator();
			while (it.hasNext()) {
				DiagnosisOutVO diagnosisVO = it.next();
				diagnoses.add(new SelectItem(Long.toString(diagnosisVO.getId()), Messages.getMessage(MessageCodes.DIAGNOSIS_SELECTITEM_LABEL, diagnosisVO.getName(),
						DateUtil.getDateStartStopString(diagnosisVO.getStart(), diagnosisVO.getStop()))));
			}
		} else {
			diagnoses = new ArrayList<SelectItem>();
		}
		return diagnoses;
	}

	public static Long getDiagnosisCount(Long probandId) {
		if (probandId != null) {
			try {
				return WebUtil.getServiceLocator().getProbandService().getDiagnosisCount(WebUtil.getAuthentication(), probandId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				WebUtil.publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static DutyRosterTurnOutVO getDutyRosterTurn(Long dutyRosterTurnId) {
		if (dutyRosterTurnId != null) {
			try {
				return getServiceLocator().getStaffService().getDutyRosterTurn(getAuthentication(), dutyRosterTurnId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ArrayList<SelectItem> getDutyRosterTurnFilterCalendars(Long trialDepartmentId, Long trialId, Long staffId) {
		ArrayList<SelectItem> filterCalendars;
		Collection<String> calendarStrings = null;
		try {
			calendarStrings = getServiceLocator().getTrialService().getCalendars(getAuthentication(), trialDepartmentId, staffId, trialId, null, null);
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (ServiceException e) {
		} catch (IllegalArgumentException e) {
		}
		if (calendarStrings != null) {
			filterCalendars = new ArrayList<SelectItem>(calendarStrings.size());
			Iterator<String> it = calendarStrings.iterator();
			while (it.hasNext()) {
				String calendar = it.next();
				filterCalendars.add(new SelectItem(calendar, calendar));
			}
		} else {
			filterCalendars = new ArrayList<SelectItem>();
		}
		filterCalendars.add(0, new SelectItem(CommonUtil.NO_SELECTION_VALUE, ""));
		return filterCalendars;
	}

	public static ArrayList<SelectItem> getDutyRosterTurnFilterTitles(Long trialDepartmentId, Long trialId, Long staffId) {
		ArrayList<SelectItem> filterTitles;
		Collection<String> titleStrings = null;
		try {
			titleStrings = getServiceLocator().getTrialService().getDutyRosterTurnTitles(getAuthentication(), trialDepartmentId, staffId, trialId, null, null);
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (ServiceException e) {
		} catch (IllegalArgumentException e) {
		}
		if (titleStrings != null) {
			filterTitles = new ArrayList<SelectItem>(titleStrings.size());
			Iterator<String> it = titleStrings.iterator();
			while (it.hasNext()) {
				String title = it.next();
				filterTitles.add(new SelectItem(title, title));
			}
		} else {
			filterTitles = new ArrayList<SelectItem>();
		}
		filterTitles.add(0, new SelectItem(CommonUtil.NO_SELECTION_VALUE, ""));
		return filterTitles;
	}

	public static ECRFOutVO getEcrf(Long ecrfId) {
		if (ecrfId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrf(getAuthentication(), ecrfId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getEcrfCount(Long trialId) {
		// PSFVO psf = new PSFVO();
		// psf.setPageSize(0);
		// Long count = null;
		if (trialId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfCount(getAuthentication(), trialId, null, null, null);
				// count = psf.getRowCount();
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ECRFFieldOutVO getEcrfField(Long ecrfFieldId) {
		if (ecrfFieldId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfField(getAuthentication(), ecrfFieldId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getEcrfFieldCount(Long trialId, Long ecrfId) {
		if (trialId != null || ecrfId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfFieldCount(getAuthentication(), trialId, ecrfId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ECRFFieldStatusEntryOutVO getEcrfFieldStatusEntry(Long id) {
		if (id != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfFieldStatusEntry(getAuthentication(), id);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getEcrfFieldStatusEntryCount(ECRFFieldStatusQueue queue, Long trialId, Long probandListEntryId, Long ecrfId, boolean last) {
		if (trialId != null || probandListEntryId != null || ecrfId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfFieldStatusEntryCount(getAuthentication(), queue, trialId, probandListEntryId, ecrfId, last);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getEcrfFieldStatusQueueName(ECRFFieldStatusQueue queue) {
		if (queue != null) {
			switch (queue) {
				case ANNOTATION:
					return Messages.getString(MessageCodes.ANNOTATION_ECRF_FIELD_STATUS_QUEUE_NAME);
				case VALIDATION:
					return Messages.getString(MessageCodes.VALIDATION_ECRF_FIELD_STATUS_QUEUE_NAME);
				case QUERY:
					return Messages.getString(MessageCodes.QUERY_ECRF_FIELD_STATUS_QUEUE_NAME);
				default:

			}
		}
		return "";
	}

	public static ECRFFieldValueOutVO getEcrfFieldValue(Long id) {
		if (id != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfFieldValueById(getAuthentication(), id);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getEcrfFieldValueCount(ECRFFieldOutVO ecrfField, boolean excludeAuditTrail) {
		if (ecrfField != null) {
			try {
				return getServiceLocator().getInputFieldService().getEcrfFieldValueCount(getAuthentication(), ecrfField.getId(), excludeAuditTrail);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}



	public static ECRFProgressVO getEcrfProgress(Long ecrfId, Long listEntryId, boolean sectionDetail) {
		if (ecrfId != null && listEntryId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfProgress(getAuthentication(), listEntryId, ecrfId, sectionDetail);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ECRFProgressSummaryVO getEcrfProgressSummary(Long listEntryId, boolean ecrfDetail, boolean sectionDetail) {
		if (listEntryId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfProgressSummary(getAuthentication(), listEntryId, ecrfDetail, sectionDetail);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ECRFStatusEntryVO getEcrfStatusEntry(Long ecrfId, Long listEntryId) {
		if (ecrfId != null && listEntryId != null) {
			try {
				return getServiceLocator().getTrialService().getEcrfStatusEntry(getAuthentication(), ecrfId, listEntryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ECRFStatusTypeVO getEcrfStatusType(Long statusTypeId) {
		// TrialStatusTypeVO statusType = null; // (TrialStatusTypeVO) getSelectionSetServiceCache(TrialStatusTypeVO.class, id);
		// if (statusType == null) {
		if (statusTypeId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getEcrfStatusType(getAuthentication(), statusTypeId);
				// putSelectionSetServiceCache(id, statusType);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return statusType;
	}

	public static String getEmailDomainName() {
		try {
			return getServiceLocator().getToolsService().getEmailDomainName();
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return "";
	}

	public static ArrayList<Enum> getEnumList(String value,Class enumeration) {

		ArrayList<Enum> result;
		if (value != null && value.length() > 0) {
			String[] list = EL_ENUM_LIST_REGEXP.split(value, -1);
			result = new ArrayList<Enum>(list.length);
			for (int i = 0; i < list.length; i++) {
				String name = list[i].trim();
				if (name.length() > 0) {
					result.add(Enum.valueOf(enumeration, name));
				}
			}
		} else {
			result = new ArrayList<Enum>();
		}
		return result;
	}


	public static ArrayList<SelectItem> getEventImportances() {
		ArrayList<SelectItem> importances;
		Collection<EventImportanceVO> importanceVOs = null;
		try {
			importanceVOs = getServiceLocator().getSelectionSetService().getEventImportances(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (importanceVOs != null) {
			importances = new ArrayList<SelectItem>(importanceVOs.size());
			Iterator<EventImportanceVO> it = importanceVOs.iterator();
			while (it.hasNext()) {
				EventImportanceVO importanceVO = it.next();
				importances.add(new SelectItem(importanceVO.getImportance().name(), importanceVO.getName()));
				// putSelectionSetServiceCache(importanceVO.getImportance(), importanceVO);
			}
		} else {
			importances = new ArrayList<SelectItem>();
		}
		return importances;
	}

	public static String getExpirationDueInString(Date today, Date expiration) {
		Boolean expired = getExpired(today, expiration);
		if (expired != null) {
			if (expired) {
				return Messages.getMessage(MessageCodes.OVERDUE_LABEL, DateUtil.getExpirationDurationString(expiration, today));
			} else {
				return DateUtil.getExpirationDurationString(today, expiration);
			}
		}
		return "";
	}

	private static Boolean getExpired(Date today, Date expiration) {
		if (today != null && expiration != null) {
			if (today.compareTo(expiration) > 0) {
				return true;
			} else {
				return false;
			}
		}
		return null;
	}

	public static Boolean getExpired(Date today, Date date, VariablePeriod validityPeriod, Long validityPeriodDays) {
		if (today != null && date != null && validityPeriod != null) {
			return getExpired(today, addIntervals(date, validityPeriod, validityPeriodDays, 1));
		}
		return null;
	}

	public static Long getFileCount(FileModule module, Long id) {
		// PSFVO psf = new PSFVO();
		// psf.setPageSize(0);
		// Long count = null;
		if (module != null && id != null) {
			try {
				return getServiceLocator().getFileService().getFileCount(getAuthentication(), module, id, null, null);
				// count = psf.getRowCount();
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getHttpBaseUrl() {
		try {
			return getServiceLocator().getToolsService().getHttpBaseUrl();
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return "";
	}

	public static String getHttpDomainName() {
		try {
			return getServiceLocator().getToolsService().getHttpDomainName();
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return "";
	}

	public static String getHttpHost() {
		try {
			return getServiceLocator().getToolsService().getHttpHost();
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return "";
	}

	public static String getHttpScheme() {
		try {
			return getServiceLocator().getToolsService().getHttpScheme();
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return "";
	}

	public static HyperlinkCategoryVO getHyperlinkCategory(Long categoryId) {
		// HyperlinkCategoryVO category = null; // (HyperlinkCategoryVO) getSelectionSetServiceCache(HyperlinkCategoryVO.class, id);
		// if (category == null) {
		if (categoryId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getHyperlinkCategory(getAuthentication(), categoryId);
				// putSelectionSetServiceCache(id, category);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return category;
	}

	public static Long getHyperlinkCount(HyperlinkModule module, Long id) {
		// PSFVO psf = new PSFVO();
		// psf.setPageSize(0);
		// Long count = null;
		if (module != null && id != null) {
			try {
				return getServiceLocator().getHyperlinkService().getHyperlinkCount(getAuthentication(), module, id, null);
				// count = psf.getRowCount();
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getIdentityString(UserOutVO user) {
		if (user != null) {
			StaffOutVO identity = user.getIdentity();
			if (identity != null) {
				return CommonUtil.staffOutVOToString(identity);
			} else {
				return CommonUtil.userOutVOToString(user);
			}
		}
		return "";
	}

	public static String getIdentityUserString(UserOutVO user) {
		if (user != null) {
			StaffOutVO identity = user.getIdentity();
			if (identity != null) {
				return Messages.getMessage(MessageCodes.USER_IDENTITY_LABEL, CommonUtil.staffOutVOToString(identity), CommonUtil.userOutVOToString(user));
			} else {
				return CommonUtil.userOutVOToString(user);
			}
		}
		return "";
	}

	public static InputFieldOutVO getInputField(Long inputFieldId) {
		if (inputFieldId != null) {
			try {
				return getServiceLocator().getInputFieldService().getInputField(getAuthentication(), inputFieldId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getInputFieldIcon(InputFieldOutVO inputField) {
		if (inputField != null) {
			return INPUT_FIELD_TYPE_ICON_STYLECLASS_PREFIX + inputField.getFieldType().getType().getValue();
		}
		return "";
	}

	public static InputFieldSelectionSetValueOutVO getInputFieldSelectionSetValue(Long selectionSetValueId) {
		if (selectionSetValueId != null) {
			try {
				return getServiceLocator().getInputFieldService().getSelectionSetValue(getAuthentication(), selectionSetValueId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ArrayList<SelectItem> getInputFieldTypes() {
		ArrayList<SelectItem> fieldTypes;
		Collection<InputFieldTypeVO> fieldTypeVOs = null;
		try {
			fieldTypeVOs = getServiceLocator().getSelectionSetService().getInputFieldTypes(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (fieldTypeVOs != null) {
			fieldTypes = new ArrayList<SelectItem>(fieldTypeVOs.size());
			Iterator<InputFieldTypeVO> it = fieldTypeVOs.iterator();
			while (it.hasNext()) {
				InputFieldTypeVO fieldTypeVO = it.next();
				fieldTypes.add(new SelectItem(fieldTypeVO.getType().name(), fieldTypeVO.getName()));
				// putSelectionSetServiceCache(fieldTypeVO.getType(), fieldTypeVO);
			}
		} else {
			fieldTypes = new ArrayList<SelectItem>();
		}
		return fieldTypes;
	}

	public static InquiryOutVO getInquiry(Long inquiryId) {
		if (inquiryId != null) {
			try {
				return getServiceLocator().getTrialService().getInquiry(getAuthentication(), inquiryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getInquiryCount(Long trialId, Boolean active, Boolean signupActive) {
		if (trialId != null) {
			try {
				return getServiceLocator().getProbandService().getInquiryCount(getAuthentication(), trialId, active, signupActive);
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (ServiceException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static InquiryValueOutVO getInquiryValue(Long id) {
		if (id != null) {
			try {
				return getServiceLocator().getProbandService().getInquiryValueById(getAuthentication(), id);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getInquiryValueCount(InquiryOutVO inquiry) {
		if (inquiry != null) {
			try {
				return getServiceLocator().getInputFieldService().getInquiryValueCount(getAuthentication(), inquiry.getId());
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getInstanceName() {
		try {
			return getServiceLocator().getToolsService().getInstanceName();
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		return "";
	}

	public static InventoryOutVO getInventory(Long inventoryId, Integer maxInstances, Integer maxParentDepth) {
		if (inventoryId != null) {
			try {
				return getServiceLocator().getInventoryService().getInventory(getAuthentication(), inventoryId, maxInstances, maxParentDepth);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static InventoryBookingOutVO getInventoryBooking(Long inventoryBookingId) {
		if (inventoryBookingId != null) {
			try {
				return getServiceLocator().getInventoryService().getInventoryBooking(getAuthentication(), inventoryBookingId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getInventoryBookingCount(Long inventoryId) {
		if (inventoryId != null) {
			try {
				return getServiceLocator().getInventoryService().getInventoryBookingCount(getAuthentication(), inventoryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ArrayList<SelectItem> getInventoryBookingFilterCalendars(Long inventoryDepartmentId, Long inventoryId, Long probandId, Long courseId, Long trialId) {
		ArrayList<SelectItem> filterCalendars;
		Collection<String> calendarStrings = null;
		try {
			calendarStrings = getServiceLocator().getInventoryService().getCalendars(getAuthentication(), inventoryDepartmentId, inventoryId, probandId, courseId, trialId, null,
					null);
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (ServiceException e) {
		} catch (IllegalArgumentException e) {
		}
		if (calendarStrings != null) {
			filterCalendars = new ArrayList<SelectItem>(calendarStrings.size());
			Iterator<String> it = calendarStrings.iterator();
			while (it.hasNext()) {
				String calendar = it.next();
				filterCalendars.add(new SelectItem(calendar, calendar));
			}
		} else {
			filterCalendars = new ArrayList<SelectItem>();
		}
		filterCalendars.add(0, new SelectItem(CommonUtil.NO_SELECTION_VALUE, ""));
		return filterCalendars;
	}

	public static InventoryCategoryVO getInventoryCategory(Long categoryId) {
		// InventoryCategoryVO category = null; // (InventoryCategoryVO) getSelectionSetServiceCache(InventoryCategoryVO.class, id);
		// if (category == null) {
		if (categoryId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getInventoryCategory(getAuthentication(), categoryId);
				// putSelectionSetServiceCache(id, category);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return category;
	}

	public static InventoryStatusEntryOutVO getInventoryStatusEntry(Long inventoryStatusEntryId) {
		if (inventoryStatusEntryId != null) {
			try {
				return getServiceLocator().getInventoryService().getInventoryStatusEntry(getAuthentication(), inventoryStatusEntryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getInventoryStatusEntryCount(Long inventoryId) {
		if (inventoryId != null) {
			try {
				return getServiceLocator().getInventoryService().getInventoryStatusEntryCount(getAuthentication(), inventoryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static InventoryStatusTypeVO getInventoryStatusType(Long statusTypeId) {
		// InventoryStatusTypeVO statusType = null; // (InventoryStatusTypeVO) getSelectionSetServiceCache(InventoryStatusTypeVO.class, id);
		// if (statusType == null) {
		if (statusTypeId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getInventoryStatusType(getAuthentication(), statusTypeId);
				// putSelectionSetServiceCache(id, statusType);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return statusType;
	}

	public static Long getInventoryTagValueCount(Long inventoryId) {
		if (inventoryId != null) {
			try {
				return getServiceLocator().getInventoryService().getInventoryTagValueCount(getAuthentication(), inventoryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static JournalCategoryVO getJournalCategory(Long categoryId) {
		// JournalCategoryVO category = null; // (JournalCategoryVO) getSelectionSetServiceCache(JournalCategoryVO.class, id);
		// if (category == null) {
		if (categoryId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getJournalCategory(getAuthentication(), categoryId);
				// putSelectionSetServiceCache(id, category);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
		// }
		// return category;
	}

	public static Long getJournalCount(JournalModule module, Long id) {
		// PSFVO psf = new PSFVO();
		// psf.setPageSize(0);
		// Long count = null;
		if (module != null && id != null) {
			try {
				return getServiceLocator().getJournalService().getJournalCount(getAuthentication(), module, id);
				// count = psf.getRowCount();
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static JournalEntryOutVO getJournalEntry(Long journalEntryId) {
		if (journalEntryId != null) {
			try {
				return getServiceLocator().getJournalService().getJournalEntry(getAuthentication(), journalEntryId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static JournalModuleVO getJournalModule(JournalModule module) {
		if (module != null) {
			try {
				return getServiceLocator().getToolsService().getLocalizedJournalModule(getAuthentication(), module);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static ArrayList<SelectItem> getJournalModules() {
		ArrayList<SelectItem> journalModules;
		Collection<JournalModuleVO> journalModuleVOs = null;
		try {
			journalModuleVOs = getServiceLocator().getSelectionSetService().getJournalModules(getAuthentication());
		} catch (ServiceException e) {
		} catch (AuthenticationException e) {
			publishException(e);
		} catch (AuthorisationException e) {
		} catch (IllegalArgumentException e) {
		}
		if (journalModuleVOs != null) {
			journalModules = new ArrayList<SelectItem>(journalModuleVOs.size());
			Iterator<JournalModuleVO> it = journalModuleVOs.iterator();
			while (it.hasNext()) {
				JournalModuleVO journalModuleVO = it.next();
				journalModules.add(new SelectItem(journalModuleVO.getModule().name(), journalModuleVO.getName()));
			}
		} else {
			journalModules = new ArrayList<SelectItem>();
		}
		return journalModules;
	}

	public static LecturerOutVO getLecturer(Long lecturerId) {
		if (lecturerId != null) {
			try {
				return getServiceLocator().getCourseService().getLecturer(getAuthentication(), lecturerId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getLecturerCount(Long courseId, Long competenceId) {
		if (courseId != null || competenceId != null) {
			try {
				return getServiceLocator().getCourseService().getLecturerCount(getAuthentication(), courseId, competenceId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Locale getLocale() {
		SessionScopeBean sessionScopeBean = getSessionScopeBean();
		if (sessionScopeBean != null) {
			return sessionScopeBean.getLocale();
		}
		return null;
	}

	public static Long getLongParamValue(GetParamNames paramName) {
		return stringToLong(getParamValue(paramName));
	}

	public static MaintenanceScheduleItemOutVO getMaintenanceScheduleItem(Long maintenanceScheduleItemId) {
		if (maintenanceScheduleItemId != null) {
			try {
				return getServiceLocator().getInventoryService().getMaintenanceScheduleItem(getAuthentication(), maintenanceScheduleItemId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getMaintenanceScheduleItemCount(Long inventoryId) {
		if (inventoryId != null) {
			try {
				return getServiceLocator().getInventoryService().getMaintenanceScheduleItemCount(getAuthentication(), inventoryId, null);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {

			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static MaintenanceTypeVO getMaintenanceType(Long maintenanceTypeId) {
		if (maintenanceTypeId != null) {
			try {
				return getServiceLocator().getSelectionSetService().getMaintenanceType(getAuthentication(), maintenanceTypeId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static Long getMedicationCount(Long probandId, Long diagnosisId, Long procedureId) {
		if (probandId != null || diagnosisId != null || procedureId != null) {
			try {
				return WebUtil.getServiceLocator().getProbandService().getMedicationCount(WebUtil.getAuthentication(), probandId, diagnosisId, procedureId);
			} catch (ServiceException e) {
			} catch (AuthenticationException e) {
				WebUtil.publishException(e);
			} catch (AuthorisationException e) {
			} catch (IllegalArgumentException e) {
			}
		}
		return null;
	}

	public static String getModifiedAnnotation(long version, UserOutVO user, Date timestamp) {
		if (version == 0) {
			if (user != null) {
				StaffOutVO identity = user.getIdentity();
				if (identity != null) {
					return Messages.getMessage(MessageCodes.CREATED, CommonUtil.staffOutVOToString(identity), DateUtil.getDateTimeFormat().format(timestamp));
				} else {
					return Messages.getMessage(MessageCodes.CREATED, CommonUt