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

The following examples show how to use net.sf.mpxj.ProjectCalendar#isWorkingDay() . 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: ProjectFileExporter.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
private void exportWeekends(ProjectCalendar calendar) {
  ProjectCalendarHours workingDayHours = calendar.getCalendarHours(Day.MONDAY);
  calendar.setWorkingDay(Day.MONDAY, isWorkingDay(Calendar.MONDAY));
  calendar.setWorkingDay(Day.TUESDAY, isWorkingDay(Calendar.TUESDAY));
  calendar.setWorkingDay(Day.WEDNESDAY, isWorkingDay(Calendar.WEDNESDAY));
  calendar.setWorkingDay(Day.THURSDAY, isWorkingDay(Calendar.THURSDAY));
  calendar.setWorkingDay(Day.FRIDAY, isWorkingDay(Calendar.FRIDAY));
  calendar.setWorkingDay(Day.SATURDAY, isWorkingDay(Calendar.SATURDAY));
  if (calendar.isWorkingDay(Day.SATURDAY)) {
    copyHours(workingDayHours, calendar.addCalendarHours(Day.SATURDAY));
  }
  calendar.setWorkingDay(Day.SUNDAY, isWorkingDay(Calendar.SUNDAY));
  if (calendar.isWorkingDay(Day.SUNDAY)) {
    copyHours(workingDayHours, calendar.addCalendarHours(Day.SUNDAY));
  }
}
 
Example 2
Source File: ProjectCommanderReader.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Read a calendar exception.
 *
 * @param calendar parent calendar
 * @param ranges default day of week working times
 * @param data byte array
 */
private void readCalendarException(ProjectCalendar calendar, Map<Day, List<DateRange>> ranges, byte[] data)
{
   long timestampInDays = DatatypeConverter.getShort(data, 2, 0);

   // Heuristic to filter out odd exception dates
   if (timestampInDays > 0xFF)
   {
      long timestampInMilliseconds = timestampInDays * 24 * 60 * 60 * 1000;
      Date exceptionDate = DateHelper.getTimestampFromLong(timestampInMilliseconds);

      Calendar cal = DateHelper.popCalendar();
      cal.setTime(exceptionDate);
      Day day = Day.getInstance(cal.get(Calendar.DAY_OF_WEEK));
      DateHelper.pushCalendar(cal);

      ProjectCalendarException ex = calendar.addCalendarException(exceptionDate, exceptionDate);
      if (!calendar.isWorkingDay(day))
      {
         ranges.get(day).stream().forEach(range -> ex.addRange(range));
      }
   }
}
 
Example 3
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 4
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 5
Source File: PlannerWriter.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * This method writes data for a single calendar to a Planner file.
 *
 * @param mpxjCalendar MPXJ calendar instance
 * @param plannerCalendar Planner calendar instance
 * @throws JAXBException on xml creation errors
 */
private void writeCalendar(ProjectCalendar mpxjCalendar, net.sf.mpxj.planner.schema.Calendar plannerCalendar) throws JAXBException
{
   //
   // Populate basic details
   //
   plannerCalendar.setId(getIntegerString(mpxjCalendar.getUniqueID()));
   plannerCalendar.setName(getString(mpxjCalendar.getName()));

   //
   // Set working and non working days
   //
   DefaultWeek dw = m_factory.createDefaultWeek();
   plannerCalendar.setDefaultWeek(dw);
   dw.setMon(getWorkingDayString(mpxjCalendar, Day.MONDAY));
   dw.setTue(getWorkingDayString(mpxjCalendar, Day.TUESDAY));
   dw.setWed(getWorkingDayString(mpxjCalendar, Day.WEDNESDAY));
   dw.setThu(getWorkingDayString(mpxjCalendar, Day.THURSDAY));
   dw.setFri(getWorkingDayString(mpxjCalendar, Day.FRIDAY));
   dw.setSat(getWorkingDayString(mpxjCalendar, Day.SATURDAY));
   dw.setSun(getWorkingDayString(mpxjCalendar, Day.SUNDAY));

   //
   // Set working hours
   //
   OverriddenDayTypes odt = m_factory.createOverriddenDayTypes();
   plannerCalendar.setOverriddenDayTypes(odt);
   List<OverriddenDayType> typeList = odt.getOverriddenDayType();
   Sequence uniqueID = new Sequence(0);

   //
   // This is a bit arbitrary, so not ideal, however...
   // The idea here is that MS Project allows us to specify working hours
   // for each day of the week individually. Planner doesn't do this,
   // but instead allows us to specify working hours for each day type.
   // What we are doing here is stepping through the days of the week to
   // find the first working day, then using the hours for that day
   // as the hours for the working day type in Planner.
   //
   for (int dayLoop = 1; dayLoop < 8; dayLoop++)
   {
      Day day = Day.getInstance(dayLoop);
      if (mpxjCalendar.isWorkingDay(day))
      {
         processWorkingHours(mpxjCalendar, uniqueID, day, typeList);
         break;
      }
   }

   //
   // Process exception days
   //
   Days plannerDays = m_factory.createDays();
   plannerCalendar.setDays(plannerDays);
   List<net.sf.mpxj.planner.schema.Day> dayList = plannerDays.getDay();
   processExceptionDays(mpxjCalendar, dayList);

   m_eventManager.fireCalendarWrittenEvent(mpxjCalendar);

   //
   // Process any derived calendars
   //
   List<net.sf.mpxj.planner.schema.Calendar> calendarList = plannerCalendar.getCalendar();

   for (ProjectCalendar mpxjDerivedCalendar : mpxjCalendar.getDerivedCalendars())
   {
      net.sf.mpxj.planner.schema.Calendar plannerDerivedCalendar = m_factory.createCalendar();
      calendarList.add(plannerDerivedCalendar);
      writeCalendar(mpxjDerivedCalendar, plannerDerivedCalendar);
   }
}
 
Example 6
Source File: SureTrakDatabaseReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Read project calendars.
 */
private void readCalendars()
{
   Table cal = m_tables.get("CAL");
   for (MapRow row : cal)
   {
      ProjectCalendar calendar = m_projectFile.addCalendar();
      m_calendarMap.put(row.getInteger("CALENDAR_ID"), calendar);
      Integer[] days =
      {
         row.getInteger("SUNDAY_HOURS"),
         row.getInteger("MONDAY_HOURS"),
         row.getInteger("TUESDAY_HOURS"),
         row.getInteger("WEDNESDAY_HOURS"),
         row.getInteger("THURSDAY_HOURS"),
         row.getInteger("FRIDAY_HOURS"),
         row.getInteger("SATURDAY_HOURS")
      };

      calendar.setName(row.getString("NAME"));
      readHours(calendar, Day.SUNDAY, days[0]);
      readHours(calendar, Day.MONDAY, days[1]);
      readHours(calendar, Day.TUESDAY, days[2]);
      readHours(calendar, Day.WEDNESDAY, days[3]);
      readHours(calendar, Day.THURSDAY, days[4]);
      readHours(calendar, Day.FRIDAY, days[5]);
      readHours(calendar, Day.SATURDAY, days[6]);

      int workingDaysPerWeek = 0;
      for (Day day : Day.values())
      {
         if (calendar.isWorkingDay(day))
         {
            ++workingDaysPerWeek;
         }
      }

      Integer workingHours = null;
      for (int index = 0; index < 7; index++)
      {
         if (days[index].intValue() != 0)
         {
            workingHours = days[index];
            break;
         }
      }

      if (workingHours != null)
      {
         int workingHoursPerDay = countHours(workingHours);
         int minutesPerDay = workingHoursPerDay * 60;
         int minutesPerWeek = minutesPerDay * workingDaysPerWeek;
         int minutesPerMonth = 4 * minutesPerWeek;
         int minutesPerYear = 52 * minutesPerWeek;

         calendar.setMinutesPerDay(Integer.valueOf(minutesPerDay));
         calendar.setMinutesPerWeek(Integer.valueOf(minutesPerWeek));
         calendar.setMinutesPerMonth(Integer.valueOf(minutesPerMonth));
         calendar.setMinutesPerYear(Integer.valueOf(minutesPerYear));
      }
   }
}
 
Example 7
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;
}