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;
17  
18  import java.util.ArrayList;
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.seasar.tuigwaa.cms.core.CmsConstants;
28  import org.seasar.tuigwaa.cms.core.CmsRequest;
29  import org.seasar.tuigwaa.model.common.EntityUtils;
30  import org.seasar.tuigwaa.model.core.TgwEntity;
31  import org.seasar.tuigwaa.system.Constants;
32  import org.seasar.tuigwaa.util.TgwContext;
33  
34  import com.isenshi.util.CharUtil;
35  
36  public class TgwPluginUtils {
37  
38  	private static final String PREFIX_ENTITYNAME = "tgw_plugin_";
39  	
40  	private static final String PREFIX_ENTITY = "com.isenshi.tuigwaa.view.wiki.plugin/";
41  
42  	// bind
43  
44  	public static void bindEntityMapToCmsRequest(Map entityMap) {
45  		for (Iterator i = entityMap.keySet().iterator(); i.hasNext();) {
46  			TgwEntity entity = (TgwEntity) i.next();
47  			String id = (String) entityMap.get(entity);
48  			bindEntityId(entity.getName(), id);
49  		}
50  	}
51  
52  	public static void bindEntityId(TgwEntity entity, Long id) {
53  		bindEntityId(entity.getName(), String.valueOf(id));
54  	}
55  
56  	public static void bindEntityObject(String domainName, String entityName,
57  			Object bean) {
58  		bindEntityObject(TgwContext.getCmsRequest(), domainName, entityName,
59  				bean);
60  	}
61  
62  	public static void bindEntityObject(TgwEntity entity, Object bean) {
63  		bindEntityObject(TgwContext.getCmsRequest(), entity, bean);
64  	}
65  
66  	public static void bindEntityObject(CmsRequest req, TgwEntity entity,
67  			Object bean) {
68  		bindEntityObject(req, entity.getDomainName(), entity.getName(), bean);
69  	}
70  
71  	public static void bindKeyword(String keyword) {
72  		if (keyword == null) {
73  			return;
74  		}
75  		List list = new ArrayList();
76  		String[] keywords = keyword.split(" "); // Need Japanese Character Space
77  		for (int i = 0; keywords != null && i < keywords.length; i++) {
78  			if (keywords[i] != null && !keywords[i].equals("")) {
79  				list.add(keywords[i]);
80  			}
81  		}
82  		TgwContext.setCmsAttribute(CmsConstants.PARAM_KEYWORD, list);
83  	}
84  
85  	// get
86  
87  	public static Object getEntityObject(CmsRequest req, TgwEntity entity) {
88  		return getEntityObject(req, entity.getDomainName(), entity.getName());
89  	}
90  
91  	public static void removeEntityObject(String domainName, String entityName) {
92  		removeEntityObject(TgwContext.getCmsRequest(), domainName, entityName);
93  	}
94  
95  	public static void removeEntityObject(CmsRequest req, String domainName,
96  			String entityName) {
97  		req.removeParameter(createEntityBindingName(entityName));
98  		req.removeAttribute(PREFIX_ENTITY + domainName + "." + entityName);
99  	}
100 
101 	public static Map getEntityNameMapFromRequest(HttpServletRequest req) {
102 		Map map = new HashMap();
103 		Enumeration enm = req.getParameterNames();
104 		while (enm.hasMoreElements()) {
105 			String key = (String) enm.nextElement();
106 			String entityName = CharUtil.removePrefixAndSuffix(key,
107 					Constants.PARAM_PREFIX_ENTITY,
108 					Constants.PARAM_SUFFIX_ENTITY);
109 			if (entityName != null) {
110 				map.put(entityName, req.getParameter(key));
111 			}
112 		}
113 		return map;
114 	}
115 
116 	public static Long getEntityId(TgwEntity entity) {
117 		return getEntityId(TgwContext.getCmsRequest(), entity);
118 	}
119 
120 	public static Long getEntityId(CmsRequest req, TgwEntity entity) {
121 		return getEntityId(req, entity.getName());
122 	}
123 
124 	// create
125 
126 	public static String createEscapeEntityBindingName(String entityName,
127 			String attrName) {
128 		return Constants.PARAM_PREFIX_ENTITY_ESCAPE + entityName
129 				+ Constants.PARAM_PREFIX_ATTR_ESCAPE + attrName;
130 	}
131 
132 	public static String createEntityBindingName(String entityName) {
133 		return Constants.PARAM_PREFIX_ENTITY + entityName
134 				+ Constants.PARAM_SUFFIX_ENTITY;
135 	}
136 
137 	// [Start] ----- Private Method ------
138 
139 	private static Long getEntityId(CmsRequest req, String entityName) {
140 		String bindingName = createEntityBindingName(entityName);
141 		String id = req.getParameter(bindingName);
142 		if (id == null) {// || id.equals("null")) {
143 			return null;
144 		}
145 		return Long.valueOf(id);
146 	}
147 
148 	private static void bindEntityId(String entityName, String id) {
149 		String bindingName = createEntityBindingName(entityName);
150 		TgwContext.setCmsParameter(bindingName, id);
151 	}
152 
153 	private static Object getEntityObject(CmsRequest req, String domainName,
154 			String entityName) {
155 		return req.getAttribute(PREFIX_ENTITY + domainName + "." + entityName);
156 	}
157 
158 	private static void bindEntityObject(CmsRequest req, String domainName,
159 			String entityName, Object bean) {
160 		if (getEntityId(req, entityName) == null) {
161 			bindEntityId(entityName, String.valueOf(EntityUtils.getId(bean)));
162 		}
163 		req.setAttributes(PREFIX_ENTITY + domainName + "." + entityName, bean);
164 	}
165 
166 	public static String getDetailPageName(CmsRequest request, TgwEntity entity) {
167 		String parentpath = request.getPage().getResource().getParentPath();
168 		String pageName = entity.getDisplayName()
169 				+ PluginUtils.getMessage("link.label.detail");
170 		if (parentpath == null || parentpath.equals("")) {
171 			return pageName;
172 		} else {
173 			return parentpath + "/" + pageName;
174 		}
175 	}
176 
177 	public static String toPluginEntityName(Class clazz) {
178 		return PREFIX_ENTITYNAME + EntityUtils.toEntityName(clazz);
179 	}
180 	
181 	public static boolean isPluginEntity(String entityName){
182 		return entityName.startsWith(PREFIX_ENTITYNAME);
183 	}
184 }