DocController.java 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
package com.farm.wcp.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.page.ViewMode;
import com.farm.doc.dao.FarmRfDoctypeDaoInter;
import com.farm.doc.domain.FarmDocfile;
import com.farm.doc.domain.FarmDocruninfo;
import com.farm.doc.domain.FarmDoctext;
import com.farm.doc.domain.FarmDoctype;
import com.farm.doc.domain.ex.DocBrief;
import com.farm.doc.domain.ex.DocEntire;
import com.farm.doc.exception.CanNoReadException;
import com.farm.doc.exception.DocNoExistException;
import com.farm.doc.server.FarmDocIndexInter;
import com.farm.doc.server.FarmDocManagerInter;
import com.farm.doc.server.FarmDocOperateRightInter;
import com.farm.doc.server.FarmDocRunInfoInter;
import com.farm.doc.server.FarmDocgroupManagerInter;
import com.farm.doc.server.FarmDocmessageManagerInter;
import com.farm.doc.server.FarmFileIndexManagerInter;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.wcp.know.service.KnowServiceInter;
import com.farm.wcp.util.ThemesUtil;
import com.farm.web.WebUtils;

@RequestMapping("/webdoc")
@Controller
public class DocController extends WebUtils {
	private final static Logger log = Logger.getLogger(DocController.class);
	@Resource
	private FarmDocgroupManagerInter farmDocgroupManagerImpl;
	@Resource
	private FarmFileManagerInter farmFileManagerImpl;
	@Resource
	private FarmDocManagerInter farmDocManagerImpl;
	@Resource
	private FarmDocRunInfoInter farmDocRunInfoImpl;
	@Resource
	private KnowServiceInter KnowServiceImpl;
	@Resource
	private FarmDocmessageManagerInter farmDocmessageManagerImpl;
	@Resource
	private FarmDocOperateRightInter farmDocOperateRightImpl;
	@Resource
	private FarmDocIndexInter farmDocIndexManagerImpl;
	@Resource
	private FarmRfDoctypeDaoInter farmRfDoctypeDaoImpl;
	@Resource
	private FarmFileIndexManagerInter farmFileIndexManagerImpl;

	/**
	 * 查看知识
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/view/Pub{docid}", method = RequestMethod.GET)
	public ModelAndView showDoc(@PathVariable("docid") String docid, HttpSession session, HttpServletRequest request)
			throws Exception {
		ViewMode page = ViewMode.getInstance();
		try {
			DocEntire doc = farmDocManagerImpl.getDoc(docid, getCurrentUser(session));
			page.putAttr("DOCE", doc);
			List<FarmDoctext> versions = farmDocManagerImpl.getDocVersions(docid);
			page.putAttr("VERSIONS", versions);
			if (getCurrentUser(session) != null) {
				boolean isenjoy = farmDocRunInfoImpl.isEnjoyDoc(getCurrentUser(session).getId(), docid);
				page.putAttr("ISENJOY", isenjoy);
			}
			FarmDoctype type = doc.getType();
			page.putAttr("TYPEID", type == null ? "" : type.getId());
			Set<String> fileTypes = new HashSet<String>();
			for (FarmDocfile node : doc.getFiles()) {
				fileTypes.add(node.getExname().trim().replace(".", "").toUpperCase());
			}
			page.putAttr("FILETYPES", fileTypes);
			farmDocRunInfoImpl.visitDoc(docid, getCurrentUser(session), getCurrentIp(request));
			LoginUser user = getCurrentUser(session);
			if (type != null) {
				List<DocBrief> typedocs = farmDocRunInfoImpl.getTypeDocs(type == null ? "" : type.getId(),
						user == null ? "none" : user.getId(), 10);
				page.putAttr("TYPEDOCS", typedocs);
			}
			if (doc.getDoc().getDomtype().equals("1")) {
				return page.returnModelAndView(ThemesUtil.getThemePath() + "/know/view");
			}
			if (doc.getDoc().getDomtype().equals("5")) {
				return page.returnModelAndView(ThemesUtil.getThemePath() + "/webfile/webfile");
			}
			if (doc.getDoc().getDomtype().equals("4")) {
				return page.putAttr("groupid", doc.getGroup().getId()).returnRedirectUrl("/webgroup/Pubshow.do");
			}
		} catch (CanNoReadException e) {
			return ViewMode.getInstance().setError(e.toString())
					.returnModelAndView(ThemesUtil.getThemePath() + "/error");
		} catch (DocNoExistException e) {
			return ViewMode.getInstance().setError(e.toString())
					.returnModelAndView(ThemesUtil.getThemePath() + "/error");
		} catch (Exception e) {
			e.printStackTrace();
			return ViewMode.getInstance().setError(e.toString())
					.returnModelAndView(ThemesUtil.getThemePath() + "/error");
		}
		return ViewMode.getInstance().setError("请实现正确的DOCTYPE类型解析")
				.returnModelAndView(ThemesUtil.getThemePath() + "/error");
	}

	/**
	 * 查看附件
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/view/PubFile{fileid}", method = RequestMethod.GET)
	public ModelAndView showFile(@PathVariable("fileid") String fileid, HttpSession session, HttpServletRequest request)
			throws Exception {
		ViewMode page = ViewMode.getInstance();
		try {
			FarmDocfile file = farmFileManagerImpl.getFile(fileid);
			if (file == null) {
				throw new DocNoExistException();
			}
			file.setUrl(farmFileManagerImpl.getFileURL(file.getId()));
			page.putAttr("file", file);
		} catch (DocNoExistException e) {
			log.error(e.getMessage());
			farmFileIndexManagerImpl.delFileLucenneIndex(fileid);
			return ViewMode.getInstance().setError(e.toString())
					.returnModelAndView(ThemesUtil.getThemePath() + "/error");
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.toString())
					.returnModelAndView(ThemesUtil.getThemePath() + "/error");
		}
		return page.returnModelAndView(ThemesUtil.getThemePath() + "/webfile/file");
	}

	@RequestMapping("/enjoy")
	@ResponseBody
	public Map<String, Object> enjoy(HttpSession session, String id) {
		try {
			farmDocRunInfoImpl.enjoyDoc(getCurrentUser(session).getId(), id);
			return ViewMode.getInstance().putAttr("commitType", "0").returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().putAttr("commitType", "1").setError(e.getMessage()).returnObjMode();
		}
	}

	@RequestMapping("/FLunEnjoy")
	@ResponseBody
	public Map<String, Object> unenjoy(HttpSession session, String id) {
		try {
			farmDocRunInfoImpl.unEnjoyDoc(getCurrentUser(session).getId(), id);
			return ViewMode.getInstance().putAttr("commitType", "0").returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().putAttr("commitType", "1").setError(e.getMessage()).returnObjMode();
		}
	}

	@RequestMapping("/PubVersion")
	public ModelAndView showVersion(String textid, HttpSession session, HttpServletRequest request) {
		ViewMode page = ViewMode.getInstance();
		try {
			DocEntire doc = farmDocManagerImpl.getDocVersion(textid, getCurrentUser(session));
			if (!doc.getDoc().getState().equals("1")) {
				throw new RuntimeException("没有权限访问该文档");
			}
			List<FarmDoctext> versions = farmDocManagerImpl.getDocVersions(doc.getDoc().getId());
			page.putAttr("VERSIONS", versions);
			return page.putAttr("DOCE", doc).returnModelAndView(ThemesUtil.getThemePath() + "/know/version");
		} catch (Exception e) {
			return page.setError(e.getMessage()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
		}
	}

	/**
	 * 公开文档(将该文档开放阅读和编辑权限,同时如果是小组文档将删除小组所有权)
	 * 
	 * @return
	 */
	@RequestMapping("/FLflyKnow")
	public ModelAndView flyKnow(String id, HttpSession session) {
		try {
			farmDocOperateRightImpl.flyDoc(id, getCurrentUser(session));
			return ViewMode.getInstance().returnRedirectUrl("/webdoc/view/Pub" + id + ".html");
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.toString())
					.returnModelAndView(ThemesUtil.getThemePath() + "/error");
		}
	}

	/**
	 * 删除知识
	 * 
	 * @return
	 */
	@RequestMapping("/FLDelKnow")
	public ModelAndView delCommit(String id, HttpSession session) {
		try {
			DocEntire doc = farmDocManagerImpl.deleteDoc(id, getCurrentUser(session));
			// 只有资源文件才有附件索引
			if (doc.getDoc().getDomtype().equals("5")) {
				// 删除附件索引
				for (FarmDocfile file : doc.getFiles()) {
					farmFileIndexManagerImpl.delFileLucenneIndex(file.getId(), doc);
				}
			}
			return ViewMode.getInstance().putAttr("MESSAGE", "删除成功!")
					.returnModelAndView(ThemesUtil.getThemePath() + "/message");
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.toString())
					.returnModelAndView(ThemesUtil.getThemePath() + "/error");
		}
	}

	@RequestMapping("/delImg")
	public Map<String, Object> delImg(String imgid) {
		try {
			farmDocManagerImpl.delImg(imgid);
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e.toString()).returnObjMode();
		}
	}

	@RequestMapping("/PubPraiseYes")
	@ResponseBody
	public Map<String, Object> praiseYes(String id, HttpSession session, HttpServletRequest request) {
		try {
			if (getCurrentUser(session) == null) {
				farmDocRunInfoImpl.praiseDoc(id, request.getRemoteAddr());
			} else {
				farmDocRunInfoImpl.praiseDoc(id, getCurrentUser(session), request.getRemoteAddr());
			}
			FarmDocruninfo runinfo = farmDocRunInfoImpl.loadRunInfo(id);
			return ViewMode.getInstance().putAttr("runinfo", runinfo).returnObjMode();
		} catch (Exception e) {
			e.printStackTrace();
			return ViewMode.getInstance().setError(e.toString()).returnObjMode();
		}
	}

	@RequestMapping("/PubPraiseNo")
	@ResponseBody
	public Map<String, Object> praiseNo(String id, HttpSession session, HttpServletRequest request) {
		try {
			if (getCurrentUser(session) == null) {
				farmDocRunInfoImpl.criticalDoc(id, getCurrentIp(request));
			} else {
				farmDocRunInfoImpl.criticalDoc(id, getCurrentUser(session), getCurrentIp(request));
			}
			FarmDocruninfo runinfo = farmDocRunInfoImpl.loadRunInfo(id);
			return ViewMode.getInstance().putAttr("runinfo", runinfo).returnObjMode();
		} catch (Exception e) {
			e.printStackTrace();
			return ViewMode.getInstance().setError(e.toString()).returnObjMode();
		}
	}
}