Java Code Examples for net.sf.mpxj.ProjectCalendar#addCalendarHours()

The following examples show how to use net.sf.mpxj.ProjectCalendar#addCalendarHours() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrimaveraReader.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Process hours in a working day.
 *
 * @param calendar project calendar
 * @param dayRecord working day data
 */
private void processCalendarHours(ProjectCalendar calendar, Record dayRecord)
{
   // ... for each day of the week
   Day day = Day.getInstance(Integer.parseInt(dayRecord.getField()));
   // Get hours
   List<Record> recHours = dayRecord.getChildren();
   if (recHours.size() == 0)
   {
      // No data -> not working
      calendar.setWorkingDay(day, false);
   }
   else
   {
      calendar.setWorkingDay(day, true);
      // Read hours
      ProjectCalendarHours hours = calendar.addCalendarHours(day);
      for (Record recWorkingHours : recHours)
      {
         addHours(hours, recWorkingHours);
      }
   }
}
 
Example 2
Source File: MSPDIReader.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method extracts data for a normal working day from an MSPDI file.
 *
 * @param calendar Calendar data
 * @param weekDay Day data
 */
private void readNormalDay(ProjectCalendar calendar, Project.Calendars.Calendar.WeekDays.WeekDay weekDay)
{
   int dayNumber = weekDay.getDayType().intValue();
   Day day = Day.getInstance(dayNumber);
   calendar.setWorkingDay(day, BooleanHelper.getBoolean(weekDay.isDayWorking()));
   ProjectCalendarHours hours = calendar.addCalendarHours(day);

   Project.Calendars.Calendar.WeekDays.WeekDay.WorkingTimes times = weekDay.getWorkingTimes();
   if (times != null)
   {
      for (Project.Calendars.Calendar.WeekDays.WeekDay.WorkingTimes.WorkingTime period : times.getWorkingTime())
      {
         Date startTime = period.getFromTime();
         Date endTime = period.getToTime();

         if (startTime != null && endTime != null)
         {
            if (startTime.getTime() >= endTime.getTime())
            {
               endTime = DateHelper.addDays(endTime, 1);
            }

            hours.addRange(new DateRange(startTime, endTime));
         }
      }
   }
}
 
Example 3
Source File: ConceptDrawProjectReader.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Reads a single day for a calendar.
 *
 * @param mpxjCalendar ProjectCalendar instance
 * @param day ConceptDraw PROJECT week day
 */
private void readWeekDay(ProjectCalendar mpxjCalendar, WeekDay day)
{
   if (day.isIsDayWorking())
   {
      ProjectCalendarHours hours = mpxjCalendar.addCalendarHours(day.getDay());
      for (Document.Calendars.Calendar.WeekDays.WeekDay.TimePeriods.TimePeriod period : day.getTimePeriods().getTimePeriod())
      {
         hours.addRange(new DateRange(period.getFrom(), period.getTo()));
      }
   }
}
 
Example 4
Source File: GanttProjectReader.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Add working days and working time to a calendar.
 *
 * @param mpxjCalendar MPXJ calendar
 * @param gpCalendar GanttProject calendar
 */
private void setWorkingDays(ProjectCalendar mpxjCalendar, Calendars gpCalendar)
{
   DayTypes dayTypes = gpCalendar.getDayTypes();
   DefaultWeek defaultWeek = dayTypes.getDefaultWeek();
   if (defaultWeek == null)
   {
      mpxjCalendar.setWorkingDay(Day.SUNDAY, false);
      mpxjCalendar.setWorkingDay(Day.MONDAY, true);
      mpxjCalendar.setWorkingDay(Day.TUESDAY, true);
      mpxjCalendar.setWorkingDay(Day.WEDNESDAY, true);
      mpxjCalendar.setWorkingDay(Day.THURSDAY, true);
      mpxjCalendar.setWorkingDay(Day.FRIDAY, true);
      mpxjCalendar.setWorkingDay(Day.SATURDAY, false);
   }
   else
   {
      mpxjCalendar.setWorkingDay(Day.MONDAY, isWorkingDay(defaultWeek.getMon()));
      mpxjCalendar.setWorkingDay(Day.TUESDAY, isWorkingDay(defaultWeek.getTue()));
      mpxjCalendar.setWorkingDay(Day.WEDNESDAY, isWorkingDay(defaultWeek.getWed()));
      mpxjCalendar.setWorkingDay(Day.THURSDAY, isWorkingDay(defaultWeek.getThu()));
      mpxjCalendar.setWorkingDay(Day.FRIDAY, isWorkingDay(defaultWeek.getFri()));
      mpxjCalendar.setWorkingDay(Day.SATURDAY, isWorkingDay(defaultWeek.getSat()));
      mpxjCalendar.setWorkingDay(Day.SUNDAY, isWorkingDay(defaultWeek.getSun()));
   }

   for (Day day : Day.values())
   {
      if (mpxjCalendar.isWorkingDay(day))
      {
         ProjectCalendarHours hours = mpxjCalendar.addCalendarHours(day);
         hours.addRange(ProjectCalendarWeek.DEFAULT_WORKING_MORNING);
         hours.addRange(ProjectCalendarWeek.DEFAULT_WORKING_AFTERNOON);
      }
   }
}
 
Example 5
Source File: GanttDesignerReader.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Read the calendar data from a Gantt Designer file.
 *
 * @param gantt Gantt Designer file.
 */
private void readCalendar(Gantt gantt)
{
   Gantt.Calendar ganttCalendar = gantt.getCalendar();
   m_projectFile.getProjectProperties().setWeekStartDay(ganttCalendar.getWeekStart());

   ProjectCalendar calendar = m_projectFile.addCalendar();
   calendar.setName("Standard");
   m_projectFile.setDefaultCalendar(calendar);

   String workingDays = ganttCalendar.getWorkDays();
   calendar.setWorkingDay(Day.SUNDAY, workingDays.charAt(0) == '1');
   calendar.setWorkingDay(Day.MONDAY, workingDays.charAt(1) == '1');
   calendar.setWorkingDay(Day.TUESDAY, workingDays.charAt(2) == '1');
   calendar.setWorkingDay(Day.WEDNESDAY, workingDays.charAt(3) == '1');
   calendar.setWorkingDay(Day.THURSDAY, workingDays.charAt(4) == '1');
   calendar.setWorkingDay(Day.FRIDAY, workingDays.charAt(5) == '1');
   calendar.setWorkingDay(Day.SATURDAY, workingDays.charAt(6) == '1');

   for (int i = 1; i <= 7; i++)
   {
      Day day = Day.getInstance(i);
      ProjectCalendarHours hours = calendar.addCalendarHours(day);
      if (calendar.isWorkingDay(day))
      {
         hours.addRange(ProjectCalendarWeek.DEFAULT_WORKING_MORNING);
         hours.addRange(ProjectCalendarWeek.DEFAULT_WORKING_AFTERNOON);
      }
   }

   for (Gantt.Holidays.Holiday holiday : gantt.getHolidays().getHoliday())
   {
      ProjectCalendarException exception = calendar.addCalendarException(holiday.getDate(), holiday.getDate());
      exception.setName(holiday.getContent());
   }
}
 
Example 6
Source File: MPD9AbstractReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Process calendar hours.
 *
 * @param calendar parent calendar
 * @param row calendar hours data
 * @param dayIndex day index
 */
private void processCalendarHours(ProjectCalendar calendar, Row row, int dayIndex)
{
   Day day = Day.getInstance(dayIndex);
   boolean working = row.getInt("CD_WORKING") != 0;
   calendar.setWorkingDay(day, working);
   if (working == true)
   {
      ProjectCalendarHours hours = calendar.addCalendarHours(day);

      Date start = row.getDate("CD_FROM_TIME1");
      Date end = row.getDate("CD_TO_TIME1");
      if (start != null && end != null)
      {
         hours.addRange(new DateRange(start, end));
      }

      start = row.getDate("CD_FROM_TIME2");
      end = row.getDate("CD_TO_TIME2");
      if (start != null && end != null)
      {
         hours.addRange(new DateRange(start, end));
      }

      start = row.getDate("CD_FROM_TIME3");
      end = row.getDate("CD_TO_TIME3");
      if (start != null && end != null)
      {
         hours.addRange(new DateRange(start, end));
      }

      start = row.getDate("CD_FROM_TIME4");
      end = row.getDate("CD_TO_TIME4");
      if (start != null && end != null)
      {
         hours.addRange(new DateRange(start, end));
      }

      start = row.getDate("CD_FROM_TIME5");
      end = row.getDate("CD_TO_TIME5");
      if (start != null && end != null)
      {
         hours.addRange(new DateRange(start, end));
      }
   }
}
 
Example 7
Source File: PrimaveraReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Process data for an individual calendar.
 *
 * @param row calendar data
 * @return ProjectCalendar instance
 */
public ProjectCalendar processCalendar(Row row)
{
   ProjectCalendar calendar = m_project.addCalendar();

   Integer id = row.getInteger("clndr_id");
   calendar.setUniqueID(id);
   calendar.setName(row.getString("clndr_name"));

   try
   {
      calendar.setMinutesPerDay(Integer.valueOf((int) NumberHelper.getDouble(row.getDouble("day_hr_cnt")) * 60));
      calendar.setMinutesPerWeek(Integer.valueOf((int) (NumberHelper.getDouble(row.getDouble("week_hr_cnt")) * 60)));
      calendar.setMinutesPerMonth(Integer.valueOf((int) (NumberHelper.getDouble(row.getDouble("month_hr_cnt")) * 60)));
      calendar.setMinutesPerYear(Integer.valueOf((int) (NumberHelper.getDouble(row.getDouble("year_hr_cnt")) * 60)));
   }
   catch (ClassCastException ex)
   {
      // We have seen examples of malformed calendar data where fields have been missing
      // from the record. We'll typically get a class cast exception here as we're trying
      // to process something which isn't a double.
      // We'll just return at this point as it's not clear that we can salvage anything
      // sensible from this record.
      return calendar;
   }

   // Process data
   String calendarData = row.getString("clndr_data");
   if (calendarData != null && !calendarData.isEmpty())
   {
      Record root = Record.getRecord(calendarData);
      if (root != null)
      {
         processCalendarDays(calendar, root);
         processCalendarExceptions(calendar, root);
      }
   }
   else
   {
      // if there is not DaysOfWeek data, Primavera seems to default to Mon-Fri, 8:00-16:00
      DateRange defaultHourRange = new DateRange(DateHelper.getTime(8, 0), DateHelper.getTime(16, 0));
      for (Day day : Day.values())
      {
         if (day != Day.SATURDAY && day != Day.SUNDAY)
         {
            calendar.setWorkingDay(day, true);
            ProjectCalendarHours hours = calendar.addCalendarHours(day);
            hours.addRange(defaultHourRange);
         }
         else
         {
            calendar.setWorkingDay(day, false);
         }
      }
   }

   m_eventManager.fireCalendarReadEvent(calendar);

   return calendar;
}
 
Example 8
Source File: SureTrakDatabaseReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Reads the integer representation of calendar hours for a given
 * day and populates the calendar.
 *
 * @param calendar parent calendar
 * @param day target day
 * @param hours working hours
 */
private void readHours(ProjectCalendar calendar, Day day, Integer hours)
{
   int value = hours.intValue();
   int startHour = 0;
   ProjectCalendarHours calendarHours = null;

   Calendar cal = DateHelper.popCalendar();
   cal.set(Calendar.HOUR_OF_DAY, 0);
   cal.set(Calendar.MINUTE, 0);
   cal.set(Calendar.SECOND, 0);
   cal.set(Calendar.MILLISECOND, 0);

   calendar.setWorkingDay(day, false);

   while (value != 0)
   {
      // Move forward until we find a working hour
      while (startHour < 24 && (value & 0x1) == 0)
      {
         value = value >> 1;
         ++startHour;
      }

      // No more working hours, bail out
      if (startHour >= 24)
      {
         break;
      }

      // Move forward until we find the end of the working hours
      int endHour = startHour;
      while (endHour < 24 && (value & 0x1) != 0)
      {
         value = value >> 1;
         ++endHour;
      }

      cal.set(Calendar.HOUR_OF_DAY, startHour);
      Date startDate = cal.getTime();
      cal.set(Calendar.HOUR_OF_DAY, endHour);
      Date endDate = cal.getTime();

      if (calendarHours == null)
      {
         calendarHours = calendar.addCalendarHours(day);
         calendar.setWorkingDay(day, true);
      }
      calendarHours.addRange(new DateRange(startDate, endDate));
      startHour = endHour;
   }

   DateHelper.pushCalendar(cal);
}
 
Example 9
Source File: ProjectCommanderReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Read an individual calendar from the file.
 *
 * @param block CCalendar block
 * @return ProjectCalendar instance
 */
private ProjectCalendar readCalendar(Block block)
{
   ProjectCalendar calendar;

   byte[] data = block.getData();
   String name = DatatypeConverter.getString(data, 0, null);

   if (name == null || name.trim().isEmpty())
   {
      calendar = null;
   }
   else
   {
      int offset = 1 + name.length();

      calendar = m_projectFile.addCalendar();
      calendar.setName(name);

      // This is guesswork - need some samples with more variation
      int workingDays = DatatypeConverter.getByte(data, offset);
      calendar.setWorkingDay(Day.SATURDAY, (workingDays & 0x40) != 0);
      calendar.setWorkingDay(Day.SUNDAY, (workingDays & 0x20) != 0);
      calendar.setWorkingDay(Day.MONDAY, (workingDays & 0x10) != 0);
      calendar.setWorkingDay(Day.TUESDAY, (workingDays & 0x08) != 0);
      calendar.setWorkingDay(Day.WEDNESDAY, (workingDays & 0x04) != 0);
      calendar.setWorkingDay(Day.THURSDAY, (workingDays & 0x02) != 0);
      calendar.setWorkingDay(Day.FRIDAY, (workingDays & 0x01) != 0);
      offset += 28;

      Map<Day, List<DateRange>> ranges = new HashMap<>();
      ranges.put(Day.SATURDAY, readCalendarHours(data, offset + 0));
      ranges.put(Day.SUNDAY, readCalendarHours(data, offset + 16));
      ranges.put(Day.MONDAY, readCalendarHours(data, offset + 32));
      ranges.put(Day.TUESDAY, readCalendarHours(data, offset + 48));
      ranges.put(Day.WEDNESDAY, readCalendarHours(data, offset + 64));
      ranges.put(Day.THURSDAY, readCalendarHours(data, offset + 80));
      ranges.put(Day.FRIDAY, readCalendarHours(data, offset + 96));

      for (Day day : DAYS)
      {
         if (calendar.isWorkingDay(day))
         {
            ProjectCalendarHours hours = calendar.addCalendarHours(day);
            ranges.get(day).stream().forEach(range -> hours.addRange(range));
         }
      }

      block.getChildBlocks().stream().filter(x -> "CDayFlag".equals(x.getName())).forEach(x -> readCalendarException(calendar, ranges, x.getData()));

      m_eventManager.fireCalendarReadEvent(calendar);
   }

   return calendar;
}