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());
}
Aggregations