View Javadoc

1   /*
2    * Copyright 2002-2004 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 net.sf.fridaymvc.managers.view;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import net.sf.fridaymvc.RunData;
26  import net.sf.fridaymvc.actions.Action;
27  import net.sf.fridaymvc.metadata.commons.RegExpValidatorAttribute;
28  
29  import org.apache.commons.attributes.Attributes;
30  
31  
32  /***
33   * 
34   * @author <a href="mailto:arto.pastinen@ofw.fi">Arto Pastinen</a>
35   * @version $Id: ActionViewManager.java,v 1.2 2004/11/23 20:35:58 artsi Exp $
36   */
37  
38  public class ActionViewManager {
39      private Map parameters = new HashMap(0);
40      
41      public void setParameter(String key, String value) {
42          parameters.put(key, value);        
43      }
44      
45      public String getAction(String actionName) {        
46          StringBuffer sb = RunData.getInstance().getRelativeServletPath();
47          synchronized(sb) {
48              sb.append("?action=");
49              sb.append(actionName);
50              if(parameters.size() > 0) {
51                  for (Iterator iter = parameters.keySet().iterator(); iter.hasNext();) {
52                      String key = (String) iter.next();
53                      sb.append("&action." + key);
54                      sb.append("=");
55                      sb.append((String) parameters.get(key));                    
56                  }
57                  parameters.clear();
58              }
59          }
60          return RunData.getInstance().getHttpServletResponse().encodeURL(sb.toString());
61      }
62      
63      public String getName(String fieldName) {
64          return "action." + fieldName;        
65      }
66      
67      public String getValue(String fieldName) {
68          return RunData.getInstance().getParameters().getString("action." + fieldName);
69      }
70      
71      public boolean passValidation(String validatorId) {
72          Action action = RunData.getInstance().getCurrentAction();
73          if(action == null) {
74              return true;
75          }
76          
77          Collection validators = Attributes.getAttributes(action.getClass(), RegExpValidatorAttribute.class);
78          // too bad that we must iterate..
79          for (Iterator iter = validators.iterator(); iter.hasNext();) {
80              RegExpValidatorAttribute element = (RegExpValidatorAttribute) iter.next();
81              if(!element.getId().equals(validatorId)) continue;
82              return element.passValidation();
83          }
84          return true;
85      } 
86      
87      public Map attributes() {
88          Action action = RunData.getInstance().getCurrentAction();
89          if(action == null) return Collections.EMPTY_MAP;
90          return action.getAttributesMap();
91      }
92  }