View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   	
5   	See the NOTICE file distributed with this work for additional 
6   	information regarding copyright ownership
7   	
8   	Licensed under the Apache License, Version 2.0 (the "License");
9   	you may not use this file except in compliance with the License.
10  	You may obtain a copy of the License at
11  	
12  	  http://www.apache.org/licenses/LICENSE-2.0
13  	
14  	Unless required by applicable law or agreed to in writing, software
15  	distributed under the License is distributed on an "AS IS" BASIS,
16  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  	See the License for the specific language governing permissions and
18  	limitations under the License.
19   */
20  package org.universAAL.middleware.ui.rdf;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.universAAL.middleware.owl.MergedRestriction;
26  import org.universAAL.middleware.rdf.PropertyPath;
27  
28  /**
29   * The abstract class for all types of form controls that serve as a placeholder
30   * for possible user input.
31   * 
32   * @author mtazari
33   * @author Carsten Stockloew
34   */
35  public abstract class Input extends FormControl {
36      public static final String MY_URI = Form.uAAL_DIALOG_NAMESPACE + "Input";
37  
38      /**
39       * A mandatory (in the sense of "best practice") property for defining a
40       * message to be communicated with human users if the provided input by them
41       * is erroneous.
42       */
43      public static final String PROP_INPUT_ALERT = Form.uAAL_DIALOG_NAMESPACE
44  	    + "inputAlert";
45  
46      /**
47       * A property that is set automatically by the dialog package as soon as an
48       * input control is added to a {@link Submit} control as mandatory input. By
49       * default, input controls are optional.
50       */
51      public static final String PROP_IS_MANDATORY = Form.uAAL_DIALOG_NAMESPACE
52  	    + "isMandatory";
53  
54      protected Input() {
55  	super();
56      }
57  
58      protected Input(String typeURI, Group parent, Label label,
59  	    PropertyPath ref, MergedRestriction valueRestriction,
60  	    Object initialValue) {
61  	super(typeURI, parent, label, ref, valueRestriction, initialValue);
62  	if (parent instanceof Repeat)
63  	    return;
64  	if (ref == null)
65  	    throw new IllegalArgumentException(
66  		    "The property path for input controls must be not null!");
67      }
68  
69      boolean checkSubmission() {
70  	Repeat r = getAncestorRepeat();
71  	if (r != null) {
72  	    if (isMandatory()) {
73  		List values = r.getAllValues(getReferencedPPath());
74  		if (values != null)
75  		    for (Iterator i = values.iterator(); i.hasNext();)
76  			if (i.next() == null)
77  			    return false;
78  	    }
79  	    return r.checkSubmission();
80  	}
81  
82  	Object o = getValue();
83  	if (o == null)
84  	    return !isMandatory();
85  
86  	return !(o instanceof List) || this.getClass() == Select.class;
87      }
88  
89      /**
90       * @see #PROP_INPUT_ALERT
91       */
92      public String getAlertString() {
93  	return (String) props.get(PROP_INPUT_ALERT);
94      }
95  
96      // public Object[] getAllowedValues() {
97      // Restriction r = getControlRestrictions();
98      // Object[] res = (r == null)? null : r.getEnumeratedValues();
99      // if (res == null) {
100     // r = getModelBasedRestrictions();
101     // res = (r == null)? null : r.getEnumeratedValues();
102     // }
103     // return res;
104     // }
105 
106     /**
107      * @see #PROP_IS_MANDATORY
108      */
109     public boolean isMandatory() {
110 	return Boolean.TRUE.equals(props.get(PROP_IS_MANDATORY));
111     }
112 
113     /**
114      * @see #PROP_INPUT_ALERT
115      */
116     public void setAlertString(String value) {
117 	if (value != null && !props.containsKey(PROP_INPUT_ALERT))
118 	    props.put(PROP_INPUT_ALERT, value);
119     }
120 
121     void setMandatory() {
122 	props.put(PROP_IS_MANDATORY, Boolean.TRUE);
123     }
124 
125     /**
126      * @see FormControl#setProperty(String, Object)
127      */
128     public boolean setProperty(String propURI, Object value) {
129 	if (PROP_INPUT_ALERT.equals(propURI)) {
130 	    if (value instanceof String && !props.containsKey(propURI)) {
131 		props.put(propURI, value);
132 		return true;
133 	    }
134 	    return false;
135 	} else
136 	    return super.setProperty(propURI, value);
137     }
138 
139     /**
140      * Tries to store the given value as user input and returns true if it
141      * passes all the known restrictions, false otherwise.
142      */
143     public boolean storeUserInput(Object value) {
144 	if (isMandatory() && value == null)
145 	    return false;
146 
147 	Group g = getParentGroup();
148 	if (g == null)
149 	    return false;
150 
151 	PropertyPath pp = getReferencedPPath();
152 	return g.setValue((pp == null ? null : pp.getThePath()), value,
153 		getLocalRestrictions());
154     }
155 }