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

The following examples show how to use net.sf.mpxj.ProjectCalendar#setWorkingDay() . 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: MPXReader.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Populates a calendar instance.
 *
 * @param record MPX record
 * @param calendar calendar instance
 * @param isBaseCalendar true if this is a base calendar
 */
private void populateCalendar(Record record, ProjectCalendar calendar, boolean isBaseCalendar)
{
   if (isBaseCalendar == true)
   {
      calendar.setName(record.getString(0));
   }
   else
   {
      calendar.setParent(m_projectFile.getCalendarByName(record.getString(0)));
   }

   calendar.setWorkingDay(Day.SUNDAY, record.getDayType(1));
   calendar.setWorkingDay(Day.MONDAY, record.getDayType(2));
   calendar.setWorkingDay(Day.TUESDAY, record.getDayType(3));
   calendar.setWorkingDay(Day.WEDNESDAY, record.getDayType(4));
   calendar.setWorkingDay(Day.THURSDAY, record.getDayType(5));
   calendar.setWorkingDay(Day.FRIDAY, record.getDayType(6));
   calendar.setWorkingDay(Day.SATURDAY, record.getDayType(7));

   m_eventManager.fireCalendarReadEvent(calendar);
}
 
Example 3
Source File: PlannerReader.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Set the working/non-working status of a weekday.
 *
 * @param mpxjCalendar MPXJ calendar
 * @param mpxjDay day of the week
 * @param plannerDay planner day type
 */
private void setWorkingDay(ProjectCalendar mpxjCalendar, Day mpxjDay, String plannerDay)
{
   DayType dayType = DayType.DEFAULT;

   if (plannerDay != null)
   {
      switch (getInt(plannerDay))
      {
         case 0:
         {
            dayType = DayType.WORKING;
            break;
         }

         case 1:
         {
            dayType = DayType.NON_WORKING;
            break;
         }
      }
   }

   mpxjCalendar.setWorkingDay(mpxjDay, dayType);
}
 
Example 4
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 5
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 single calendar from an MSPDI file.
 *
 * @param calendar Calendar data
 * @param map Map of calendar UIDs to names
 * @param baseCalendars list of base calendars
 */
private void readCalendar(Project.Calendars.Calendar calendar, HashMap<BigInteger, ProjectCalendar> map, List<Pair<ProjectCalendar, BigInteger>> baseCalendars)
{
   ProjectCalendar bc = m_projectFile.addCalendar();
   bc.setUniqueID(NumberHelper.getInteger(calendar.getUID()));
   bc.setName(calendar.getName());
   BigInteger baseCalendarID = calendar.getBaseCalendarUID();
   if (baseCalendarID != null)
   {
      baseCalendars.add(new Pair<>(bc, baseCalendarID));
   }

   readExceptions(calendar, bc);
   boolean readExceptionsFromDays = bc.getCalendarExceptions().isEmpty();

   Project.Calendars.Calendar.WeekDays days = calendar.getWeekDays();
   if (days != null)
   {
      for (Project.Calendars.Calendar.WeekDays.WeekDay weekDay : days.getWeekDay())
      {
         readDay(bc, weekDay, readExceptionsFromDays);
      }
   }
   else
   {
      bc.setWorkingDay(Day.SUNDAY, DayType.DEFAULT);
      bc.setWorkingDay(Day.MONDAY, DayType.DEFAULT);
      bc.setWorkingDay(Day.TUESDAY, DayType.DEFAULT);
      bc.setWorkingDay(Day.WEDNESDAY, DayType.DEFAULT);
      bc.setWorkingDay(Day.THURSDAY, DayType.DEFAULT);
      bc.setWorkingDay(Day.FRIDAY, DayType.DEFAULT);
      bc.setWorkingDay(Day.SATURDAY, DayType.DEFAULT);
   }

   readWorkWeeks(calendar, bc);

   map.put(calendar.getUID(), bc);

   m_eventManager.fireCalendarReadEvent(bc);
}
 
Example 6
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 7
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 8
Source File: PlannerReader.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method extracts data for a single resource from a Planner file.
 *
 * @param plannerResource Resource data
 */
private void readResource(net.sf.mpxj.planner.schema.Resource plannerResource) throws MPXJException
{
   Resource mpxjResource = m_projectFile.addResource();

   //mpxjResource.setResourceCalendar(m_projectFile.getBaseCalendarByUniqueID(getInteger(plannerResource.getCalendar())));
   mpxjResource.setEmailAddress(plannerResource.getEmail());
   mpxjResource.setUniqueID(getInteger(plannerResource.getId()));
   mpxjResource.setName(plannerResource.getName());
   mpxjResource.setNotes(plannerResource.getNote());
   mpxjResource.setInitials(plannerResource.getShortName());
   mpxjResource.setType(getInt(plannerResource.getType()) == 2 ? ResourceType.MATERIAL : ResourceType.WORK);
   //plannerResource.getStdRate();
   //plannerResource.getOvtRate();
   //plannerResource.getUnits();
   //plannerResource.getProperties();

   ProjectCalendar calendar = mpxjResource.addResourceCalendar();

   calendar.setWorkingDay(Day.SUNDAY, DayType.DEFAULT);
   calendar.setWorkingDay(Day.MONDAY, DayType.DEFAULT);
   calendar.setWorkingDay(Day.TUESDAY, DayType.DEFAULT);
   calendar.setWorkingDay(Day.WEDNESDAY, DayType.DEFAULT);
   calendar.setWorkingDay(Day.THURSDAY, DayType.DEFAULT);
   calendar.setWorkingDay(Day.FRIDAY, DayType.DEFAULT);
   calendar.setWorkingDay(Day.SATURDAY, DayType.DEFAULT);

   ProjectCalendar baseCalendar = m_projectFile.getCalendarByUniqueID(getInteger(plannerResource.getCalendar()));
   if (baseCalendar == null)
   {
      baseCalendar = m_defaultCalendar;
   }
   calendar.setParent(baseCalendar);

   m_eventManager.fireResourceReadEvent(mpxjResource);
}
 
Example 9
Source File: CalendarRecord.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override public void process(Context context)
{
   ProjectCalendar calendar = context.addCalendar(getString(0));
   calendar.setName(getString(2));

   String flags = getString(1);
   for (Day day : Day.values())
   {
      calendar.setWorkingDay(day, flags.charAt(day.getValue() - 1) == 'Y');
   }
   context.getEventManager().fireCalendarReadEvent(calendar);
}
 
Example 10
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 11
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 12
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 13
Source File: PrimaveraReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Retrieve the correct calendar for a resource.
 *
 * @param calendarID calendar ID
 * @return calendar for resource
 */
private ProjectCalendar getResourceCalendar(Integer calendarID)
{
   ProjectCalendar result = null;
   if (calendarID != null)
   {
      ProjectCalendar calendar = m_project.getCalendarByUniqueID(calendarID);
      if (calendar != null)
      {
         //
         // If the resource is linked to a base calendar, derive
         // a default calendar from the base calendar.
         //
         if (!calendar.isDerived())
         {
            ProjectCalendar resourceCalendar = m_project.addCalendar();
            resourceCalendar.setParent(calendar);
            resourceCalendar.setWorkingDay(Day.MONDAY, DayType.DEFAULT);
            resourceCalendar.setWorkingDay(Day.TUESDAY, DayType.DEFAULT);
            resourceCalendar.setWorkingDay(Day.WEDNESDAY, DayType.DEFAULT);
            resourceCalendar.setWorkingDay(Day.THURSDAY, DayType.DEFAULT);
            resourceCalendar.setWorkingDay(Day.FRIDAY, DayType.DEFAULT);
            resourceCalendar.setWorkingDay(Day.SATURDAY, DayType.DEFAULT);
            resourceCalendar.setWorkingDay(Day.SUNDAY, DayType.DEFAULT);
            result = resourceCalendar;
         }
         else
         {
            //
            // Primavera seems to allow a calendar to be shared between resources
            // whereas in the MS Project model there is a one-to-one
            // relationship. If we find a shared calendar, take a copy of it
            //
            if (calendar.getResource() == null)
            {
               result = calendar;
            }
            else
            {
               ProjectCalendar copy = m_project.addCalendar();
               copy.copy(calendar);
               result = copy;
            }
         }
      }
   }

   return result;
}
 
Example 14
Source File: PrimaveraPMFileReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Process resources.
 *
 * @param apibo xml container
 */
private void processResources(APIBusinessObjects apibo)
{
   List<ResourceType> resources = apibo.getResource();
   for (ResourceType xml : resources)
   {
      Resource resource = m_projectFile.addResource();
      resource.setUniqueID(xml.getObjectId());
      resource.setName(xml.getName());
      resource.setCode(xml.getEmployeeId());
      resource.setEmailAddress(xml.getEmailAddress());
      resource.setGUID(DatatypeConverter.parseUUID(xml.getGUID()));
      resource.setNotes(xml.getResourceNotes());
      resource.setCreationDate(xml.getCreateDate());
      resource.setType(RESOURCE_TYPE_MAP.get(xml.getResourceType()));
      resource.setMaxUnits(reversePercentage(xml.getMaxUnitsPerTime()));
      resource.setParentID(xml.getParentObjectId());

      Integer calendarID = xml.getCalendarObjectId();
      if (calendarID != null)
      {
         ProjectCalendar calendar = m_projectFile.getCalendarByUniqueID(calendarID);
         if (calendar != null)
         {
            if (calendar.isDerived())
            {
               //
               // Primavera seems to allow a calendar to be shared between resources
               // whereas in the MS Project model there is a one-to-one
               // relationship. If we find a calendar is shared between resources,
               // take a copy of it so each resource has its own copy.
               //
               if (calendar.getResource() == null)
               {
                  resource.setResourceCalendar(calendar);
               }
               else
               {
                  ProjectCalendar copy = m_projectFile.addCalendar();
                  copy.copy(calendar);
                  resource.setResourceCalendar(copy);
               }
            }
            else
            {
               //
               // If the resource is linked to a base calendar, derive
               // a default calendar from the base calendar.
               //
               ProjectCalendar resourceCalendar = m_projectFile.addCalendar();
               resourceCalendar.setParent(calendar);
               resourceCalendar.setWorkingDay(Day.MONDAY, DayType.DEFAULT);
               resourceCalendar.setWorkingDay(Day.TUESDAY, DayType.DEFAULT);
               resourceCalendar.setWorkingDay(Day.WEDNESDAY, DayType.DEFAULT);
               resourceCalendar.setWorkingDay(Day.THURSDAY, DayType.DEFAULT);
               resourceCalendar.setWorkingDay(Day.FRIDAY, DayType.DEFAULT);
               resourceCalendar.setWorkingDay(Day.SATURDAY, DayType.DEFAULT);
               resourceCalendar.setWorkingDay(Day.SUNDAY, DayType.DEFAULT);
               resource.setResourceCalendar(resourceCalendar);
            }
         }
      }

      readUDFTypes(resource, xml.getUDF());

      m_eventManager.fireResourceReadEvent(resource);
   }
}
 
Example 15
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 16
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;
}