📄 mschedule.java
字号:
/**
* (AB)() -> (B)(A) -> (B)(A)
* (BC)() -> (BC)() -> (B)(C)
* - if the row above is empty, move the first one right
* - else - check col_1..x above and move any content if the same
* - if size > 0
* - if the element is is not the same as above,
* move to the first empty column on the right
*/
// if in one column cell, there is more than one, move it to the right
for (int y = 0; y < maxYslots; y++)
{
// if an element is the same as the line above, move it there
if (y > 0 && matrix[0][y].size() > 0)
{
for (int x = 1; x < maxXslots; x++)
{
if (matrix[x][y-1].size() > 0) // above slot is not empty
{
Object above = matrix[x][y-1].get(0);
for (int i = 0; i < matrix[x][y].size(); i++)
{
if (above.equals(matrix[0][y].get(i))) // same - move it
{
matrix[x][y].add(matrix[0][y].get(i));
matrix[0][y].remove(i--);
}
}
}
}
} // if an element is the same as the line above, move it there
// we need to move items to the right
if (matrix[0][y].size() > 1)
{
Object above = null;
if (y > 0 && matrix[0][y-1].size() > 0)
above = matrix[0][y-1].get(0);
//
for (int i = 0; matrix[0][y].size() > 1; i++)
{
Object move = matrix[0][y].get(i);
if (!move.equals(above)) // we can move it
{
for (int x = 1; move != null && x < maxXslots; x++)
{
if (matrix[x][y].size() == 0) // found an empty slot
{
matrix[x][y].add(move);
matrix[0][y].remove(i--);
move = null;
}
}
}
}
} // we need to move items to the right
} // for all y slots
// go through the matrix and assign the X position
for (int y = 0; y < maxYslots; y++)
{
for (int x = 0; x < maxXslots; x++)
{
if (matrix[x][y].size() > 0)
{
MAssignmentSlot mas = (MAssignmentSlot)matrix[x][y].get(0);
mas.setX(x, xSlots[y]);
}
}
}
// clean up
matrix = null;
} // layoutSlots
/**
* Layout Y axis
* @param mas assignment slot
*/
private void layoutY (MAssignmentSlot mas)
{
int timeSlotStart = getTimeSlotIndex(mas.getStartTime(), false);
int timeSlotEnd = getTimeSlotIndex(mas.getEndTime(), true);
if (TimeUtil.isAllDay(mas.getStartTime(), mas.getEndTime()))
timeSlotEnd = m_timeSlots.length - 1;
//
mas.setY (timeSlotStart, timeSlotEnd);
} // layoutY
/**
* Return the Time Slot index for the time.
* Based on start time and not including end time
* @param time time (day is ignored)
* @param endTime if true, the end time is included
* @return slot index
*/
private int getTimeSlotIndex (Timestamp time, boolean endTime)
{
// Just one slot
if (m_timeSlots.length <= 1)
return 0;
// search for it
for (int i = 0; i < m_timeSlots.length; i++)
{
if (m_timeSlots[i].inSlot (time, endTime))
return i;
}
Log.error("MSchedule.getTimeSlotIndex - did not find Slot for " + time + " end=" + endTime);
return 0;
} // getTimeSlotIndex
/**
* Get Basic Info
* @param S_Resource_ID resource
*/
private void getBaseInfo (int S_Resource_ID)
{
// Resource is Active and Available
String sql = Access.addROAccessSQL (m_ctx,
"SELECT r.IsActive,r.IsAvailable,null," // r.IsSingleAssignment,"
+ "r.S_ResourceType_ID,rt.C_UOM_ID "
+ "FROM S_Resource r, S_ResourceType rt "
+ "WHERE r.S_Resource_ID=?"
+ " AND r.S_ResourceType_ID=rt.S_ResourceType_ID",
"r", true);
//
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, S_Resource_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
if (!"Y".equals(rs.getString(1))) // Active
m_isAvailable = false;
if (m_isAvailable && !"Y".equals(rs.getString(2))) // Available
m_isAvailable = false;
m_isSingleAssignment = "Y".equals(rs.getString(3));
//
m_S_ResourceType_ID = rs.getInt(4);
m_C_UOM_ID = rs.getInt(5);
// Log.trace(Log.l6_Database, "- Resource_ID=" + m_S_ResourceType_ID + ",IsAvailable=" + m_isAvailable);
}
else
m_isAvailable = false;
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("MSchedule.getBaseInfo", e);
m_isAvailable = false;
}
m_S_Resource_ID = S_Resource_ID;
} // getBaseInfo
/**
* Create Unavailable Timeslots.
* For every day from startDay..endDay create unavailable slots
* for 00:00..startTime and endTime..24:00
* @param list list to add time slots to
* @param startTime start time in day
* @param endTime end time in day
*/
private void createTimeSlot (ArrayList list,
Timestamp startTime, Timestamp endTime)
{
// Log.trace(Log.l5_DData, "MSchedule.createTimeSlot");
GregorianCalendar cal = new GregorianCalendar(Language.getLanguage().getLocale());
cal.setTimeInMillis(m_startDate.getTime());
// End Date for Comparison
GregorianCalendar calEnd = new GregorianCalendar(Language.getLanguage().getLocale());
calEnd.setTimeInMillis(m_endDate.getTime());
while (cal.before(calEnd))
{
// 00:00..startTime
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Timestamp start = new Timestamp (cal.getTimeInMillis());
//
GregorianCalendar cal_1 = new GregorianCalendar(Language.getLanguage().getLocale());
cal_1.setTimeInMillis(startTime.getTime());
cal.set(Calendar.HOUR_OF_DAY, cal_1.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal_1.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal_1.get(Calendar.SECOND));
Timestamp end = new Timestamp (cal.getTimeInMillis());
//
MAssignmentSlot ma = new MAssignmentSlot (start, end,
Msg.getMsg(m_ctx, "ResourceNotInSlotTime"), "",
MAssignmentSlot.STATUS_NotInSlotTime);
list.add(ma);
// endTime .. 00:00 next day
cal_1.setTimeInMillis(endTime.getTime());
cal.set(Calendar.HOUR_OF_DAY, cal_1.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal_1.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal_1.get(Calendar.SECOND));
start = new Timestamp (cal.getTimeInMillis());
//
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.add(Calendar.DAY_OF_YEAR, 1);
end = new Timestamp (cal.getTimeInMillis());
//
ma = new MAssignmentSlot (start, end,
Msg.getMsg(m_ctx, "ResourceNotInSlotTime"), "",
MAssignmentSlot.STATUS_NotInSlotTime);
list.add(ma);
}
} // createTimeSlot
/**
* Create Unavailable Dayslots.
* For every day from startDay..endDay create unavailable slots
* @param list list to add Day slots to
* @param OnMonday true if OK to have appointments (i.e. blocked if false)
* @param OnTuesday true if OK
* @param OnWednesday true if OK
* @param OnThursday true if OK
* @param OnFriday true if OK
* @param OnSaturday true if OK
* @param OnSunday true if OK
*/
private void createDaySlot (ArrayList list,
boolean OnMonday, boolean OnTuesday, boolean OnWednesday,
boolean OnThursday, boolean OnFriday, boolean OnSaturday, boolean OnSunday)
{
// Log.trace(Log.l5_DData, "MSchedule.createDaySlot");
GregorianCalendar cal = new GregorianCalendar(Language.getLanguage().getLocale());
cal.setTimeInMillis(m_startDate.getTime());
// End Date for Comparison
GregorianCalendar calEnd = new GregorianCalendar(Language.getLanguage().getLocale());
calEnd.setTimeInMillis(m_endDate.getTime());
while (cal.before(calEnd))
{
int weekday = cal.get(Calendar.DAY_OF_WEEK);
if ((!OnSaturday && weekday == Calendar.SATURDAY)
|| (!OnSunday && weekday == Calendar.SUNDAY)
|| (!OnMonday && weekday == Calendar.MONDAY)
|| (!OnTuesday && weekday == Calendar.TUESDAY)
|| (!OnWednesday && weekday == Calendar.WEDNESDAY)
|| (!OnThursday && weekday == Calendar.THURSDAY)
|| (!OnFriday && weekday == Calendar.FRIDAY))
{
// 00:00..00:00 next day
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Timestamp start = new Timestamp (cal.getTimeInMillis());
cal.add(Calendar.DAY_OF_YEAR, 1);
Timestamp end = new Timestamp (cal.getTimeInMillis());
MAssignmentSlot ma = new MAssignmentSlot (start, end,
Msg.getMsg(m_ctx, "ResourceNotInSlotDay"), "",
MAssignmentSlot.STATUS_NotInSlotDay);
list.add(ma);
}
else // next day
cal.add(Calendar.DAY_OF_YEAR, 1);
}
} // createDaySlot
/**
* Create a day slot for range
* @param list list
* @param ma assignment
*/
private void createDaySlot (ArrayList list, MAssignmentSlot ma)
{
// Log.trace(Log.l5_DData, "MSchedule.createDaySlot", ma);
//
Timestamp start = ma.getStartTime();
GregorianCalendar calStart = new GregorianCalendar();
calStart.setTime(start);
calStart.set(Calendar.HOUR_OF_DAY, 0);
calStart.set(Calendar.MINUTE, 0);
calStart.set(Calendar.SECOND, 0);
calStart.set(Calendar.MILLISECOND, 0);
Timestamp end = ma.getEndTime();
GregorianCalendar calEnd = new GregorianCalendar();
calEnd.setTime(end);
calEnd.set(Calendar.HOUR_OF_DAY, 0);
calEnd.set(Calendar.MINUTE, 0);
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
//
while (calStart.before(calEnd))
{
Timestamp xStart = new Timestamp(calStart.getTimeInMillis());
calStart.add(Calendar.DAY_OF_YEAR, 1);
Timestamp xEnd = new Timestamp(calStart.getTimeInMillis());
MAssignmentSlot myMa = new MAssignmentSlot (xStart, xEnd,
ma.getName(), ma.getDescription(), ma.getStatus());
list.add(myMa);
}
} // createDaySlot
/*************************************************************************/
/**
* Get Day Time Slots for Date
* @return "heading" or null
*/
public MAssignmentSlot[] getDayTimeSlots ()
{
return m_timeSlots;
} // getDayTimeSlots
/**
* Create Time Slots
*/
private void createTimeSlots()
{
// development error
if (m_typeName == null)
throw new IllegalStateException("ResourceTyoeName not set");
ArrayList list = new ArrayList();
UOM uom = UOM.getUOM(m_ctx, m_C_UOM_ID);
int minutes = UOMConversion.convertToMinutes (m_ctx, m_C_UOM_ID, UOMConversion.ONE);
Log.trace(Log.l4_Data, "MSchedule.createDayTimeSlots", "Minutes=" + minutes);
//
if (minutes > 0 && minutes < 60*24)
{
// Set Start Time
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(m_startDate);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
// we have slots - create first
if (m_slotStartTime != null)
{
long start = cal.getTimeInMillis();
cal.setTime(TimeUtil.getDayTime(m_startDate, m_slotStartTime)); // set to start time
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
list.add(new MAssignmentSlot(start, cal.getTimeInMillis()));
}
// Set End Time
GregorianCalendar calEnd = new GregorianCalendar();
if (m_slotEndTime != null)
{
calEnd.setTime(TimeUtil.getDayTime(m_startDate, m_slotEndTime));
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
}
else // No Slot - all day
{
calEnd.setTime(m_startDate);
calEnd.set(Calendar.HOUR_OF_DAY, 0);
calEnd.set(Calendar.MINUTE, 0);
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
calEnd.add(Calendar.DAY_OF_YEAR, 1);
}
//System.out.println("Start=" + new Timestamp(cal.getTimeInMillis()));
//System.out.println("Endt=" + new Timestamp(calEnd.getTimeInMillis()));
// Set end Slot Time
GregorianCalendar calEndSlot = new GregorianCalendar();
calEndSlot.setTime(cal.getTime());
calEndSlot.add(Calendar.MINUTE, minutes);
while (cal.before(calEnd))
{
list.add(new MAssignmentSlot(cal.getTimeInMillis(), calEndSlot.getTimeInMillis()));
// Next Slot
cal.add(Calendar.MINUTE, minutes);
calEndSlot.add(Calendar.MINUTE, minutes);
}
// create last slot
calEndSlot.setTime(cal.getTime());
calEndSlot.set(Calendar.HOUR_OF_DAY, 0);
calEndSlot.set(Calendar.MINUTE, 0);
calEndSlot.set(Calendar.SECOND, 0);
calEndSlot.set(Calendar.MILLISECOND, 0);
calEndSlot.add(Calendar.DAY_OF_YEAR, 1); // 00:00 next day
list.add(new MAssignmentSlot(cal.getTimeInMillis(), calEndSlot.getTimeInMillis()));
}
else // Day, ....
{
list.add (new MAssignmentSlot(TimeUtil.getDay(m_startDate), TimeUtil.getNextDay(m_startDate)));
}
//
m_timeSlots = new MAssignmentSlot[list.size()];
list.toArray(m_timeSlots);
} // createTimeSlots
/*************************************************************************/
/**
* Get Resource ID. Set by getAssignmentSlots
* @return current resource
*/
public int getS_Resource_ID()
{
return m_S_Resource_ID;
} // getS_Resource_ID
/**
* Return Start Date. Set by getAssignmentSlots
* @return start date
*/
public Timestamp getStartDate ()
{
return m_startDate;
} // getStartDate
/**
* Return End Date. Set by getAssignmentSlots
* @return end date
*/
public Timestamp getEndDate ()
{
return m_endDate;
} // getEndDate
} // MSchedule
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -