Search in sources :

Example 1 with ScheduleMaker

use of jp.ossc.nimbus.service.scheduler2.ScheduleMaker in project nimbus by nimbus-org.

the class ScheduleManagerServlet method processIsMakeScheduleResponse.

/**
 * スケジュール作成判定リクエスト処理を行う。<p>
 *
 * @param req HTTPリクエスト
 * @param resp HTTPレスポンス
 * @param responseType レスポンス種別
 * @exception ServletException
 * @exception IOException
 */
protected void processIsMakeScheduleResponse(HttpServletRequest req, HttpServletResponse resp, String responseType) throws ServletException, IOException {
    String masterId = getParameter(req, "masterId");
    Date date = null;
    try {
        date = getDateParameter(req, "date", "yyyyMMdd", true);
    } catch (ParseException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'date' is illegal." + e.toString());
        return;
    }
    Date from = null;
    try {
        from = getDateParameter(req, "from", "yyyyMMdd", false);
    } catch (ParseException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'from' is illegal." + e.toString());
        return;
    }
    Date to = null;
    try {
        to = getDateParameter(req, "to", "yyyyMMdd", false);
    } catch (ParseException e) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'to' is illegal." + e.toString());
        return;
    }
    Exception exception = null;
    boolean isMake = false;
    Map masterIdDayMap = new TreeMap();
    try {
        if (masterId != null) {
            ScheduleMaster scheduleMaster = scheduleManager.findScheduleMaster(masterId);
            ScheduleMaker scheduleMaker = scheduleMaster != null ? scheduleManager.getScheduleMaker(scheduleMaster.getScheduleType()) : null;
            if (scheduleMaster == null) {
                exception = new Exception("scheduleMaster not found. masterId=" + masterId);
            } else {
                Map dayMap = new TreeMap();
                masterIdDayMap.put(scheduleMaster.getId(), dayMap);
                if (from == null && to == null) {
                    isMake = scheduleMaker.isMakeSchedule(date, scheduleMaster);
                    dayMap.put(date, isMake ? Boolean.TRUE : Boolean.FALSE);
                } else {
                    if (to == null) {
                        to = date;
                    } else if (from == null) {
                        from = date;
                    }
                    Calendar current = Calendar.getInstance();
                    current.setTime(from);
                    Calendar toCal = Calendar.getInstance();
                    toCal.setTime(to);
                    while (current.before(toCal) || current.equals(toCal)) {
                        Date today = current.getTime();
                        isMake = scheduleMaker.isMakeSchedule(today, scheduleMaster);
                        dayMap.put(today, isMake ? Boolean.TRUE : Boolean.FALSE);
                        current.add(Calendar.DAY_OF_YEAR, 1);
                    }
                }
            }
        } else {
            List scheduleMasters = scheduleManager.findAllScheduleMasters();
            if (from == null && to == null) {
                for (int i = 0; i < scheduleMasters.size(); i++) {
                    ScheduleMaster scheduleMaster = (ScheduleMaster) scheduleMasters.get(i);
                    ScheduleMaker scheduleMaker = scheduleManager.getScheduleMaker(scheduleMaster.getScheduleType());
                    isMake = scheduleMaker.isMakeSchedule(date, scheduleMaster);
                    Map map = (Map) masterIdDayMap.get(scheduleMaster.getId());
                    if (map == null) {
                        map = new TreeMap();
                        masterIdDayMap.put(scheduleMaster.getId(), map);
                    }
                    map.put(date, isMake ? Boolean.TRUE : Boolean.FALSE);
                }
            } else {
                if (to == null) {
                    to = date;
                } else if (from == null) {
                    from = date;
                }
                Calendar current = Calendar.getInstance();
                current.setTime(from);
                Calendar toCal = Calendar.getInstance();
                toCal.setTime(to);
                while (current.before(toCal) || current.equals(toCal)) {
                    Date today = current.getTime();
                    for (int i = 0; i < scheduleMasters.size(); i++) {
                        ScheduleMaster scheduleMaster = (ScheduleMaster) scheduleMasters.get(i);
                        ScheduleMaker scheduleMaker = scheduleManager.getScheduleMaker(scheduleMaster.getScheduleType());
                        isMake = scheduleMaker.isMakeSchedule(today, scheduleMaster);
                        Map map = (Map) masterIdDayMap.get(scheduleMaster.getId());
                        if (map == null) {
                            map = new TreeMap();
                            masterIdDayMap.put(scheduleMaster.getId(), map);
                        }
                        map.put(today, isMake ? Boolean.TRUE : Boolean.FALSE);
                    }
                    current.add(Calendar.DAY_OF_YEAR, 1);
                }
            }
        }
    } catch (ScheduleManageException e) {
        exception = e;
    }
    final StringBuilder buf = new StringBuilder();
    if ("json".equals(responseType)) {
        resp.setContentType("application/json;charset=UTF-8");
        Map jsonMap = new HashMap();
        if (exception == null) {
            List result = new ArrayList();
            Iterator entries = masterIdDayMap.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry entry = (Map.Entry) entries.next();
                Map resultMap = new HashMap(2);
                resultMap.put("masterId", entry.getKey());
                List resultList = new ArrayList();
                Iterator entries2 = ((Map) entry.getValue()).entrySet().iterator();
                while (entries2.hasNext()) {
                    Map.Entry entry2 = (Map.Entry) entries2.next();
                    Map resultMap2 = new HashMap(2);
                    resultMap2.put("date", entry2.getKey());
                    resultMap2.put("isMake", entry2.getValue());
                    resultList.add(resultMap2);
                }
                resultMap.put("result", resultList);
                result.add(resultMap);
            }
            jsonMap.put("result", result);
        } else {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            exception.printStackTrace(pw);
            pw.flush();
            jsonMap.put("exception", sw.toString());
        }
        buf.append(toStringConverter.convertToObject(jsonConverter.convertToStream(jsonMap)));
    } else {
        resp.setContentType("text/html;charset=UTF-8");
        if (exception != null) {
            buf.append(exception(exception));
        } else {
            buf.append("<html>");
            buf.append(header(HEADER_OTHER));
            buf.append("<body>");
            buf.append(scheduleMasterIsMakeCondition(getCurrentPath(req), masterId, from, to));
            buf.append(scheduleMasterIsMake(getCurrentPath(req), masterIdDayMap));
            buf.append("</body>");
            buf.append("</html>");
        }
    }
    resp.getWriter().println(buf.toString());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ScheduleManageException(jp.ossc.nimbus.service.scheduler2.ScheduleManageException) Date(java.util.Date) ServletException(javax.servlet.ServletException) ParseException(java.text.ParseException) ScheduleManageException(jp.ossc.nimbus.service.scheduler2.ScheduleManageException) IOException(java.io.IOException) ScheduleMaster(jp.ossc.nimbus.service.scheduler2.ScheduleMaster) StringWriter(java.io.StringWriter) ScheduleMaker(jp.ossc.nimbus.service.scheduler2.ScheduleMaker) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(java.text.ParseException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) PrintWriter(java.io.PrintWriter)

Aggregations

IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ServletException (javax.servlet.ServletException)1 ScheduleMaker (jp.ossc.nimbus.service.scheduler2.ScheduleMaker)1 ScheduleManageException (jp.ossc.nimbus.service.scheduler2.ScheduleManageException)1 ScheduleMaster (jp.ossc.nimbus.service.scheduler2.ScheduleMaster)1