View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.app.client.web.request;
18  
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Set;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.gwe.persistence.model.HeadResourceInfo;
26  import org.gwe.persistence.model.JobInfo;
27  import org.gwe.persistence.model.OrderInfo;
28  
29  /**
30   * @author Marco Ruiz
31   * @since Dec 14, 2008
32   */
33  public class Param<VALUE_TYPE, OBJECT_TYPE> {
34  
35  	private static final Set<Param> params = new HashSet<Param>();
36  	
37  	public static final Param<String,  HeadResourceInfo> CLUSTER_ID = new Param<String, HeadResourceInfo>  ("clusterId", "selectedCluster", new StringParser()); 
38  	public static final Param<Integer, OrderInfo>        ORDER_ID   = new Param<Integer, OrderInfo>        ("orderId",   "selectedOrder",   new IntegerParser()); 
39  	public static final Param<Integer, JobInfo>          JOB_NUM    = new Param<Integer, JobInfo>          ("jobNum",    "selectedJob",     new IntegerParser());
40  	public static final Param<Boolean, Object>           INSTALL    = new Param<Boolean, Object>           ("install",   "",                new BooleanParser());
41  	public static final Param<String,  OrderInfo>        P2EL       = new Param<String, OrderInfo>         ("p2el",      "queuedOrder",     new StringParser());
42  	public static final Param<String,  List<String>>     COMMANDS   = new Param<String, List<String>>      ("commands",  "commands",        new StringParser());
43  	public static final Param<String,  String>           OPER       = new Param<String, String>            ("operation", "operationResult", new StringParser());
44  	public static final Param<String,  String>           BUNDLE     = new Param<String, String>            ("bundle",    "",                new StringParser());
45  
46  	public static Set<Param> values() {
47  		return new HashSet<Param>(params);
48  	}
49  	
50  	private String fieldId;
51  	private String objectIdentifiedName;
52  	private ParamParser<VALUE_TYPE> parser;
53  	
54  	protected Param(String fieldId, String objectIdentifiedName, ParamParser<VALUE_TYPE> paramParser) { 
55  		this.fieldId = fieldId;
56  		this.objectIdentifiedName = objectIdentifiedName;
57  		this.parser = paramParser;
58  		params.add(this);
59  	}
60  	
61  	public String getFieldId() { 
62  		return fieldId; 
63  	}
64  	
65  	public Object extractFrom(HttpServletRequest request) {
66          return (request == null) ? null : request.getParameter(fieldId);
67  	}
68  
69  	public VALUE_TYPE parse(Object value) {
70      	return parser.parse(value);
71      }
72  
73  	public String getObjectIdentifiedName() {
74      	return objectIdentifiedName;
75      }
76  	
77  	public String toString() {
78  		return fieldId;
79  	}
80  }
81  
82  interface ParamParser<VALUE_TYPE> {
83  	public VALUE_TYPE parse(Object value);
84  }
85  
86  class StringParser implements ParamParser<String> {
87  	public String parse(Object value) {
88  		return (value != null) ? value.toString() : null;
89      }
90  }
91  
92  class IntegerParser implements ParamParser<Integer> {
93  	public Integer parse(Object value) {
94  		try {
95  			return Integer.parseInt(value.toString());
96  		} catch(Exception e) {
97  			return null;
98  		}
99      }
100 }
101 
102 class BooleanParser implements ParamParser<Boolean> {
103 	public Boolean parse(Object value) {
104 		try {
105 			return Boolean.parseBoolean(value.toString());
106 		} catch(Exception e) {
107 			return false;
108 		}
109     }
110 }