ingo@1172: package de.intevation.flys.artifacts.states; ingo@1172: ingo@1178: import org.apache.log4j.Logger; ingo@1178: ingo@1180: import org.w3c.dom.Element; ingo@1180: ingo@1178: import de.intevation.artifacts.Artifact; ingo@1178: import de.intevation.artifacts.CallContext; ingo@1178: ingo@1178: import de.intevation.artifactdatabase.data.DefaultStateData; ingo@1178: import de.intevation.artifactdatabase.data.StateData; ingo@1178: ingo@1180: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; ingo@1180: ingo@1178: import de.intevation.flys.artifacts.FLYSArtifact; ingo@1180: import de.intevation.flys.artifacts.model.CalculationResult; ingo@1180: import de.intevation.flys.artifacts.model.WQKms; ingo@1180: import de.intevation.flys.artifacts.resources.Resources; ingo@1178: import de.intevation.flys.utils.FLYSUtils; felix@1655: import de.intevation.flys.utils.StringUtil; ingo@1178: ingo@1178: ingo@1172: /** ingo@1172: * @author Ingo Weinzierl ingo@1172: */ ingo@1172: public class WaterlevelSelectState extends DefaultState { ingo@1172: ingo@1178: private static final Logger logger = ingo@1178: Logger.getLogger(WaterlevelSelectState.class); ingo@1178: ingo@1178: public static final String SPLIT_CHAR = ";"; ingo@1178: ingo@1180: public static final String WINFO_WSP_STATE_ID = "state.winfo.waterlevel"; ingo@1180: ingo@1180: public static final String I18N_STATIC_KEY = "wsp.selected.string"; ingo@1180: ingo@1178: ingo@1172: @Override ingo@1172: protected String getUIProvider() { ingo@1172: return "wsp_datacage_panel"; ingo@1172: } ingo@1178: ingo@1178: felix@1655: /** felix@1655: * @param flys ignored felix@1655: * @param cc ignrored felix@1655: */ ingo@1178: @Override ingo@1178: public StateData transform( ingo@1178: FLYSArtifact flys, ingo@1178: CallContext cc, ingo@2205: StateData stateData, ingo@1178: String name, ingo@1178: String val ingo@1178: ) { ingo@1178: if (!isValueValid(val)) { ingo@1178: logger.error("The given input string is not valid: '" + val + "'"); ingo@1178: return null; ingo@1178: } ingo@1178: felix@1655: return new DefaultStateData(name, null, null, StringUtil.unbracket(val)); ingo@1178: } ingo@1178: ingo@1178: ingo@1178: @Override ingo@1178: public boolean validate(Artifact artifact) ingo@1178: throws IllegalArgumentException ingo@1178: { ingo@1178: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@1178: ingo@1178: StateData data = flys.getData("wsp"); ingo@1178: ingo@1178: if (data == null) { ingo@1178: throw new IllegalArgumentException("WSP is empty"); ingo@1178: } ingo@1178: ingo@1178: return true; ingo@1178: } ingo@1178: ingo@1178: ingo@1180: @Override ingo@1180: protected Element createStaticData( ingo@1743: FLYSArtifact flys, ingo@1180: ElementCreator creator, ingo@1180: CallContext cc, ingo@1180: String name, ingo@1180: String value, ingo@1180: String type ingo@1180: ) { ingo@1180: Element dataElement = creator.create("data"); ingo@1180: creator.addAttr(dataElement, "name", name, true); ingo@1180: creator.addAttr(dataElement, "type", type, true); ingo@1180: ingo@1180: Element itemElement = creator.create("item"); ingo@1180: creator.addAttr(itemElement, "value", value, true); ingo@1180: ingo@1180: String[] labels = getLabels(cc, value); ingo@1180: Object[] obj = new Object[] { labels[0] }; ingo@1180: ingo@1180: String attrValue = Resources.getMsg( ingo@1180: cc.getMeta(), I18N_STATIC_KEY, I18N_STATIC_KEY, obj); ingo@1180: ingo@1180: creator.addAttr(itemElement, "label", attrValue, true); ingo@1180: dataElement.appendChild(itemElement); ingo@1180: ingo@1180: return dataElement; ingo@1180: } ingo@1180: ingo@1180: felix@1655: /** felix@1655: * Get name to display for selected watelerlevel (for example "Q=123") sascha@3076: * from the CalculationResult. felix@1655: */ ingo@1180: public static String[] getLabels(CallContext cc, String value) { ingo@1180: String[] parts = value.split(SPLIT_CHAR); ingo@1180: ingo@1180: FLYSArtifact artifact = FLYSUtils.getArtifact(parts[0], cc); ingo@1180: ingo@1180: CalculationResult rawData = (CalculationResult) artifact.compute( ingo@1180: cc, ingo@1180: null, ingo@1180: WINFO_WSP_STATE_ID, ingo@1180: ComputeType.ADVANCE, ingo@1180: false); ingo@1180: ingo@1180: WQKms[] wqkms = (WQKms[]) rawData.getData(); ingo@1180: ingo@1180: int idx = -1; ingo@1180: try { ingo@1180: idx = Integer.parseInt(parts[2]); ingo@1180: } ingo@1180: catch (NumberFormatException nfe) { /* do nothing */ } ingo@1180: ingo@1657: String name = wqkms[idx].getName(); ingo@1657: felix@1660: return new String[] { StringUtil.wWrap(name) }; ingo@1180: } ingo@1180: ingo@1180: ingo@1178: /** ingo@1178: * Validates the given String. A valid string for this state requires the ingo@1178: * format: "UUID;FACETNAME;FACETINDEX". ingo@1178: * ingo@1178: * @param value The string value requires validation. ingo@1178: * ingo@1178: * @return true, if the string applies the specified format, otherwise ingo@1178: * false. ingo@1178: */ ingo@1178: public static boolean isValueValid(String value) { ingo@1178: logger.debug("Validate string: '" + value + "'"); ingo@1178: felix@1655: value = StringUtil.unbracket(value); ingo@1178: ingo@1178: logger.debug("Validate substring: '" + value + "'"); ingo@1178: ingo@1178: if (value == null || value.length() == 0) { ingo@1178: return false; ingo@1178: } ingo@1178: ingo@1178: String[] parts = value.split(SPLIT_CHAR); ingo@1178: ingo@1178: if (parts == null || parts.length < 3) { ingo@1178: return false; ingo@1178: } ingo@1178: ingo@1178: if (parts[0] == null || parts[0].length() == 0) { ingo@1178: return false; ingo@1178: } ingo@1178: ingo@1178: if (parts[1] == null || parts[1].length() == 0) { ingo@1178: return false; ingo@1178: } ingo@1178: ingo@1178: try { ingo@1178: Integer.parseInt(parts[2]); ingo@1178: } ingo@1178: catch (NumberFormatException nfe) { ingo@1178: logger.error("Index is not a valid integer!", nfe); ingo@1178: } ingo@1178: ingo@1178: return true; ingo@1178: } ingo@1172: } ingo@1172: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :