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 com.isenshi.util.extlib;
17  
18  import java.io.IOException;
19  import java.util.MissingResourceException;
20  
21  import javax.servlet.ServletContext;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.struts.tiles.ComponentContext;
29  import org.apache.struts.tiles.Controller;
30  import org.seasar.tuigwaa.model.core.TgwEntity;
31  import org.seasar.tuigwaa.system.Constants;
32  import org.seasar.tuigwaa.util.TgwContext;
33  import org.seasar.tuigwaa.util.TgwResource;
34  import org.seasar.tuigwaa.util.TgwUtils;
35  
36  import com.isenshi.util.PathUtils;
37  
38  /***
39   * Set title string on sitemanage pages.
40   * 
41   * @author someda
42   */
43  public class TilesTitleController implements Controller {
44  
45  	private static final String TATTR_MANAGETITLE = "_manageTitle_";
46  	private static final String MESSAGE_TITLE_PREFIX = "title.label.";
47  	
48  	private static final String MESSAGE_TITLE_DEFAULTKEY = "title.label.sitemanage";	
49  	private static final String DEFAULT_TITLE = TgwResource.getMessage(MESSAGE_TITLE_DEFAULTKEY);
50  	
51  	private Log log = LogFactory.getLog(getClass());
52  	
53  	public void perform(ComponentContext tileContext,
54  			HttpServletRequest request, HttpServletResponse response,
55  			ServletContext servletContext) throws ServletException, IOException {
56  		// do-nothing deprecated
57  	}
58  
59  	public void execute(ComponentContext tileContext,
60  			HttpServletRequest request, HttpServletResponse response,
61  			ServletContext servletContext) throws Exception {
62  				
63  		String siteName = TgwContext.getSiteName();
64  		String servletPath = request.getServletPath();
65  		String actionPath = PathUtils.getResourceName(servletPath);
66  		
67  		String key = MESSAGE_TITLE_PREFIX + actionPath.replaceAll(".do","");
68  		
69  		String pageName = (String) request.getAttribute(Constants.RATTR_PAGENAME);
70  		String entityName = (String) request.getAttribute(Constants.RATTR_ENTITY_NAME);
71  		String logicName = (String) request.getParameter(Constants.PARAM_LOGIC_NAME);
72  		
73  		String displayName = null;
74  		try{
75  			if(entityName != null && !"".equals(entityName)){
76  				TgwEntity entity = TgwUtils.getEntity(siteName,entityName);
77  				if(entity != null){ 
78  					displayName = entity.getDisplayName();
79  				}
80  			}
81  		}catch(Exception e){
82  			// log4j bug ? couldn't use log instance
83  			e.printStackTrace();
84  		}
85  	
86  		String title = DEFAULT_TITLE;
87  		try{
88  			title = TgwResource.getMessage(key);
89  		}catch(MissingResourceException mre){
90  			log.error(mre.getMessage());			
91  		}
92  					
93  		StringBuffer buf = new StringBuffer(title);
94  		appendIfNotNull(buf,pageName);
95  		appendIfNotNull(buf,displayName);		
96  		appendIfNotNull(buf,logicName);
97  		
98  		String manageTitle = buf.toString();		
99  		tileContext.putAttribute(TATTR_MANAGETITLE,manageTitle);		
100 	}	
101 		
102 	private void appendIfNotNull(StringBuffer buf, String value){				
103 		
104 		if(value != null && ! "".equals(value)){
105 			buf.append(" - " + value);
106 		}
107 	}
108 	
109 	
110 
111 }