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.basic;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.seasar.tuigwaa.cms.core.CmsConstants;
22  import org.seasar.tuigwaa.cms.core.CmsRequest;
23  import org.seasar.tuigwaa.cms.core.CmsResponse;
24  import org.seasar.tuigwaa.cms.core.Resource;
25  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
26  import org.seasar.tuigwaa.plugin.AbstractPlugin;
27  import org.seasar.tuigwaa.plugin.PluginException;
28  import org.seasar.tuigwaa.plugin.PluginRequest;
29  import org.seasar.tuigwaa.system.TgwSecurityException;
30  
31  import com.isenshi.util.CharUtil;
32  import com.isenshi.util.HtmlBuffer;
33  
34  public class RecentPlugin extends AbstractPlugin {
35  	
36  	private static final int DEFAULT_NUM = 10;
37  
38  	public String doHTMLView(CmsRequest request, CmsResponse response,
39  			PluginRequest prequest) throws PluginException {
40  		
41  		String[] args = prequest.getArgs();
42  		int num = DEFAULT_NUM;
43  		if(existArg(args,0)){
44  			num = Integer.parseInt(args[0]);			
45  		}
46  		
47  		WikiContext context = getConfiguration().getWikiContext();
48  		
49  		HtmlBuffer buf = new HtmlBuffer();
50  		buf.appendHeading(2,CharUtil.replace(getMessage("recent.modified"), String.valueOf(num)));		
51  		try{
52  			List resourceList = context.getRecentList(request, num);
53  			if(resourceList.size() > 0){
54  				buf.appendStartTag("ul");
55  				for(Iterator i = resourceList.iterator();i.hasNext();){
56  					Resource resource = (Resource) i.next();
57  					printLink(buf,context,resource,request);
58  				}
59  				buf.endTag();
60  			}
61  		}catch(Exception e){
62  			throw new PluginException(e);
63  		}
64  		return buf.toString();
65  	}
66  	
67  	private void printLink(HtmlBuffer buf,WikiContext context,Resource resource,CmsRequest request){
68  		
69  		String path = resource.getPath();
70  		String fileName = resource.getPageName();
71  		String parentPath = resource.getParentPath();		
72  		
73  		try{		
74  			String url = null;
75  			if(parentPath.endsWith(CmsConstants.ATTACHEMENT_SUFFIX)){
76  				String attachedPageName = parentPath.replaceAll(CmsConstants.ATTACHEMENT_SUFFIX,"");
77  				url = context.getAttachedFileURL(attachedPageName,fileName,request).toString();
78  			}else{
79  				url = context.getURLByName(path,request).toString();
80  			}			
81  			
82  			buf.appendStartTag("li");
83  			buf.appendAttribute("class","file");
84  			buf.appendStartTag("a");
85  			buf.appendAttribute("href",url);
86  			buf.appendAttribute("title",parentPath);
87  			buf.appendBody(fileName);
88  			buf.endTag();
89  			buf.appendStartTag("br");
90  			buf.endTag();
91  						
92  			int lastIndex = parentPath.lastIndexOf(resource.getPageName());
93  			if(lastIndex != -1 ) parentPath = parentPath.substring(0,lastIndex);
94  			
95  			if(parentPath!=null&&!parentPath.equals("")){
96  				buf.appendBody("(" + parentPath + ")");
97  				buf.appendStartTag("br");
98  				buf.endTag();
99  			}
100 			
101 			buf.appendBody(resource.getModificationDate());
102 			buf.endTag();
103 		}catch(TgwSecurityException tse){
104 			// do-nothing, security link
105 		}
106 	}
107 
108 }