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.app; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; 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.cache.ConfigCache; import com.apus.hades.admin.definition.OperatLog; import com.apus.hades.admin.dto.AdPlatformDTO; import com.apus.hades.admin.service.ad.IPositionService; import com.apus.hades.admin.util.Struts2Utils; import com.apus.hades.admin.web.controller.BaseController; import com.apus.hades.common.CommonConstant; import com.apus.hades.common.exception.WebException; import com.apus.hades.common.utils.DateUtil; import com.apus.hades.core.common.AgeRangeType; import com.apus.hades.core.common.CheckFlag; import com.apus.hades.core.common.GenderType; import com.apus.hades.core.common.InvertType; import com.apus.hades.core.common.PushType; import com.apus.hades.core.common.Status; import com.apus.hades.core.common.SystemHandle; import com.apus.hades.core.common.SystemModule; import com.apus.hades.core.common.ValidDateType; import com.apus.hades.core.common.Week; import com.apus.hades.core.manager.ad.AppManager; import com.apus.hades.core.manager.ad.PositionManager; import com.apus.hades.core.manager.ad.ShowTypeManager; import com.apus.hades.core.manager.system.DictionaryManager; import com.apus.hades.core.model.ad.App; import com.apus.hades.core.model.ad.Position; import com.apus.hades.core.model.ad.RealPlatformInfo; import com.apus.hades.core.model.ad.ShowType; import com.apus.hades.core.model.system.Dictionary; /** * @Descriptions ??. * @date 2015211 * @author wangliping */ public class PositionController extends BaseController { private static final long serialVersionUID = 1953491977461137450L; private final static Logger LOGGER = LoggerFactory.getLogger(PositionController.class); @Autowired private PositionManager positionManager; @Autowired private ShowTypeManager showTypeManager; @Autowired private AppManager appManager; @Autowired private DictionaryManager dictionaryManager; @Autowired private IPositionService positionService; private RealPlatformInfoForm realPlatformInfoForm; private List<ShowType> types; private Position position; private Status[] status = Status.values(); private CheckFlag[] flags = CheckFlag.values(); private PushType[] pushTypes = PushType.values(); private InvertType[] invertTypes = InvertType.values(); private AgeRangeType[] ageRangeTypes = AgeRangeType.values(); private GenderType[] genderTypes = GenderType.values(); private Week[] weeks = Week.values(); private RealPlatformInfo realPlatformInfo; private List<RealPlatformInfo> infos; private Long[] showTypeIds; /** * . * * @return */ public String add() { Long appId = Struts2Utils.getLong("id"); Struts2Utils.setAttribute("appId", appId); types = showTypeManager.findAll(); return "addPosition"; } public String setDate() { Long realPlatformInfoId = Struts2Utils.getLong("id"); RealPlatformInfo realPlatformInfo = positionManager.findRealPlatformInfo(realPlatformInfoId); Struts2Utils.setAttribute("realPlatformInfo", realPlatformInfo); return "setDate"; } @OperatLog(Handle = SystemHandle.SET, Module = SystemModule.POSITION_MODEL, Desc = "sheduleRealPlatformInfoByValidDates") public void sheduleRealPlatformInfoByValidDates() { JSONObject ajaxJson = new JSONObject(); ajaxJson.put("success", false); try { Long realPlatformInfoId = Struts2Utils.getLong("id"); String validDatesStr = Struts2Utils.getParameter("validDates"); String timeDuringStr = Struts2Utils.getParameter("timeDuring"); String whatDay = Struts2Utils.getParameter("whatDay"); String startDate = Struts2Utils.getParameter("startDate"); String endDate = Struts2Utils.getParameter("endDate"); String validDateTypeStr = Struts2Utils.getParameter("validDateType"); ValidDateType type = ValidDateType.valueOf(validDateTypeStr); switch (type) { case DATES: logger.info("sheduleRealPlatformInfo {} {} {}", realPlatformInfoId, validDatesStr, timeDuringStr); if (StringUtils.isBlank(validDatesStr) || StringUtils.isBlank(timeDuringStr)) { throw new WebException("??"); } String[] validDates = validDatesStr.split(","); String[] timeDuring = timeDuringStr.split(","); if (null == validDates || null == timeDuring) { ajaxJson.put("msg", ",?"); Struts2Utils.renderJson(ajaxJson); return; } positionManager.sheduleRealPlatformInfo(realPlatformInfoId, type, timeDuring, null, null, null, validDates); break; case RANGE: logger.info("sheduleRealPlatformInfo {} {} {}", realPlatformInfoId, timeDuringStr, startDate, endDate, whatDay); if (null == whatDay || StringUtils.isBlank(timeDuringStr) || StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) { throw new WebException("??"); } String[] timeDurings = timeDuringStr.split(","); positionManager.sheduleRealPlatformInfo(realPlatformInfoId, type, timeDurings, DateUtil.strToDate(startDate, "yyyy-MM-dd"), DateUtil.strToDate(endDate, "yyyy-MM-dd"), whatDay.split(","), null); break; case ACCURATE_TIME: logger.info("sheduleRealPlatformInfo {} {} {}", realPlatformInfoId, startDate, endDate); if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) { throw new WebException("??"); } positionManager.sheduleRealPlatformInfo(realPlatformInfoId, type, null, DateUtil.strToDate(startDate, "yyyy-MM-dd HH:mm:ss"), DateUtil.strToDate(endDate, "yyyy-MM-dd HH:mm:ss"), whatDay.split(","), null); break; default: throw new IOException(""); } ajaxJson.put("success", true); } catch (WebException e) { ajaxJson.put("msg", e.getMessage()); } catch (Exception e) { LOGGER.error("??:", e); ajaxJson.put("msg", "??:" + e.getMessage()); } Struts2Utils.renderJson(ajaxJson); } /** * . * * @return */ public String edit() { if (null == position) { Long id = Struts2Utils.getLong("id"); position = positionManager.findPosition(id); types = showTypeManager.findAll(); } return "editPosition"; } /** * ?. * * @return */ @OperatLog(Handle = SystemHandle.ADD, Module = SystemModule.POSITION_MODEL) public String save() { try { if (null == position) { throw new WebException("????"); } String ids = StringUtils.join(showTypeIds, CommonConstant.SEPARATOR_COMMA); position.setRel_ad_type_ids(ids); position.setLast_modify_time(new Date()); position = positionManager.save(position); listPosition(position.getRel_app_id(), position.getId()); } catch (WebException e) { addActionMessage(e.getMessage()); return list(); } catch (Exception e) { addActionError("??,?:" + e.getMessage()); LOGGER.error(e.getMessage(), e); return add(); } return "listPosition"; } /** * . * * @return */ @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.POSITION_MODEL) public String update() { try { if (null == position) { throw new WebException("???"); } String ids = position.getRel_ad_type_ids(); if (null != showTypeIds) { ids += CommonConstant.SEPARATOR_COMMA + StringUtils.join(showTypeIds, CommonConstant.SEPARATOR_COMMA); } position.setRel_ad_type_ids(ids); position = positionManager.update(position); listPosition(position.getRel_app_id(), position.getId()); } catch (WebException e) { addActionMessage(e.getMessage()); } catch (Exception e) { addActionError("??:" + e.getMessage()); LOGGER.error(e.getMessage(), e); return edit(); } return list(); } public String list() { try { Long appId = Struts2Utils.getLong("appId"); Long positionId = Struts2Utils.getLong("positionId"); if (null != position) { listPosition(position.getRel_app_id(), position.getId()); } else if (null != appId || null != positionId) { listPosition(appId, positionId); } } catch (Exception e) { e.printStackTrace(); } return "listPosition"; } private void listPosition(Long appId, Long positionId) { if (null != positionId) { position = positionManager.findPosition(positionId); appId = position.getRel_app_id(); } Struts2Utils.setAttribute("appId", appId); List<Position> positions = positionManager.findPositionByAppId(appId); if (null == positions || positions.isEmpty()) { return; } Struts2Utils.setAttribute("positions", positions); if (null == position) { position = positions.get(0); } infos = positionManager.findRealPlatformInfoByPositionId(position.getId()); } /** * ?/??. * * @return */ public String chooselist() { Long positionId = Struts2Utils.getLong("id"); List<AdPlatformDTO> dtos = positionService.findByPositionId(positionId); Struts2Utils.setAttribute("dtos", dtos); position = positionManager.findPosition(positionId); return "chooselist"; } /** * ajax ?-?. */ @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.POSITION_MODEL, Desc = "ajax ?-?.") public void modifySetting() { boolean success = true; try { Long realPlatformInfoId = Struts2Utils.getLong("id"); String status = Struts2Utils.getParameter("status"); float probability = Struts2Utils.getFloat("probability"); int max_per_day = Struts2Utils.getInt("max_per_day"); int priority = Struts2Utils.getInt("priority"); int weights = Struts2Utils.getInt("weights"); String checkFlag = Struts2Utils.getParameter("checkFlag"); positionManager.updateRealPlatformInfo(realPlatformInfoId, Status.valueOf(status), probability, max_per_day, priority, CheckFlag.valueOf(checkFlag), weights); } catch (Exception e) { success = false; LOGGER.error(e.getMessage(), e); } JSONObject json = new JSONObject(); json.put("success", success); Struts2Utils.renderJson(json); } /** * ajax ?. */ @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.POSITION_MODEL, Desc = "ajax ?.") public void modifyPushType() { boolean success = true; try { Long positionId = Struts2Utils.getLong("positionId"); String pushType = Struts2Utils.getParameter("pushType"); position = positionManager.findPosition(positionId); position.setAd_push_typeEnum(PushType.valueOf(pushType)); positionManager.update(position); } catch (Exception e) { LOGGER.error(e.getMessage(), e); success = false; } JSONObject json = new JSONObject(); json.put("success", success); Struts2Utils.renderJson(json); } /** * /??. */ @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.POSITION_MODEL, Desc = "/??") public String choose() { String[] checks = Struts2Utils.getParameters("checkIds"); Long positionId = Struts2Utils.getLong("positionId"); position = positionManager.findPosition(positionId); List<AdPlatformDTO> dtos = positionService.findCheckedByPositionId(positionId); // if (null == checks || checks.length == 0) { if (null != dtos && !dtos.isEmpty()) { for (AdPlatformDTO dto : dtos) { positionManager.deletePlatformInfo(positionId, dto.getId()); } } return list(); } // ?. List<AdPlatformDTO> simples = new ArrayList<AdPlatformDTO>(); for (String check : checks) { Long id = Long.parseLong(StringUtils.substringBefore(check, "_")); simples.add(new AdPlatformDTO(id)); } // ? ?. if (null == dtos || dtos.isEmpty()) { for (AdPlatformDTO dto : simples) { positionManager.addPlatformInfo(positionId, ConfigCache.LIMIT_PARTNER_IDS.getValue(), dto.getId()); } return list(); } for (AdPlatformDTO dto : dtos) { // check ?? dto.setChecked(false); for (AdPlatformDTO sm : simples) { // ? true if (dto.equals(sm)) { dto.setChecked(true); sm.setChecked(true); break; } } // ? if (!dto.isChecked()) { positionManager.deletePlatformInfo(positionId, dto.getId()); } } // ?? for (AdPlatformDTO dto : simples) { if (!dto.isChecked()) { positionManager.addPlatformInfo(positionId, ConfigCache.LIMIT_PARTNER_IDS.getValue(), dto.getId()); } } return list(); } /** * ??. * * @return * @throws WebException */ public String support() throws WebException { Long realPlatformId = Struts2Utils.getLong("id"); Long appId = Struts2Utils.getLong("appId"); try { realPlatformInfo = positionManager.findRealPlatformInfo(realPlatformId); App app = appManager.find(appId); List<Dictionary> channel_ids = dictionaryManager.find("channel_id"); List<Dictionary> version_ids = dictionaryManager.find("version_id", app.getName()); List<Dictionary> langs = dictionaryManager.find("lang"); List<Dictionary> group_ids = dictionaryManager.find("group_id"); List<Dictionary> apps = dictionaryManager.find("app"); List<Dictionary> countrys = dictionaryManager.find("country"); Struts2Utils.setAttribute("channel_ids", channel_ids); Struts2Utils.setAttribute("version_ids", version_ids); Struts2Utils.setAttribute("langs", langs); Struts2Utils.setAttribute("group_ids", group_ids); Struts2Utils.setAttribute("apps", apps); Struts2Utils.setAttribute("countrys", countrys); Struts2Utils.setAttribute("positionId", realPlatformInfo.getRel_ad_position_id()); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return "support"; } /** * ??. */ @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.POSITION_MODEL, Desc = "??.") public String modifySupport() { try { Long positionId = Struts2Utils.getLong("positionId"); if (null == positionId) { throw new WebException("???"); } position = positionManager.findPosition(positionId); if (null == position) { throw new WebException("???"); } if (null == realPlatformInfo || null == realPlatformInfo.getId()) { throw new WebException("???"); } RealPlatformInfo info = positionManager.findRealPlatformInfo(realPlatformInfo.getId()); info.setChannel_invert_typeEnum(realPlatformInfo.getChannel_invert_typeEnum()); info.setVersion_invert_typeEnum(realPlatformInfo.getVersion_invert_typeEnum()); info.setPhone_invert_typeEnum(realPlatformInfo.getPhone_invert_typeEnum()); info.setLang_invert_typeEnum(realPlatformInfo.getLang_invert_typeEnum()); info.setCountry_invert_typeEnum(realPlatformInfo.getCountry_invert_typeEnum()); info.setApp_invert_typeEnum(realPlatformInfo.getApp_invert_typeEnum()); info.setGroup_invert_typeEnum(realPlatformInfo.getGroup_invert_typeEnum()); if (StringUtils.isNotBlank(realPlatformInfo.getChannel_ids())) { info.setChannel_ids(realPlatformInfo.getChannel_ids().trim()); } else { info.setChannel_ids(StringUtils.EMPTY); } if (StringUtils.isNotBlank(realPlatformInfo.getVersion_ids())) { info.setVersion_ids(realPlatformInfo.getVersion_ids().trim()); } else { info.setVersion_ids(StringUtils.EMPTY); } if (StringUtils.isNotBlank(realPlatformInfo.getPhone_models())) { info.setPhone_models(realPlatformInfo.getPhone_models().trim()); } else { info.setPhone_models(StringUtils.EMPTY); } if (StringUtils.isNotBlank(realPlatformInfo.getLangs())) { info.setLangs(realPlatformInfo.getLangs().trim()); } else { info.setLangs(StringUtils.EMPTY); } if (StringUtils.isNotBlank(realPlatformInfo.getCountrys())) { info.setCountrys(realPlatformInfo.getCountrys().trim()); } else { info.setCountrys(StringUtils.EMPTY); } if (StringUtils.isNotBlank(realPlatformInfo.getApps())) { info.setApps(realPlatformInfo.getApps().trim()); } else { info.setApps(StringUtils.EMPTY); } if (StringUtils.isNotBlank(realPlatformInfo.getGroup_ids())) { info.setGroup_ids(realPlatformInfo.getGroup_ids().trim()); } else { info.setGroup_ids(StringUtils.EMPTY); } info.setAge_range(realPlatformInfo.getAge_range()); info.setGender(realPlatformInfo.getGender()); positionManager.updateRealPlatformInfo(info); } catch (WebException e) { addActionMessage(e.getMessage()); } return list(); } @OperatLog(Handle = SystemHandle.EDIT, Module = SystemModule.POSITION_MODEL, Desc = "modifyRealPlatformInfo") public String modifyRealPlatformInfo() { try { if (null == realPlatformInfoForm) { throw new WebException("???"); } position = positionManager.findPosition(realPlatformInfoForm.getPositionId()); position.setAd_push_typeEnum(realPlatformInfoForm.getPushType()); positionManager.update(position); if (null != realPlatformInfoForm.getRealInfo_id()) { for (int index = 0; index < realPlatformInfoForm.getRealInfo_id().length; index++) { RealPlatformInfo info = positionManager .findRealPlatformInfo(realPlatformInfoForm.getRealInfo_id()[index]); if (null == info) { continue; } Float delivery_probability = (null == realPlatformInfoForm.getProbability() ? null : realPlatformInfoForm.getProbability()[index] / 100); Integer max_per_day = (null == realPlatformInfoForm.getMax_per_day() ? null : realPlatformInfoForm.getMax_per_day()[index]); Integer priority = (null == realPlatformInfoForm.getPriority() ? null : realPlatformInfoForm.getPriority()[index]); Integer weights = (null == realPlatformInfoForm.getWeights() ? null : realPlatformInfoForm.getWeights()[index]); positionManager.updateRealPlatformInfo(info.getId(), realPlatformInfoForm.getStatus()[index], delivery_probability, max_per_day, priority, realPlatformInfoForm.getCheckFlag()[index], weights); } } } catch (WebException e) { addActionMessage(e.getMessage()); } catch (Exception e) { addActionError("??:" + e.getMessage()); LOGGER.error(e.getMessage(), e); } return list(); } /***************************************************************************/ public List<ShowType> getTypes() { return types; } public void setTypes(List<ShowType> types) { this.types = types; } public Position getPosition() { return position; } public void setPosition(Position position) { this.position = position; } public Status[] getStatus() { return status; } public void setStatus(Status[] status) { this.status = status; } public List<RealPlatformInfo> getInfos() { return infos; } public void setInfos(List<RealPlatformInfo> infos) { this.infos = infos; } public CheckFlag[] getFlags() { return flags; } public void setFlags(CheckFlag[] flags) { this.flags = flags; } public PushType[] getPushTypes() { return pushTypes; } public void setPushTypes(PushType[] pushTypes) { this.pushTypes = pushTypes; } public InvertType[] getInvertTypes() { return invertTypes; } public void setInvertTypes(InvertType[] invertTypes) { this.invertTypes = invertTypes; } public RealPlatformInfo getRealPlatformInfo() { return realPlatformInfo; } public void setRealPlatformInfo(RealPlatformInfo realPlatformInfo) { this.realPlatformInfo = realPlatformInfo; } public RealPlatformInfoForm getRealPlatformInfoForm() { return realPlatformInfoForm; } public Long[] getShowTypeIds() { return showTypeIds; } public void setShowTypeIds(Long[] showTypeIds) { this.showTypeIds = showTypeIds; } public void setRealPlatformInfoForm(RealPlatformInfoForm realPlatformInfoForm) { this.realPlatformInfoForm = realPlatformInfoForm; } public Week[] getWeeks() { return weeks; } public void setWeeks(Week[] weeks) { this.weeks = weeks; } public AgeRangeType[] getAgeRangeTypes() { return ageRangeTypes; } public void setAgeRangeTypes(AgeRangeType[] ageRangeTypes) { this.ageRangeTypes = ageRangeTypes; } public GenderType[] getGenderTypes() { return genderTypes; } public void setGenderTypes(GenderType[] genderTypes) { this.genderTypes = genderTypes; } }