PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/211Project/to211-web/src/main/java/com/to211/web/controller/OrganizationWebTemplateController.java

http://java-hiking.googlecode.com/
Java | 252 lines | 181 code | 27 blank | 44 comment | 12 complexity | eb4e8c24ee4efe241647a13e972cd8f6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. package com.to211.web.controller;
  2. import java.io.FileInputStream;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.annotation.Resource;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.validation.ObjectError;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.servlet.ModelAndView;
  13. import com.alibaba.fastjson.JSON;
  14. import com.fdhay.authcenter.proxy.domain.annotation.Authorization;
  15. import com.fdhay.authcenter.proxy.domain.enums.AuthorizationType;
  16. import com.to211.common.util.FileUtils;
  17. import com.to211.common.util.MimeTypeUtils;
  18. import com.to211.common.util.WebResourceUtils;
  19. import com.to211.domain.Domain;
  20. import com.to211.domain.WebDesignDTO;
  21. import com.to211.domain.WebModule;
  22. import com.to211.domain.WebTemplate;
  23. import com.to211.domain.base.Page;
  24. import com.to211.domain.base.json.JsonMessage;
  25. import com.to211.service.DirectoryOfWebTemplateService;
  26. import com.to211.service.DomainService;
  27. import com.to211.service.WebModuleService;
  28. @Controller
  29. @RequestMapping("/org/webTemplate")
  30. public class OrganizationWebTemplateController extends OrganizationController {
  31. @Resource private DomainService domainService;
  32. @Resource private DirectoryOfWebTemplateService directoryOfWebTemplateService;
  33. @Resource private WebModuleService webModuleService;
  34. /**
  35. * 机构用户设计自己的主页页面布局
  36. */
  37. @RequestMapping(value="/design/{organizationCode}/**", method=RequestMethod.GET)
  38. @Authorization(type=AuthorizationType.AUTHENTICATED)
  39. public ModelAndView design(@PathVariable String organizationCode) {
  40. try {
  41. this.checkWithLoginUserOrganization(organizationCode);
  42. String templateCode = getWebTemplateCodeOfOrganization(organizationCode);
  43. String webResourceName = getWebResourceName(String.format("/design/%s/", organizationCode));
  44. String webTempalteDataDir = directoryOfWebTemplateService.getTemplateDataBaseDirAndCreateIfNotExist(templateCode);
  45. //如果是html页面,则调用freemarker模板生成,其他文件直接返回到客户端
  46. if(isHtmlPage(webResourceName)){
  47. String content = this.getContentFromTemplateDir(webTempalteDataDir, webResourceName, getModelMap());
  48. getModelMap().put("organizationCode", organizationCode);
  49. getModelMap().put("moduleBlockers", this.getOrganizationModuleBlockers());
  50. return toViewOfContentWithLayout(content, "web-template/design", getModelMap());
  51. }else{
  52. getResponse().setContentType(MimeTypeUtils.getMimeTypeOfResource(webResourceName));
  53. FileInputStream fis = new FileInputStream(getWebResourceFullName(webTempalteDataDir, webResourceName));
  54. FileUtils.copy(fis, getResponse().getOutputStream());
  55. return null;
  56. }
  57. } catch (Exception e) {
  58. logger.error(e);
  59. return toError(e.getMessage());
  60. }
  61. }
  62. /**
  63. * 保存用户主页设计
  64. * @param jsonData - eg:[{"sectionId": "section_1", "moduleId": "module_1"},...]
  65. * @return
  66. */
  67. @RequestMapping(value="/saveDesign", method=RequestMethod.POST)
  68. @Authorization(type=AuthorizationType.AUTHENTICATED)
  69. public ModelAndView saveDesign(String jsonData) {
  70. try {
  71. if(StringUtils.isEmpty(jsonData)){
  72. return toError("提交的数据为空!");
  73. }
  74. logger.debug(jsonData);
  75. //把用户填充的排版哪个section显示哪个模块的对应关系入库
  76. this.updateOrgWebLayoutMappingToDB(jsonData);
  77. return toHTML("保存成功");
  78. } catch (Exception e) {
  79. logger.error(e);
  80. return toError(e.getMessage());
  81. }
  82. }
  83. @RequestMapping(value="/view/{organizationCode}/**", method=RequestMethod.GET)
  84. @Authorization(type=AuthorizationType.AUTHENTICATED)
  85. public ModelAndView view(@PathVariable String organizationCode) {
  86. try {
  87. String templateCode = getWebTemplateCodeOfOrganization(organizationCode);
  88. String webTempalteDataDir = directoryOfWebTemplateService.getTemplateDataBaseDirAndCreateIfNotExist(templateCode);
  89. String webResourceName = getWebResourceName(String.format("/view/%s/", organizationCode));
  90. //如果是html页面,则调用freemarker模板生成,其他文件直接返回到客户端
  91. if(isHtmlPage(webResourceName)){
  92. String content = this.generateWebContent(templateCode, this.getSectionFillInListFromDB());
  93. getModelMap().put("domain", domainService.selectByOrganizationCode(organizationCode));
  94. return toViewOfContentWithLayout(content, "web-template/design_preview", getModelMap());
  95. }else{
  96. getResponse().setContentType(MimeTypeUtils.getMimeTypeOfResource(webResourceName));
  97. FileInputStream fis = new FileInputStream(getWebResourceFullName(webTempalteDataDir, webResourceName));
  98. FileUtils.copy(fis, getResponse().getOutputStream());
  99. return null;
  100. }
  101. } catch (Exception e) {
  102. logger.error(e);
  103. return toError(e.getMessage());
  104. }
  105. }
  106. /**
  107. * 生成新web内容
  108. * @return
  109. */
  110. @RequestMapping(value="/generateNewWeb", method=RequestMethod.POST)
  111. @Authorization(type=AuthorizationType.AUTHENTICATED)
  112. public ModelAndView generateNewWeb() {
  113. try {
  114. String organizationCode = getLoginUser().getOrganizationCode();
  115. String webTemplateCode = this.getWebTemplateCodeOfOrganization(organizationCode);
  116. super.initOrganizationWebContent(organizationCode, webTemplateCode, getSectionFillInListFromDB());
  117. return toHTML("生成成功");
  118. } catch (Exception e) {
  119. logger.error(e);
  120. return toError(e.getMessage());
  121. }
  122. }
  123. /**********************************机构所用模板***********************************************/
  124. @RequestMapping(value="/listTemplate")
  125. @Authorization(type=AuthorizationType.AUTHENTICATED)
  126. public ModelAndView getWebTemplateList(Page<WebTemplate> page){
  127. String organizationCode = getLoginUser().getOrganizationCode();
  128. webTemplateService.selectOrganizationWebTemplateListByPage(organizationCode, page);
  129. WebTemplate currentWebTemplate = this.getWebTemplateOfOrganization(organizationCode);
  130. getModelMap().put("page", page);
  131. getModelMap().put("currentWebTemplate", currentWebTemplate);
  132. return toViewWithoutLayout("organization/organization-web-template-list", getModelMap());
  133. }
  134. /**
  135. * 更新机构所用的模板
  136. * @param userWebTemplateId
  137. * @param webTemplateCode
  138. * @return
  139. */
  140. @RequestMapping(value = "/updateTemplate")
  141. @Authorization(type=AuthorizationType.AUTHENTICATED)
  142. public ModelAndView updateWebTemplate(Integer webTemplateId) {
  143. List<ObjectError> errors = new ArrayList<ObjectError>(1);
  144. if(webTemplateId == null || webTemplateId < 0){
  145. ObjectError error = new ObjectError("organization", "没有选择模板");
  146. errors.add(error);
  147. }
  148. WebTemplate webTemplate = webTemplateService.selectByPrimaryKey(webTemplateId);
  149. if(webTemplate == null){
  150. ObjectError error = new ObjectError("organization", "没有模板记录");
  151. errors.add(error);
  152. }
  153. if(!errors.isEmpty()){
  154. getModelMap().put("errorList", errors);
  155. return toViewWithoutLayout("organization/organization-web-template-list", getModelMap());
  156. }
  157. //更新机构所用的模板
  158. Domain domain = domainService.selectByOrganizationCode(this.getLoginUser().getOrganizationCode());
  159. domain.setWebTemplateCode(webTemplate.getCode());
  160. domainService.updateByPrimaryKeySelective(domain);
  161. JsonMessage msg = JsonMessage.sucess();
  162. return toJSON(msg);
  163. }
  164. /**********************************~: end 机构所用模板***********************************************/
  165. /**
  166. * 检测设计的主页布局是不是自己的主页布局
  167. * @param organizationCode
  168. * @throws IllegalArgumentException
  169. */
  170. private void checkWithLoginUserOrganization(String organizationCode) throws IllegalArgumentException{
  171. if(!isStaticResource()
  172. && !StringUtils.equals(organizationCode, getLoginUser().getOrganizationCode())){
  173. throw new IllegalArgumentException("对不起,您只能设计您自己的主页布局!");
  174. }
  175. }
  176. /**
  177. * 判断请求资源是否为静态资源
  178. * @param request
  179. * @return
  180. */
  181. private boolean isStaticResource(){
  182. return WebResourceUtils.isStaticResource(getRequest().getRequestURI());
  183. }
  184. /**
  185. * 获取机构的主页对应的模板码
  186. * @param organizationCode
  187. * @return
  188. */
  189. private String getWebTemplateCodeOfOrganization(String organizationCode) {
  190. Domain domain = domainService.selectByOrganizationCode(organizationCode);
  191. String webTemplateCode = domain.getWebTemplateCode();
  192. if(StringUtils.isNotEmpty(webTemplateCode)){
  193. return webTemplateCode;
  194. }
  195. return ORGANIZATION_WEB_DEFAULT_WEB_TEMPLATE_CODE;
  196. }
  197. /**
  198. * 获取机构主页对应的模板对象
  199. * @param organizationCode
  200. * @return
  201. */
  202. private WebTemplate getWebTemplateOfOrganization(String organizationCode) {
  203. String webTemplateCode = this.getWebTemplateCodeOfOrganization(organizationCode);
  204. WebTemplate query = new WebTemplate();
  205. query.setCode(webTemplateCode);
  206. return webTemplateService.selectByWhere(query);
  207. }
  208. private void updateOrgWebLayoutMappingToDB(String jsonData) {
  209. Domain domain = domainService.selectByOrganizationCode(this.getLoginUser().getOrganizationCode());
  210. domain.setOrgWebLayoutMapping(jsonData);
  211. domainService.updateByPrimaryKeySelective(domain);
  212. }
  213. private List<WebDesignDTO> getSectionFillInListFromDB() {
  214. Domain domain = domainService.selectByOrganizationCode(this.getLoginUser().getOrganizationCode());
  215. List<WebDesignDTO> sectionFillInList = JSON.parseArray(domain.getOrgWebLayoutMapping(), WebDesignDTO.class);
  216. return sectionFillInList;
  217. }
  218. private String getOrganizationModuleBlockers(){
  219. getModelMap().put("moduleList", getOrganizationModuleList());
  220. return this.getContentFromTemplate("web-template/web_module_blocker", getModelMap());
  221. }
  222. private List<WebModule> getOrganizationModuleList(){
  223. return webModuleService.selectListByOrganizationCode(getLoginUser().getOrganizationCode());
  224. }
  225. }