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.util;
17  
18  import java.util.Comparator;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.apache.struts.action.ActionForward;
25  import org.apache.struts.action.ActionMapping;
26  import org.apache.struts.action.ActionServlet;
27  import org.seasar.framework.container.S2Container;
28  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
29  import org.seasar.tuigwaa.cms.core.Resource;
30  import org.seasar.tuigwaa.controller.TuigwaaActionServlet;
31  import org.seasar.tuigwaa.model.ModelService;
32  import org.seasar.tuigwaa.model.core.TgwAttribute;
33  import org.seasar.tuigwaa.model.core.TgwEntity;
34  import org.seasar.tuigwaa.model.core.impl.ByteArrayAttribute;
35  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
36  import org.seasar.tuigwaa.model.core.impl.SecurityAttribute;
37  import org.seasar.tuigwaa.system.Constants;
38  
39  import com.isenshi.util.CharUtil;
40  import com.isenshi.util.converter.ConverterResource;
41  import com.isenshi.util.functor.UnaryFunction;
42  
43  /***
44   * @author nishioka
45   */
46  public class TgwUtils {
47  
48  	public static final Comparator COMPARATOR_FILEFOLDER = new Comparator() {
49  		public int compare(Object left, Object right) {
50  			Resource resource1 = (Resource) left;
51  			Resource resource2 = (Resource) right;
52  			if (resource1.isFolder() && resource2.isFile()) {
53  				return -1;
54  			} else if (resource2.isFolder() && resource1.isFile()) {
55  				return 1;
56  			} else {
57  				return resource1.getPath().compareTo(resource2.getPath());
58  			}
59  		}
60  	};
61  
62  	public static TuigwaaActionServlet getTuigwaaActionServlet() {
63  		S2Container container = SingletonS2ContainerFactory.getContainer();
64  		return (TuigwaaActionServlet) container.getServletContext()
65  				.getAttribute(Constants.CATTR_TUIGWAASERVLET);
66  	}
67  
68  	public static TgwEntity getEntity(HttpServletRequest request,
69  			ActionServlet servlet) {
70  		String domain = TgwContext.getSiteName();
71  		String entity = request.getParameter(Constants.PARAM_ENTITY_NAME);
72  		return getEntity(domain, entity);
73  	}
74  
75  	public static TgwEntity getEntity(String domain, String entity) {
76  		S2Container container = SingletonS2ContainerFactory.getContainer();
77  		ModelService service = (ModelService) container
78  				.getComponent(ModelService.class);
79  		return service.getEntity(domain, entity);
80  	}
81  
82  	public static String toTableName(TgwEntity entity) {
83  		String entityName = entity.getName();
84  		if (entity.isImportedEntity()) {
85  			return entityName;
86  		}
87  		return entityName + "_";
88  	}
89  
90  	public static String toHibernateType(TgwAttribute field) {
91  		if (field instanceof FileAttribute
92  				|| field instanceof ByteArrayAttribute) {
93  			return "binary";
94  		} else if (field instanceof SecurityAttribute) {
95  			return "string";
96  		}
97  		return field.getType();
98  	}
99  
100 	public static ActionForward forwardPage(HttpServletRequest request,
101 			String pageName) {
102 		TgwContext.bindPageName(pageName);
103 		return Constants.WIKI_FORWARD;
104 	}
105 
106 	public static ActionForward listRecordForward(ActionMapping mapping,
107 			HttpServletRequest request, String entityName) {
108 		TgwContext.bindEntityName(entityName);
109 		return mapping.findForward("listRecord");
110 	}
111 
112 	public static ActionForward dialoggCloseAndListRecordForward(
113 			ActionMapping mapping, HttpServletRequest request,
114 			String entityName, String filter) {
115 		return dialoggCloseAndListRecordForward(mapping, request, entityName,
116 				filter, null);
117 	}
118 
119 	public static ActionForward dialoggCloseAndListRecordForward(
120 			ActionMapping mapping, HttpServletRequest request,
121 			String entityName, String filter, String filterType) {
122 		String siteName = TgwContext.getSiteName();
123 		Map params = new HashMap();
124 		params.put(Constants.PARAM_ENTITY_NAME, entityName);
125 		params.put(Constants.PARAM_ENTITY_FILTER, filter);
126 		if (filterType != null) {
127 			params.put(Constants.PARAM_ENTITY_FILTERTYPE, filterType);
128 		}
129 		return dialogCloseForward(mapping, request, siteName,
130 				Constants.ACTION_URI_VIEWRECORD, params);
131 	}
132 
133 	public static ActionForward dialogCloseForward(ActionMapping mapping,
134 			HttpServletRequest request, String siteName, String action,
135 			Map params) {
136 		String uri = request.getContextPath() + "/" + siteName + "/" + action;
137 		if (params != null) {
138 			uri = uri + "?" + CharUtil.createQuery(params);
139 		}
140 		request.setAttribute(Constants.RATTR_BASEWINDOW_URL, uri);
141 		return mapping.findForward("dialogclose");
142 	}
143 
144 	public static void convert(String siteName, String formName, Object obj) {
145 		ConverterResource resource = (ConverterResource) SingletonS2ContainerFactory
146 				.getContainer().getComponent(ConverterResource.class);
147 		UnaryFunction function = resource.getFunction(siteName, formName);
148 		if (function != null) {
149 			function.evaluate(obj);
150 		}
151 	}
152 
153 }