/** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.eclipse.datatools.connectivity.oda.openmrs.ui.impl; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; // TODO: Auto-generated Javadoc /** * This Class helps the data set pages with building and extracting information * from the different properties and query strings. */ public class Utils { private static Logger log = Logger.getLogger(Utils.class.getName()); /** * Gets the aggregate name for a given token from the token storage. * * @param token the token to get the aggregate for * @param tokenProperty the token storage * * @return the aggregate name for the desired token */ public static String getAggregateNameForTokenFromTokenProperty( String token, String tokenProperty) { log.info(token); log.info(tokenProperty); if (!tokenProperty.contains(token)) return null; // iterate through the different tokens and their modifiers String[] breakup = tokenProperty.split("\\|"); for (int i = 0; i < breakup.length; i++) { // check if we're at the part that has the token we are looking for if (breakup[i].contains("{" + token)) { String aggregate = breakup[i].split(" ")[0]; log.info("Aggregate: " + aggregate); return aggregate; } } return null; } /** * Gets the aggregate value for a given token from token storage. * * @param token the token to get the aggregate value for * @param tokenProperty the token storage * * @return the aggregate value for the desired token */ public static String getAggregateValueForTokenFromTokenProperty( String token, String tokenProperty) { log.info(token); log.info(tokenProperty); if (!tokenProperty.contains(token)) return null; // iterate through the different tokens and their modifiers String[] breakup = tokenProperty.split("\\|"); for (int i = 0; i < breakup.length; i++) { // check if we're at the part that has the token we are looking for if (breakup[i].contains("{" + token)) { String value = breakup[i].split(" ")[1]; log.info("Aggregate value: " + value); return value; } } return null; } /** * Replace the aggregate name in the query for a give token. * * @param aggregateToken the token to replace the aggregate name for * @param aggregate the new aggregate name * @param tokenProperties the token query * * @return the new token query */ public static String replaceAggregateNameForToken(String aggregateToken, String aggregate, String tokenProperties) { // split up the tokens String[] tokens = tokenProperties.split("\\|"); String query = ""; // rebuild the query for (String token : tokens) { if (token.contains("{" + aggregateToken + "}")) { token = aggregate + token.substring(token.indexOf(" ")); } query += "|" + token; } return query.substring(1); } /** * Replace the aggregate value in the query for a given token. * * @param aggregateToken the token to replace the aggregate value for * @param value the new aggregate value * @param tokenProperties the token query * * @return the new token query */ public static String replaceAggregateValueForToken(String aggregateToken, String value, String tokenProperties) { // split up the tokens String[] tokens = tokenProperties.split("\\|"); String query = ""; String aggregate = getAggregateNameForTokenFromTokenProperty( aggregateToken, tokenProperties); // rebuild the query for (String token : tokens) { if (token.contains("{" + aggregateToken + "}")) { token = aggregate + " " + value + " " + token.substring(token.indexOf("{")); } query += "|" + token; } return query.substring(1); } /** * Uses a map of token names and modifiers as well as the current token part * of the query to create a new token part of the query. * * @param selectedTokenModifiers the map of token names and their modifiers * @param currentTokenStorage the current token storage string * * @return the token string with updated modifier information */ public static String tokenModifiersToPropertyStorageString( Map selectedTokenModifiers, String currentTokenStorage) { log.setLevel(Level.INFO); String storageString = ""; for (Map.Entry entry : selectedTokenModifiers .entrySet()) { String token = entry.getKey(); Modifiers modifiers = entry.getValue(); log.info(storageString); String splits = getSplitsForTokenFromTokenProperty(token, currentTokenStorage); String aggregate = getAggregateNameForTokenFromTokenProperty(token, currentTokenStorage); String value = getAggregateValueForTokenFromTokenProperty(token, currentTokenStorage); if (splits != null) storageString += "|" + aggregate + " " + value + " " + "{" + token + "} " + modifiers.toModifierURLformat() + getSplitsForTokenFromTokenProperty(token, currentTokenStorage); else storageString += "|" + aggregate + " " + value + " " + "{" + token + "} " + modifiers.toModifierURLformat(); } return storageString.substring(1); } /** * Gets the user selected splitters for a specific token given the current * token query * * @param token the token to get the splitters for * @param tokenProperty the current token property string * * @return the splits for the desired token */ public static String getSplitsForTokenFromTokenProperty(String token, String tokenProperty) { log.info(token); log.info(tokenProperty); if (!tokenProperty.contains(token)) return null; // iterate through the different tokens and their splits String[] breakup = tokenProperty.split("\\|"); for (int i = 0; i < breakup.length; i++) { // check if we're at the part that has the token we are looking for if (breakup[i].contains("{" + token)) { // check if there is a split if (breakup[i].contains(":")) { // grab the rest of the string that is after the token return breakup[i].substring(breakup[i].indexOf(":")); } else { return null; } } } return null; } /** * Takes the current token part of the query and adds the desired split for * a given token. * * @param splitToken the token name to add the split to * @param splitToAdd the splitter to add to the token * @param tokenProperties the token properties * * @return the current token property string */ public static String addSplitForToken(String splitToken, String splitToAdd, String tokenProperties) { // split up the tokens String[] tokens = tokenProperties.split("\\|"); String query = ""; // rebuild the query for (String token : tokens) { if (token.contains("{" + splitToken + "}")) { token = token + ":" + splitToAdd; } query += "|" + token; } return query.substring(1); } /** * Removes the splitter for the desired token. * * @param splitToken the token to remove the splitter from * @param splitToRemove the splitter to remove from the token * @param tokenProperties the current token part of the query * * @return the updated token query with the splitter removed */ public static String removeSplitForToken(String splitToken, String splitToRemove, String tokenProperties) { // split up the tokens String[] tokens = tokenProperties.split("\\|"); String query = ""; // rebuild the query for (String token : tokens) { if (token.contains("{" + splitToken + "}")) { token = token.replace(":" + splitToRemove, ""); } query += "|" + token; } return query.substring(1); } /** * Gets the modifier information for the desired token. * * @param token the token to get the modifier information for * @param tokenProperty the current token part of the query * * @return the modifier info for the token */ public static String getModifierInfoForTokenFromTokenProperty(String token, String tokenProperty) { log.info(token); log.info(tokenProperty); if (!tokenProperty.contains(token)) return null; // iterate through the different tokens and their modifiers String[] breakup = tokenProperty.split("\\|"); for (int i = 0; i < breakup.length; i++) { // check if we're at the part that has the token we are looking for if (breakup[i].contains("{" + token)) { // if there is a : right after }, we know that there are no // modifiers if (breakup[i].contains("}:")) return null; // check if there is more data after {token}, if so, there are // modifiers else if (breakup[i].length() > token.length() + 2) { // grab the rest of the string that is after the token if (breakup[i].contains(":")) return breakup[i].substring( breakup[i].indexOf("}") + 1, breakup[i] .indexOf(":")); else return breakup[i] .substring(breakup[i].indexOf("}") + 1); } else { return null; } } } return null; } }