/** * 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.HashMap; import java.util.Iterator; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /** * Helps the ODA interface keep track of modifiers for different tokens. */ public class Modifiers { // keeps track of which modifiers are enabled private Map enabled; // keeps track of the arguments for the different modifiers private Map modifierArguments; // keeps track of the order of the modifiers private Map modifierOrders; private Logger log = Logger.getLogger(ModifierPage.class.getName()); /** * Instantiates a new Modifiers object with a list of modifiers that Logic * Service supports. * * @param modifierNames the modifier names to use with the Logic Service */ public Modifiers(String[] modifierNames) { log.setLevel(Level.INFO); enabled = new HashMap(); modifierArguments = new HashMap(); modifierOrders = new HashMap(); for (int i = 0; i < modifierNames.length; i++) { // initialize all of the modifiers to be disabled enabled.put(modifierNames[i], Boolean.FALSE); // initialize all of the arguments to be empty modifierArguments.put(modifierNames[i], ""); // initialize the order of all of the modifiers to be the order in // which the array was passed modifierOrders.put(i, modifierNames[i]); } } /** * Gets the modifier at certain location in the table. * * @param order the location in the table to query * * @return the modifier name at location */ public String getModifierAtLocation(int order) { return modifierOrders.get(order); } /** * Gets the location for a given modifier name. * * @param modifier the modifier to search for * * @return the location of the modifier */ private int getLocationForModifier(String modifier) { boolean done = false; int location; for (location = 0; location < modifierOrders.size() && !done; location++) { String temp = modifierOrders.get(location); if (modifier.equals(temp)) done = true; } return location - 1; } /** * Moves the provided modifier up one position in the table. Does nothing if * the modifier is at the very top of the table. * * @param modifier the modifier */ public void moveModifierUp(String modifier) { int location = getLocationForModifier(modifier); if (location > 0) { log .info("Modifer: " + modifier + " Current location: " + location); // get the modifier above so we can swap locations String above = getModifierAtLocation(location - 1); log.info("Modifier above: " + above); // swith the order modifierOrders.put(location, above); modifierOrders.put(location - 1, modifier); } } /** * Moves the provided modifier down one position in the table. Does nothing * if the modifier is at the very bottom of the table. * * @param modifier the modifier */ public void moveModifierDown(String modifier) { int location = getLocationForModifier(modifier); if (location < getSize() - 1) { // get the modifier below so we can swap locations String below = getModifierAtLocation(location + 1); // swith the order modifierOrders.put(location, below); modifierOrders.put(location + 1, modifier); } } /** * Gets the size of the modifier table. * * @return the size */ public int getSize() { return modifierOrders.size(); } /** * Checks if a given modifier has been enabled. * * @param modifier the modifier to check for enablement * * @return true, if it is enabled */ public boolean isEnabled(String modifier) { return enabled.get(modifier); } /** * Sets a modifier to enabled. Called from the ModifierPage when the check * box for the modifier is selected. * * @param modifier the modifier to mark as enabled */ public void setEnabled(String modifier) { enabled.put(modifier, true); } /** * Sets a modifier to disabled. Called from the ModifierPage when the check * box for the modifier is deselected. * * @param modifier the modifier to mark as disabled */ public void setDisabled(String modifier) { enabled.put(modifier, false); } /** * Gets the modifier arguments for a given modifier. * * @param modifier the modifier to get the arguments for * * @return the modifier arguments */ public String getModifierArguments(String modifier) { return modifierArguments.get(modifier); } /** * Sets the modifier arguments. * * @param modifier the modifier to set the arguments for * @param arguments the arguments */ public void setModifierArguments(String modifier, String arguments) { modifierArguments.put(modifier, arguments); } /** * For debugging purpose, print the contents of the modifiers */ public void print() { Iterator iterator = enabled.keySet().iterator(); while (iterator.hasNext()) { String temp = iterator.next(); log.info("Modifier Name: " + temp + " Enabled: " + enabled.get(temp) + " Argument: " + modifierArguments.get(temp) + " Order: " + getLocationForModifier(temp)); } } /** * Returns the list of modifiers and their arguments that are enabled in the * appropriate order in a format for sending to the Logic Web Service * * @return all the modifiers in a URL friendly format */ public String toModifierURLformat() { // Iterator iterator = enabled.keySet().iterator(); String formattedModifiers = ""; for (int i = 0; i < modifierOrders.size(); i++) { if (isEnabled(getModifierAtLocation(i))) { if (getModifierArguments(getModifierAtLocation(i)) != "") { formattedModifiers += getModifierAtLocation(i) + " " + getModifierArguments(getModifierAtLocation(i)) + " "; } else { formattedModifiers += getModifierAtLocation(i) + " "; } } } log.info(formattedModifiers); return formattedModifiers; } /** * Check if a certain modifier exists. * * @param modifier the modifier to check for existance * * @return true, if exists */ private boolean doesModifierExist(String modifier) { if (enabled.get(modifier) != null) return true; else return false; } /** * Takes the URL format from a previously created data set and loads the * table based on this URL format. This allows the user's modifier selection * to be persisted and later revisited and altered if desired. * * @param request the URL format of the token modifiers */ public void loadModifierURLformat(String request) { log.info(request); // go through the URL and put the enabled modifiers at the top of the // list with appropriate values String[] breakUp = request.split(" "); // keep track of what order to put the tokens in int top = 0; // go through the modifiers and arguments for (int i = 0; i < breakUp.length; i++) { // make sure the string is one of the modifiers if (doesModifierExist(breakUp[i])) { // enable it setEnabled(breakUp[i]); // we want to move the modifier to the "top" // keep calling moveModifierUp until it is in the correct "top" // location while (getLocationForModifier(breakUp[i]) != top) { moveModifierUp(breakUp[i]); } // increase where the location of top is top++; // if the next string is not a modifier, it should be the // modifier's argument // otherwise, it may be a modifier that does not require an // argument if ((i + 1) < breakUp.length && !doesModifierExist(breakUp[i + 1])) { setModifierArguments(breakUp[i], breakUp[i + 1]); // add to i so the argument is skipped on the next loop i++; } } // make sure the string is one of the modifiers - this is for two // word modifiers else if (doesModifierExist(breakUp[i] + " " + breakUp[i + 1])) { String twoWordModifier = breakUp[i] + " " + breakUp[i + 1]; // enable it setEnabled(twoWordModifier); // we want to move the modifier to the "top" // keep calling moveModifierUp until it is in the correct "top" // location while (getLocationForModifier(twoWordModifier) != top) { moveModifierUp(twoWordModifier); } // increase where the location of top is top++; // if the next string is not a modifier, it should be the // modifier's argument // otherwise, it may be a modifier that does not require an // argument if ((i + 1) < breakUp.length && !doesModifierExist(breakUp[i + 2])) { setModifierArguments(twoWordModifier, breakUp[i + 2]); // add to i so the argument is skipped on the next loop i = i + 2; } } } } }