Java tutorial
/* * Copyright 2015 ireader.com All right reserved. This software is the * confidential and proprietary information of ireader.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with ireader.com. */ package com.apus.hades.admin.web.controller.ad.source; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import com.apus.hades.admin.AdminConstant; import com.apus.hades.admin.definition.OperatLog; import com.apus.hades.admin.util.Struts2Utils; import com.apus.hades.admin.web.controller.BaseController; import com.apus.hades.common.exception.FileIOException; import com.apus.hades.common.exception.WebException; import com.apus.hades.common.orm.mybatis.paginator.domain.PageBounds; import com.apus.hades.common.orm.mybatis.paginator.domain.PageList; import com.apus.hades.core.common.ClickAction; import com.apus.hades.core.common.SystemHandle; import com.apus.hades.core.common.SystemModule; import com.apus.hades.core.manager.ad.ShowTypeManager; import com.apus.hades.core.manager.ad.SourceManager; import com.apus.hades.core.model.ad.ShowLimit; import com.apus.hades.core.model.ad.ShowType; import com.apus.hades.core.model.ad.Source; import com.apus.hades.core.model.ad.SourceRelValue; import com.apus.hades.file.impl.DefaultFileApiServiceImpl; /** * @Descriptions . * @date 201532 * @author wangliping */ public class SourceController extends BaseController { private static final long serialVersionUID = 8102044635267854063L; private final static Logger LOGGER = LoggerFactory.getLogger(SourceController.class); @Autowired private SourceManager sourceManager; @Autowired private ShowTypeManager showTypeManager; private Source source; private PageList<Source> pages; public List<ShowType> showTypes; public ClickAction[] actions = ClickAction.values(); public String add() { showTypes = showTypeManager.findAll(); return "add"; } public String edit() { if (null == source) { long id = Struts2Utils.getLong("id"); source = sourceManager.find(id); } showTypes = showTypeManager.findAll(); return "edit"; } public String detail() { if (null == source) { long id = Struts2Utils.getLong("id"); source = sourceManager.find(id); } showTypes = showTypeManager.findAll(); return "detail"; } /** * ajax limit. */ @OperatLog(Handle = SystemHandle.DELETE, Module = SystemModule.SOURCE_MODEL, Desc = "ajax limit.") public void deleteLimitType() { JSONObject json = new JSONObject(); boolean success = true; try { Long limitId = Struts2Utils.getLong("limitid"); Long id = Struts2Utils.getLong("id"); Source source = sourceManager.find(id); SourceRelValue sourceRelValue = source.getSourceRelValue(limitId); if (null != sourceRelValue) { sourceManager.deleteSourceRelValue(sourceRelValue.getId()); } } catch (Exception e) { success = false; json.put("msg", e.getMessage()); } json.put("success", success); Struts2Utils.renderJson(json); } /** * ajax ?. */ public void getSourceLimit() { JSONObject json = new JSONObject(); JSONArray array = new JSONArray(); boolean success = true; try { Long id = Struts2Utils.getLong("id"); Long typeId = Struts2Utils.getLong("typeId"); source = sourceManager.find(id); ShowType showType = showTypeManager.find(typeId); List<ShowLimit> limits = showType.getShowLimitList(); List<SourceRelValue> values = null; // ? ? if (source.getAd_type_id().longValue() == typeId.longValue()) { values = source.getSourceRelValueList(); } JSONObject obj; if (null != values && !values.isEmpty()) { ShowLimit limit; for (SourceRelValue value : values) { obj = new JSONObject(); limit = showType.getShowLimit(value.getRel_ad_show_limit_id()); if (null == limit) { obj.put("fail", true); obj.put("source", source.getId()); } else { obj.put("style", limit.getStyleEnum()); obj.put("size", limit.getPic_size()); obj.put("limit", limit.getLimit_size()); obj.put("proportion", limit.getPic_proportion()); obj.put("name", limit.getStyleEnum().getName()); obj.put("fail", false); } obj.put("value", getSourceVal(limit, value.getValue())); obj.put("id", value.getRel_ad_show_limit_id()); obj.put("clickAction", value.getClick_action()); obj.put("actionUrl", value.getAction_url()); array.add(obj); } } // if (null != limits && !limits.isEmpty()) { for (ShowLimit limit : limits) { if (!source.isExistsLimit(limit.getId())) { obj = new JSONObject(); obj.put("id", limit.getId()); obj.put("style", limit.getStyleEnum()); obj.put("name", limit.getStyleEnum().getName()); obj.put("size", limit.getPic_size()); obj.put("proportion", limit.getPic_proportion()); obj.put("limit", limit.getLimit_size()); obj.put("value", StringUtils.EMPTY); obj.put("fail", false); array.add(obj); } } } } catch (Exception e) { success = false; e.printStackTrace(); json.put("msg", e.getMessage()); } json.put("success", success); json.put("array", array); Struts2Utils.renderJson(json); } @OperatLog(Handle = SystemHandle.ADD, Module = SystemModule.SOURCE_MODEL) public String save() { try { if (null == source) { throw new WebException("???"); } if (source.getClick_actionEnum() == ClickAction.NONE) { source.setAction_url(StringUtils.EMPTY); } sourceManager.save(source); this.saveSourceRelValue(); } catch (WebException e) { addActionMessage(e.getMessage()); } catch (FileIOException e) { addActionError(",??"); LOGGER.error(e.getMessage(), e); return add(); } catch (Exception e) { addActionError("?,?:" + e.getMessage()); LOGGER.error(e.getMessage(), e); return add(); } return list(); } /** * ???. * * @param limit * @return */ private String getInputName(ShowLimit limit) { return limit.getStyleEnum().name() + "_" + limit.getId(); } /** * . * @throws FileIOException * * @throws IOException */ @OperatLog(Handle = SystemHandle.ADD, Module = SystemModule.SOURCE_MODEL, Desc = ".") private void saveSourceRelValue() throws FileIOException { ShowType showType = showTypeManager.find(source.getAd_type_id()); List<ShowLimit> showLimits = showType.getShowLimitList(); if (null == showLimits || showLimits.isEmpty()) { return; } SourceRelValue relValue; for (ShowLimit limit : showLimits) { relValue = new SourceRelValue(source.getId(), limit.getId()); String inputName = getInputName(limit); String value = StringUtils.EMPTY; if (limit.getStyleEnum().isWord()) { value = Struts2Utils.getParameter(inputName); } else { File file = Struts2Utils.getFile(inputName); String contentType = Struts2Utils.getContentTypes(inputName); //api String fileName = this.getfileName(source.getId(), limit.getId(), getExtention(contentType)); DefaultFileApiServiceImpl.getInstance().putFile(fileName, file); value = fileName; } relValue.setValue(value); sourceManager.addSourceRelValue(source.getId(), relValue); } } private String getExtention(String fileName) { int pos = fileName.lastIndexOf("/"); return fileName.substring(pos + 1); } private String getfileName(Long sourceId, Long limitId, String fileName) { return source.getSourceFilePath() + limitId + "." + fileName; } /** * . * @throws FileIOException * * @throws IOException */ @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.SOURCE_MODEL, Desc = ".") private void updateSourceRelValue() throws FileIOException { ShowType showType = showTypeManager.find(source.getAd_type_id()); List<ShowLimit> showLimits = showType.getShowLimitList(); // 1.?. if (null != showLimits && !showLimits.isEmpty()) { SourceRelValue relValue; for (ShowLimit limit : showLimits) { if (!source.isExistsLimit(limit.getId())) { relValue = new SourceRelValue(source.getId(), limit.getId()); relValue.setValue(StringUtils.EMPTY); sourceManager.addSourceRelValue(source.getId(), relValue); } } // ?source source = sourceManager.find(source.getId()); } List<SourceRelValue> relValues = source.getSourceRelValueList(); if (null == relValues || relValues.isEmpty()) { return; } // ?SourceRelValue?. ShowLimit limit; for (SourceRelValue relValue : relValues) { limit = showType.getShowLimit(relValue.getRel_ad_show_limit_id()); if (null == limit) { continue; } String inputName = getInputName(limit); String value = null; if (limit.getStyleEnum().isWord()) { value = Struts2Utils.getParameter(inputName); relValue.setValue(value); } else { File file = Struts2Utils.getFile(inputName); if (null == file) { continue; } String contentType = Struts2Utils.getContentTypes(inputName); //api String fileName = this.getfileName(source.getId(), limit.getId(), getExtention(contentType)); DefaultFileApiServiceImpl.getInstance().updateFile(fileName, file); relValue.setValue(fileName); } sourceManager.updateSourceRelValue(relValue); } } /** * ??. * @throws FileIOException */ @OperatLog(Handle = SystemHandle.DELETE, Module = SystemModule.SOURCE_MODEL, Desc = "??.") private void clearSourceRelValue() throws FileIOException { List<SourceRelValue> relValues = source.getSourceRelValueList(); if (null == relValues || relValues.isEmpty()) { return; } for (SourceRelValue relValue : relValues) { sourceManager.deleteSourceRelValue(relValue.getId()); // if (!relValue.getStyle_codeEnum().isWord()) { DefaultFileApiServiceImpl.getInstance().delFile(relValue.getValue()); } } } @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.SOURCE_MODEL) public String update() { try { if (null == source || null == source.getId()) { throw new WebException("??"); } if (source.getClick_actionEnum() == ClickAction.NONE) { source.setAction_url(StringUtils.EMPTY); } sourceManager.update(source); Long typeId = Struts2Utils.getLong("defaultTypeId"); source = sourceManager.find(source.getId()); if (source.getAd_type_id().longValue() == typeId.longValue()) { // ? updateSourceRelValue(); } else { // ? clearSourceRelValue(); // ?? saveSourceRelValue(); } } catch (WebException e) { addActionMessage(e.getMessage()); } catch (FileIOException e) { addActionError(",??"); LOGGER.error(e.getMessage(), e); return add(); } catch (Exception e) { addActionError("?:" + e.getMessage()); LOGGER.error(e.getMessage(), e); return edit(); } return list(); } @OperatLog(Handle = SystemHandle.DELETE, Module = SystemModule.SOURCE_MODEL) public String delete() { try { Long id = Struts2Utils.getLong("id"); if (null == id) { throw new WebException("?"); } sourceManager.delete(id); } catch (WebException e) { addActionMessage(e.getMessage()); } catch (Exception e) { addActionError("?:" + e.getMessage()); LOGGER.error(e.getMessage(), e); } return list(); } public String list() { Map<String, Object> params = new HashMap<String, Object>(); String showTypeId = Struts2Utils.getParameter("ad_show_type_id"); if (StringUtils.isNotBlank(showTypeId)) { params.put("ad_type_id", Long.parseLong(showTypeId)); Struts2Utils.setAttribute("ad_show_type_id", showTypeId); } String name = Struts2Utils.getParameter("name"); if (StringUtils.isNotBlank(name)) { params.put("name", name); Struts2Utils.setAttribute("name", name); } pages = sourceManager.findByPage(params, new PageBounds(pagination.getPageNo(), pagination.getPageSize())); super.setPagination(pages.getPagination()); showTypes = showTypeManager.findAll(); return "list"; } /** * ?? * * @param limit * @param val */ private String getSourceVal(ShowLimit limit, String value) { if (!limit.getStyleEnum().isWord()) { value = AdminConstant.S3_DOMAIN + value; } return value; } public Source getSource() { return source; } public void setSource(Source source) { this.source = source; } public PageList<Source> getPages() { return pages; } public void setPages(PageList<Source> pages) { this.pages = pages; } public List<ShowType> getShowTypes() { return showTypes; } public void setShowTypes(List<ShowType> showTypes) { this.showTypes = showTypes; } public ClickAction[] getActions() { return actions; } public void setActions(ClickAction[] actions) { this.actions = actions; } }