View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-8-4 下午09:48:31
4    */
5   package gboat2.web.action;
6   
7   import gboat2.base.core.GBoatClassLoader;
8   import gboat2.base.core.dao.QuerySupport;
9   import gboat2.base.core.model.ContentMetadata;
10  import gboat2.base.core.service.IBaseService;
11  import gboat2.base.core.util.ActionUtil;
12  import gboat2.base.core.util.SpringContextUtil;
13  import gboat2.base.core.web.BaseActionSupport;
14  import gboat2.web.business.IMetadataBusiness;
15  
16  import java.net.URL;
17  import java.util.HashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.struts2.convention.annotation.ResultPath;
22  import org.osgi.framework.Bundle;
23  import org.springframework.beans.factory.annotation.Autowired;
24  
25  /**
26   * 
27   * 属性查看
28   * @author lysming
29   * @since jdk1.6
30   * @date 2013-8-4
31   *  
32   */
33  @ResultPath("/content/property")
34  public class PropertyAction extends BaseActionSupport {
35  	@Autowired
36  	IMetadataBusiness metadataService;
37  	
38  	private String actionName;
39  
40  	private String invokeMethod;
41  	
42  	private String bundleName;
43  	
44  	private String viewPage;
45  
46  	public String view() {
47  		Bundle bundle = GBoatClassLoader.getInstance().getBundle(actionName.substring(0,actionName.lastIndexOf(".")));
48  		bundleName = bundle.getSymbolicName();
49  		Class actionClass;
50  		try {
51  			actionClass = bundle.loadClass(actionName);
52  			String uriPrefix = ActionUtil.getUriPrefix(actionName);
53  			URL url = null;
54  			//根据@ResultPath 读取,如未配置按照约定的路径
55  			String resultPath = ActionUtil.getDefaultResultPath(actionName);
56  			ResultPath path = (ResultPath)actionClass.getAnnotation(ResultPath.class);
57  			if(path!=null){
58  				resultPath = path.value();
59  			}
60  			//元数据定义支持两种方式,一,每一个方法独立定义,文件名称为"uriPrefix-method.md";二,所有方法统一定义,文件名称为"uriPrefix.md"
61  			//第一种定义方式的优先级高于第二种方式
62  			String filePath = resultPath + "/metadata/" + uriPrefix + "-" + invokeMethod + ".md";
63  			url = bundle.getResource(filePath);
64  			//如果无法根据上边filePath的格式获取md文件,则使用另一种获取文件的方式
65  			if (url == null) {
66  				filePath = resultPath + "/metadata/" + uriPrefix + ".md";
67  				url = bundle.getResource(filePath);
68  			}
69  			
70  			if(url!=null){
71  				//检查数据库配置
72  				Map<String, Object> params = new HashMap<String, Object>();
73  				params.put(QuerySupport.PARAM_TABLENAME, ContentMetadata.class);
74  				params.put("_metaDataStatus", "1");
75  				params.put("_fileName", url.getFile());
76  				params.put("_bundleSymble", bundle.getSymbolicName());
77  				IBaseService baseService = (IBaseService) SpringContextUtil.getInstance().getBeanOfId("baseService");
78  				List<ContentMetadata> list = (List<ContentMetadata>) baseService.query(params);
79  		
80  				if (!list.isEmpty()) {
81  					viewPage = "db://"+bundle.getSymbolicName()+url.getFile();
82  				}else{
83  					viewPage = url.toString();
84  				}
85  			}else{
86  				url = bundle.getResource(resultPath + "/" + getVelocityFileName(actionName,invokeMethod));
87  				if(url!=null){
88  					viewPage = url.toString();
89  				}
90  			}
91  		} catch (ClassNotFoundException e) {
92  			e.printStackTrace();
93  		}
94  		
95  		return "view";
96  	}
97  	
98  	private  String getVelocityFileName(String actionName, String invokeMethod) {
99  		StringBuilder velocityFile = new StringBuilder();
100 		velocityFile.append(ActionUtil.getUriPrefix(actionName));
101 		velocityFile.append("-").append(invokeMethod);
102 		velocityFile.append(".vm");
103 
104 		return velocityFile.toString();
105 	}
106 	
107 	public String getActionName() {
108 		return actionName;
109 	}
110 
111 	public void setActionName(String actionName) {
112 		this.actionName = actionName;
113 	}
114 
115 	public String getInvokeMethod() {
116 		return invokeMethod;
117 	}
118 
119 	public void setInvokeMethod(String invokeMethod) {
120 		this.invokeMethod = invokeMethod;
121 	}
122 	
123 	public String getBundleName() {
124 		
125 		return bundleName;
126 	}
127 
128 	public String getViewPage(){
129 		return viewPage;
130 	}
131 }