001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.example;
021:
022: import java.util.Arrays;
023: import java.util.HashSet;
024:
025: import de.schlund.pfixcore.example.iwrapper.Trouser;
026: import de.schlund.pfixcore.generator.IHandler;
027: import de.schlund.pfixcore.generator.IWrapper;
028: import de.schlund.pfixcore.workflow.Context;
029: import de.schlund.util.statuscodes.StatusCodeLib;
030:
031: /**
032: * TrouserHandler.java
033: *
034: *
035: * Created: Thu Oct 18 18:53:20 2001
036: *
037: * @author <a href="mailto:jtl@schlund.de">Jens Lautenbacher</a>
038: *
039: *
040: */
041:
042: public class TrouserHandler implements IHandler {
043: public void handleSubmittedData(Context context, IWrapper wrapper)
044: throws Exception {
045: Trouser trouser = (Trouser) wrapper;
046: ContextTrouser ct = SampleRes.getContextTrouser(context);
047: Integer color = trouser.getColor();
048: String size = trouser.getSize();
049: Integer[] feature = trouser.getFeature();
050:
051: // Sample Check: reject a certain combination
052: if (feature != null && color.equals(new Integer(2))) {
053: HashSet<Integer> set = new HashSet<Integer>(Arrays
054: .asList(feature));
055: if (set.contains(new Integer(1))) {
056: // The combination of feature 1 and color No. 2 is invalid (maybe out of stock)
057: String[] args = new String[2];
058: args[0] = "1";
059: args[1] = "2";
060: trouser
061: .addSCodeColor(
062: StatusCodeLib.PFIXCORE_EXAMPLE_TROUSER_FEATURECOLOR_OUTOF_STOCK,
063: args, null);
064: return;
065: }
066: }
067:
068: // Everything was ok, store it.
069: ct.setSize(size);
070: ct.setFeature(feature);
071: ct.setColor(color);
072: }
073:
074: public void retrieveCurrentStatus(Context context, IWrapper wrapper)
075: throws Exception {
076: Trouser trouser = (Trouser) wrapper;
077: ContextTrouser ct = SampleRes.getContextTrouser(context);
078:
079: // we use the ct.needsData() call to look if the Context Ressource has meaningful content
080: // to display. This may be handled differently depending on the case.
081: if (!ct.needsData()) {
082: trouser.setColor(ct.getColor());
083: trouser.setSize(ct.getSize());
084: trouser.setFeature(ct.getFeature());
085: }
086: }
087:
088: public boolean needsData(Context context) throws Exception {
089: ContextTrouser ct = SampleRes.getContextTrouser(context);
090: return ct.needsData();
091: }
092:
093: public boolean prerequisitesMet(Context context) throws Exception {
094: ContextAdultInfo cai = SampleRes.getContextAdultInfo(context);
095: return !cai.needsData();
096: }
097:
098: public boolean isActive(Context context) throws Exception {
099: ContextAdultInfo cai = SampleRes.getContextAdultInfo(context);
100: ContextTrouser ct = SampleRes.getContextTrouser(context);
101: Boolean adult = cai.getAdult();
102: if (adult != null) {
103: // Depending on the situation, this may or may not be the right thing to do:
104: // The result of this code is that the current information in ct will be lost as
105: // soon as cai.getAdult becomes false (again).
106: if (adult.booleanValue() == false) {
107: ct.reset();
108: }
109: return adult.booleanValue();
110: } else {
111: return false;
112: }
113: }
114:
115: }// TrouserHandler
|