ObjectClassFormController.java 8.0 KB
Newer Older
H
hot13399 已提交
1
package com.spring.mvc.mini.web;
H
hot13399 已提交
2 3 4 5 6 7 8 9 10 11

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import javax.validation.Valid;

H
hot13399 已提交
12
import com.spring.mvc.mini.pojo.*;
H
hot13399 已提交
13 14 15
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
H
hot13399 已提交
16
import org.springframework.beans.factory.annotation.Value;
H
hot13399 已提交
17 18 19 20 21 22 23 24 25 26 27
import org.springframework.mvc.extensions.ajax.AjaxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

28
import com.spring.mvc.mini.json.RequestStatusJsonParser;
H
hot13399 已提交
29
import com.spring.mvc.mini.mail.MailSender;
H
hot13399 已提交
30
import com.spring.mvc.mini.pojo.RequestStatus;
H
hot13399 已提交
31
import com.spring.mvc.mini.validation.ObjectClassDataValidator;
H
hot13399 已提交
32 33 34

@Controller
@RequestMapping("/objectclassform")
H
hot13399 已提交
35
@SessionAttributes({"ojbclslisttype", "userinfo", "mocrid"})
H
hot13399 已提交
36
public class ObjectClassFormController {
H
hot13399 已提交
37

H
hot13399 已提交
38
    private static final Logger LOG = LoggerFactory.getLogger(ObjectClassFormController.class);
39
    private static final String MESSAGE = "message";
H
hot13399 已提交
40

H
hot13399 已提交
41 42 43 44 45 46
    @Value("${mail.to}")
    private String mailto;

    @Value("${web.hostname}")
    private String webHostname;

H
hot13399 已提交
47
    @Autowired
H
hot13399 已提交
48
    private RequestStatusJsonParser requestStatusJsonParser;
H
hot13399 已提交
49 50

    @Autowired
H
hot13399 已提交
51
    private ObjectClassDataValidator objectClassDataValidator;
H
hot13399 已提交
52 53

    @Autowired
H
hot13399 已提交
54
    private MailSender mailSender;
H
hot13399 已提交
55 56 57 58 59 60 61 62 63 64 65 66

    @ModelAttribute
    public void ajaxAttribute(WebRequest request, Model model) {
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
    }

    @ModelAttribute("userinfo")
    public UserInfo createUserinfo() {
        return new UserInfo();
    }

    @ModelAttribute("mocrid")
H
hot13399 已提交
67
    public String createMocrid() {
H
hot13399 已提交
68 69 70 71
        return String.valueOf(1 + requestStatusJsonParser.getLatestmocrid());
    }

    @RequestMapping(method = RequestMethod.GET)
H
hot13399 已提交
72 73
    public void handleObjectClassForm(Model model, @ModelAttribute("userinfo") UserInfo userinfo, @ModelAttribute("mocrid") String mocrid) {
        ObjectClassesType objectClassesType = new ObjectClassesType();
H
hot13399 已提交
74 75 76 77

        ArrayList<ObjectClass> objectClasses = new ArrayList<ObjectClass>();
        objectClasses.add(createObjectClassInstance(0, userinfo, mocrid));

H
hot13399 已提交
78
        objectClassesType.setObjectClasses(objectClasses);
H
hot13399 已提交
79

H
hot13399 已提交
80
        model.addAttribute("objectClassesType", objectClassesType);
H
hot13399 已提交
81 82 83 84 85
    }


    //Add More
    @RequestMapping(params = {"objclscount"}, method = RequestMethod.POST)
H
hot13399 已提交
86
    public void handleObjectClassFormWithParam(@Valid ObjectClassesType objectClassesType, @ModelAttribute("userinfo") UserInfo userinfo, @ModelAttribute("mocrid") String mocrid, @RequestParam String ObjectClassCount, Model model) {
H
hot13399 已提交
87

H
hot13399 已提交
88
        ArrayList<ObjectClass> objectClasses = objectClassesType.getObjectClasses();
H
hot13399 已提交
89
        objectClasses.add(createObjectClassInstance(Integer.parseInt(ObjectClassCount), userinfo, mocrid));
H
hot13399 已提交
90 91
        objectClassesType.setObjectClasses(objectClasses);
        model.addAttribute("ObjectClassesType", objectClassesType);
H
hot13399 已提交
92 93 94 95
    }

    //Submit
    @RequestMapping(method = RequestMethod.POST)
H
hot13399 已提交
96
    public String handleSubmit(ObjectClassesType objectClassesType, @ModelAttribute("userinfo") UserInfo userinfo,
H
hot13399 已提交
97 98 99 100 101
                               @ModelAttribute("mocrid") String mocrid,
                               @ModelAttribute("ajaxRequest") boolean ajaxRequest,
                               Model model, RedirectAttributes redirectAttrs) {

        if (userinfo.getUsername() == null) {
102
            model.addAttribute(MESSAGE, "Sorry, Please submit UserInfoForm at first. ");
H
hot13399 已提交
103 104 105
            return null;
        }

H
hot13399 已提交
106
        ArrayList<ObjectClass> objectClasses = objectClassesType.getObjectClasses();
H
hot13399 已提交
107 108 109 110 111 112

        try {
            objectClassDataValidator.checkData(objectClasses);
            objectClassDataValidator.checkmocrid(mocrid, requestStatusJsonParser.getLatestmocrid());

        } catch (Exception e) {
113
            model.addAttribute(MESSAGE, e.toString());
H
hot13399 已提交
114 115 116 117 118 119 120 121 122 123
            return null;
        }

        constructDebugMessage(objectClasses);

        RequestStatus s = new RequestStatus();
        Calendar cal = Calendar.getInstance();

        s.setSubmitDate(cal.getTime());
        s.setmocrid(Integer.parseInt(mocrid));
H
hot13399 已提交
124
        s.setObjectClassesType(objectClassesType);
H
hot13399 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
        s.setStatus(StatusType.ongoing);
        s.setUserinfo(userinfo);

        ArrayList<RequestStatus> l = requestStatusJsonParser.readStatus();
        l.add(s);

        RequestStatusListType type = new RequestStatusListType();
        type.setRequestStatuses(l);
        requestStatusJsonParser.writeStatus(type);

        try {
            this.commitAndSendMail(userinfo, constructMailSubject(mocrid, objectClasses), constructMailText(userinfo, mocrid));
        } catch (Exception e) {

139
            model.addAttribute(MESSAGE, e.toString());
H
hot13399 已提交
140 141 142 143 144 145
            return null;
        }

        String message = "MO CR:" + mocrid + " committed. System will send mail to reviewers.";

        if (ajaxRequest) {
146
            model.addAttribute(MESSAGE, message);
H
hot13399 已提交
147 148
            return null;
        } else {
149
            redirectAttrs.addFlashAttribute(MESSAGE, message);
H
hot13399 已提交
150 151 152 153 154 155 156 157
            return "redirect:/objectclassform";
        }
    }

    private String constructMailText(@ModelAttribute("userinfo") UserInfo userinfo, @ModelAttribute("mocrid") String mocrid) {
        StringBuffer textsb = new StringBuffer();
        textsb.append("Hi,\r\n\r\nThese MO CR's shall be approved if no other comments.\r\nIf you have any comment, please comment on this page:\r\n");
        textsb.append("http://");
H
hot13399 已提交
158
        textsb.append(webHostname);
H
hot13399 已提交
159 160 161 162 163 164 165
        textsb.append(":8080/spring-mvc-mini/requeststatus?mocrid=");
        textsb.append(mocrid);
        textsb.append("\r\nor mailto:");
        textsb.append(userinfo.getEmail());
        textsb.append("\r\n");
        textsb.append("The request will be committed in 5 days.\r\n\r\nThanks.\r\n");
        textsb.append("http://");
H
hot13399 已提交
166
        textsb.append(webHostname);
H
hot13399 已提交
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
        textsb.append(":8080/spring-mvc-mini/");
        return textsb.toString();
    }

    private String constructMailSubject(@ModelAttribute("mocrid") String mocrid, ArrayList<ObjectClass> objectClasses) {
        StringBuffer subjectsb = new StringBuffer();
        subjectsb.append("Forthcoming approval of MO CR ");
        subjectsb.append(mocrid);
        subjectsb.append(" for ");
        for (ObjectClass objcls : objectClasses) {
            subjectsb.append(objcls.getAbbreviation());
        }
        return subjectsb.toString();
    }

    private void constructDebugMessage(ArrayList<ObjectClass> objectClasses) {
        StringBuffer debugmessage = new StringBuffer();
        for (ObjectClass objcls : objectClasses) {
            debugmessage.append(objcls);
        }

        LOG.debug(debugmessage.toString());
    }

    private ObjectClass createObjectClassInstance(int i, UserInfo userinfo, String mocrid) {
        ObjectClass objcls = new ObjectClass();
        objcls.setIntclass(requestStatusJsonParser.getMaxIntClass() + 1 + i);

        Calendar cal = Calendar.getInstance();
        Date currenttime = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY.MM.DD");

        objcls.setComment("# " + userinfo.getEmail() + " " + sdf.format(currenttime) + " MO CR " + mocrid);
        return objcls;
    }

    public void commitAndSendMail(UserInfo userinfo, String subject, String text) throws Exception {

H
hot13399 已提交
205
        Address[] toAddress = {new InternetAddress(mailto), new InternetAddress(userinfo.getEmail())};
H
hot13399 已提交
206 207 208 209

        mailSender.sendMail(userinfo.getUsername(), userinfo.getPassword(), userinfo.getEmail(), toAddress, subject, text);
    }

H
hot13399 已提交
210
}