View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin.database;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.seasar.tuigwaa.cms.core.CmsRequest;
22  import org.seasar.tuigwaa.cms.core.CmsResponse;
23  import org.seasar.tuigwaa.plugin.PluginException;
24  import org.seasar.tuigwaa.plugin.PluginRequest;
25  import org.seasar.tuigwaa.plugin.basic.VelocityPlugin;
26  
27  import com.isenshi.util.CharUtil;
28  import com.isenshi.util.HtmlBuffer;
29  
30  /***
31   * @author someda
32   */
33  public class InterwikiView extends VelocityPlugin {
34  	
35  	protected void initialize(CmsRequest request, CmsResponse response,
36  			PluginRequest prequest) throws PluginException {				
37  		putPath(prequest.getName(), getClass());
38  		setPojoClass(InterwikiDto.class);
39  	}
40  	
41  	public String doHTMLView(CmsRequest request, CmsResponse response,
42  			PluginRequest prequest) throws PluginException {
43  
44  		String[] args = prequest.getArgs();
45  		if(!existArg(args,0)){  // display input form		
46  			return doHTMLViewBindingObject(request,response,prequest,null);
47  		}
48  		
49  		if(!existArg(args,1)){
50  			throw new PluginException("needs pageName as argument");
51  		}
52  
53  		String label = args[1];
54  				
55  		// select bean
56  		String ret = null;				
57  		try{
58  			InterwikiDto dto = (InterwikiDto) getDao().loadByValue(InterwikiDto.PROP_SERVERNAME,args[0]);
59  			
60  			if(dto != null){
61  				HtmlBuffer buf = new HtmlBuffer();
62  				String url = dto.getUrl();
63  				String encodingType = dto.getEncodingType();
64  				Map context = buildContextMap(args,encodingType);				
65  				if(isTemplate(url)){
66  					url = CharUtil.replace(url,context);
67  				}else{
68  					url = url + (String) context.get("0");
69  				}				
70  				
71  				if(args.length==2){
72  					buf.appendAnchor(url,args[0] + ":" + label);
73  				}else{
74  					buf.appendAnchor(url,label);
75  				}
76  				ret = buf.toString();
77  			}else{				
78  				ret = CharUtil.replace(getMessage("interwiki.notfound"),args[0]);				
79  			}
80  		}catch(Exception e){
81  			ret = getMessage("errors.database.read");	
82  		}
83  		return ret;
84  	}
85  	
86  	private Map buildContextMap(String[] args, String encoding){
87  		Map contextMap = new HashMap();
88  		if(args.length > 2){
89  			for(int i=2;i<args.length;i++){
90  				int keyidx = i-2;
91  				String key = keyidx + "";
92  				contextMap.put(key,CharUtil.urlEncode(args[i],encoding));
93  			}
94  		}else{
95  			contextMap.put("0",CharUtil.urlEncode(args[1],encoding));
96  		}		
97  		return contextMap;
98  	}	
99  	
100 	private boolean isTemplate(String url){
101 		return url.indexOf("${0}") != -1;
102 	}
103 	
104 }