/211Project/to211-web/src/main/java/com/to211/web/controller/OrganizationWebTemplateController.java
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
- package com.to211.web.controller;
-
- import java.io.FileInputStream;
- import java.util.ArrayList;
- import java.util.List;
-
- import javax.annotation.Resource;
-
- import org.apache.commons.lang.StringUtils;
- import org.springframework.stereotype.Controller;
- import org.springframework.validation.ObjectError;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.servlet.ModelAndView;
-
- import com.alibaba.fastjson.JSON;
- import com.fdhay.authcenter.proxy.domain.annotation.Authorization;
- import com.fdhay.authcenter.proxy.domain.enums.AuthorizationType;
- import com.to211.common.util.FileUtils;
- import com.to211.common.util.MimeTypeUtils;
- import com.to211.common.util.WebResourceUtils;
- import com.to211.domain.Domain;
- import com.to211.domain.WebDesignDTO;
- import com.to211.domain.WebModule;
- import com.to211.domain.WebTemplate;
- import com.to211.domain.base.Page;
- import com.to211.domain.base.json.JsonMessage;
- import com.to211.service.DirectoryOfWebTemplateService;
- import com.to211.service.DomainService;
- import com.to211.service.WebModuleService;
-
- @Controller
- @RequestMapping("/org/webTemplate")
- public class OrganizationWebTemplateController extends OrganizationController {
-
- @Resource private DomainService domainService;
- @Resource private DirectoryOfWebTemplateService directoryOfWebTemplateService;
- @Resource private WebModuleService webModuleService;
-
- /**
- * 机构用户设计自己的主页页面布局
- */
- @RequestMapping(value="/design/{organizationCode}/**", method=RequestMethod.GET)
- @Authorization(type=AuthorizationType.AUTHENTICATED)
- public ModelAndView design(@PathVariable String organizationCode) {
- try {
- this.checkWithLoginUserOrganization(organizationCode);
- String templateCode = getWebTemplateCodeOfOrganization(organizationCode);
-
- String webResourceName = getWebResourceName(String.format("/design/%s/", organizationCode));
- String webTempalteDataDir = directoryOfWebTemplateService.getTemplateDataBaseDirAndCreateIfNotExist(templateCode);
- //如果是html页面,则调用freemarker模板生成,其他文件直接返回到客户端
- if(isHtmlPage(webResourceName)){
- String content = this.getContentFromTemplateDir(webTempalteDataDir, webResourceName, getModelMap());
- getModelMap().put("organizationCode", organizationCode);
- getModelMap().put("moduleBlockers", this.getOrganizationModuleBlockers());
- return toViewOfContentWithLayout(content, "web-template/design", getModelMap());
- }else{
- getResponse().setContentType(MimeTypeUtils.getMimeTypeOfResource(webResourceName));
- FileInputStream fis = new FileInputStream(getWebResourceFullName(webTempalteDataDir, webResourceName));
- FileUtils.copy(fis, getResponse().getOutputStream());
- return null;
- }
- } catch (Exception e) {
- logger.error(e);
- return toError(e.getMessage());
- }
- }
-
- /**
- * 保存用户主页设计
- * @param jsonData - eg:[{"sectionId": "section_1", "moduleId": "module_1"},...]
- * @return
- */
- @RequestMapping(value="/saveDesign", method=RequestMethod.POST)
- @Authorization(type=AuthorizationType.AUTHENTICATED)
- public ModelAndView saveDesign(String jsonData) {
- try {
- if(StringUtils.isEmpty(jsonData)){
- return toError("提交的数据为空!");
- }
- logger.debug(jsonData);
- //把用户填充的排版哪个section显示哪个模块的对应关系入库
- this.updateOrgWebLayoutMappingToDB(jsonData);
-
- return toHTML("保存成功");
- } catch (Exception e) {
- logger.error(e);
- return toError(e.getMessage());
- }
- }
-
- @RequestMapping(value="/view/{organizationCode}/**", method=RequestMethod.GET)
- @Authorization(type=AuthorizationType.AUTHENTICATED)
- public ModelAndView view(@PathVariable String organizationCode) {
- try {
- String templateCode = getWebTemplateCodeOfOrganization(organizationCode);
-
- String webTempalteDataDir = directoryOfWebTemplateService.getTemplateDataBaseDirAndCreateIfNotExist(templateCode);
-
- String webResourceName = getWebResourceName(String.format("/view/%s/", organizationCode));
- //如果是html页面,则调用freemarker模板生成,其他文件直接返回到客户端
- if(isHtmlPage(webResourceName)){
- String content = this.generateWebContent(templateCode, this.getSectionFillInListFromDB());
- getModelMap().put("domain", domainService.selectByOrganizationCode(organizationCode));
- return toViewOfContentWithLayout(content, "web-template/design_preview", getModelMap());
- }else{
- getResponse().setContentType(MimeTypeUtils.getMimeTypeOfResource(webResourceName));
- FileInputStream fis = new FileInputStream(getWebResourceFullName(webTempalteDataDir, webResourceName));
- FileUtils.copy(fis, getResponse().getOutputStream());
- return null;
- }
- } catch (Exception e) {
- logger.error(e);
- return toError(e.getMessage());
- }
- }
-
- /**
- * 生成新web内容
- * @return
- */
- @RequestMapping(value="/generateNewWeb", method=RequestMethod.POST)
- @Authorization(type=AuthorizationType.AUTHENTICATED)
- public ModelAndView generateNewWeb() {
- try {
- String organizationCode = getLoginUser().getOrganizationCode();
- String webTemplateCode = this.getWebTemplateCodeOfOrganization(organizationCode);
- super.initOrganizationWebContent(organizationCode, webTemplateCode, getSectionFillInListFromDB());
- return toHTML("生成成功");
- } catch (Exception e) {
- logger.error(e);
- return toError(e.getMessage());
- }
- }
-
- /**********************************机构所用模板***********************************************/
- @RequestMapping(value="/listTemplate")
- @Authorization(type=AuthorizationType.AUTHENTICATED)
- public ModelAndView getWebTemplateList(Page<WebTemplate> page){
- String organizationCode = getLoginUser().getOrganizationCode();
- webTemplateService.selectOrganizationWebTemplateListByPage(organizationCode, page);
- WebTemplate currentWebTemplate = this.getWebTemplateOfOrganization(organizationCode);
- getModelMap().put("page", page);
- getModelMap().put("currentWebTemplate", currentWebTemplate);
- return toViewWithoutLayout("organization/organization-web-template-list", getModelMap());
- }
-
- /**
- * 更新机构所用的模板
- * @param userWebTemplateId
- * @param webTemplateCode
- * @return
- */
- @RequestMapping(value = "/updateTemplate")
- @Authorization(type=AuthorizationType.AUTHENTICATED)
- public ModelAndView updateWebTemplate(Integer webTemplateId) {
- List<ObjectError> errors = new ArrayList<ObjectError>(1);
-
- if(webTemplateId == null || webTemplateId < 0){
- ObjectError error = new ObjectError("organization", "没有选择模板");
- errors.add(error);
- }
- WebTemplate webTemplate = webTemplateService.selectByPrimaryKey(webTemplateId);
- if(webTemplate == null){
- ObjectError error = new ObjectError("organization", "没有模板记录");
- errors.add(error);
- }
- if(!errors.isEmpty()){
- getModelMap().put("errorList", errors);
- return toViewWithoutLayout("organization/organization-web-template-list", getModelMap());
- }
- //更新机构所用的模板
- Domain domain = domainService.selectByOrganizationCode(this.getLoginUser().getOrganizationCode());
- domain.setWebTemplateCode(webTemplate.getCode());
- domainService.updateByPrimaryKeySelective(domain);
-
- JsonMessage msg = JsonMessage.sucess();
- return toJSON(msg);
- }
-
- /**********************************~: end 机构所用模板***********************************************/
-
- /**
- * 检测设计的主页布局是不是自己的主页布局
- * @param organizationCode
- * @throws IllegalArgumentException
- */
- private void checkWithLoginUserOrganization(String organizationCode) throws IllegalArgumentException{
- if(!isStaticResource()
- && !StringUtils.equals(organizationCode, getLoginUser().getOrganizationCode())){
- throw new IllegalArgumentException("对不起,您只能设计您自己的主页布局!");
- }
- }
-
- /**
- * 判断请求资源是否为静态资源
- * @param request
- * @return
- */
- private boolean isStaticResource(){
- return WebResourceUtils.isStaticResource(getRequest().getRequestURI());
- }
-
- /**
- * 获取机构的主页对应的模板码
- * @param organizationCode
- * @return
- */
- private String getWebTemplateCodeOfOrganization(String organizationCode) {
- Domain domain = domainService.selectByOrganizationCode(organizationCode);
- String webTemplateCode = domain.getWebTemplateCode();
- if(StringUtils.isNotEmpty(webTemplateCode)){
- return webTemplateCode;
- }
- return ORGANIZATION_WEB_DEFAULT_WEB_TEMPLATE_CODE;
- }
-
- /**
- * 获取机构主页对应的模板对象
- * @param organizationCode
- * @return
- */
- private WebTemplate getWebTemplateOfOrganization(String organizationCode) {
- String webTemplateCode = this.getWebTemplateCodeOfOrganization(organizationCode);
- WebTemplate query = new WebTemplate();
- query.setCode(webTemplateCode);
- return webTemplateService.selectByWhere(query);
- }
-
- private void updateOrgWebLayoutMappingToDB(String jsonData) {
- Domain domain = domainService.selectByOrganizationCode(this.getLoginUser().getOrganizationCode());
- domain.setOrgWebLayoutMapping(jsonData);
- domainService.updateByPrimaryKeySelective(domain);
- }
-
- private List<WebDesignDTO> getSectionFillInListFromDB() {
- Domain domain = domainService.selectByOrganizationCode(this.getLoginUser().getOrganizationCode());
- List<WebDesignDTO> sectionFillInList = JSON.parseArray(domain.getOrgWebLayoutMapping(), WebDesignDTO.class);
- return sectionFillInList;
- }
-
- private String getOrganizationModuleBlockers(){
- getModelMap().put("moduleList", getOrganizationModuleList());
- return this.getContentFromTemplate("web-template/web_module_blocker", getModelMap());
- }
-
- private List<WebModule> getOrganizationModuleList(){
- return webModuleService.selectListByOrganizationCode(getLoginUser().getOrganizationCode());
- }
- }