Java Code Examples for net.sf.mpxj.Duration#convertUnits()

The following examples show how to use net.sf.mpxj.Duration#convertUnits() . 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: DatatypeConverter.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Print duration.
 *
 * Note that Microsoft's xsd:duration parser implementation does not
 * appear to recognise durations other than those expressed in hours.
 * We use the compatibility flag to determine whether the output
 * is adjusted for the benefit of Microsoft Project.
 *
 * @param writer parent MSPDIWriter instance
 * @param duration Duration value
 * @return xsd:duration value
 */
public static final String printDurationMandatory(MSPDIWriter writer, Duration duration)
{
   String result;

   if (duration == null)
   {
      // SF-329: null default required to keep Powerproject happy when importing MSPDI files
      result = "PT0H0M0S";
   }
   else
   {
      TimeUnit durationType = duration.getUnits();

      if (durationType != TimeUnit.HOURS && durationType != TimeUnit.ELAPSED_HOURS)
      {
         duration = duration.convertUnits(TimeUnit.HOURS, writer.getProjectFile().getProjectProperties());
      }
      result = new XsdDuration(duration).print(writer.getMicrosoftProjectCompatibleOutput());
   }

   return (result);
}
 
Example 2
Source File: DatatypeConverter.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Parse duration represented as an arbitrary fraction of minutes.
 *
 * @param properties project properties
 * @param value duration value
 * @param targetTimeUnit required output time units
 * @param factor required fraction of a minute
 * @return Duration instance
 */
private static final Duration parseDurationInFractionsOfMinutes(ProjectProperties properties, Number value, TimeUnit targetTimeUnit, int factor)
{
   Duration result = null;

   if (value != null)
   {
      result = Duration.getInstance(value.intValue() / factor, TimeUnit.MINUTES);
      if (targetTimeUnit != result.getUnits())
      {
         result = result.convertUnits(targetTimeUnit, properties);
      }
   }

   return (result);
}
 
Example 3
Source File: RecurrenceUtility.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Convert the integer representation of a duration value and duration units
 * into an MPXJ Duration instance.
 *
 * @param properties project properties, used for duration units conversion
 * @param durationValue integer duration value
 * @param unitsValue integer units value
 * @return Duration instance
 */
public static Duration getDuration(ProjectProperties properties, Integer durationValue, Integer unitsValue)
{
   Duration result;
   if (durationValue == null)
   {
      result = null;
   }
   else
   {
      result = Duration.getInstance(durationValue.intValue(), TimeUnit.MINUTES);
      TimeUnit units = getDurationUnits(unitsValue);
      if (result.getUnits() != units)
      {
         result = result.convertUnits(units, properties);
      }
   }
   return (result);
}
 
Example 4
Source File: RecurrenceUtility.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Convert an MPXJ Duration instance into an integer duration in minutes
 * ready to be written to an MPX file.
 *
 * @param properties project properties, used for duration units conversion
 * @param duration Duration instance
 * @return integer duration in minutes
 */
public static Integer getDurationValue(ProjectProperties properties, Duration duration)
{
   Integer result;
   if (duration == null)
   {
      result = null;
   }
   else
   {
      if (duration.getUnits() != TimeUnit.MINUTES)
      {
         duration = duration.convertUnits(TimeUnit.MINUTES, properties);
      }
      result = Integer.valueOf((int) duration.getDuration());
   }
   return (result);
}
 
Example 5
Source File: JsonWriter.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Write a duration field to the JSON file.
 *
 * @param fieldName field name
 * @param value field value
 */
private void writeDurationField(String fieldName, Object value) throws IOException
{
   if (value != null)
   {
      if (value instanceof String)
      {
         m_writer.writeNameValuePair(fieldName + "_text", (String) value);
      }
      else
      {
         Duration val = (Duration) value;
         if (val.getDuration() != 0)
         {
            Duration minutes = val.convertUnits(TimeUnit.MINUTES, m_projectFile.getProjectProperties());
            long seconds = (long) (minutes.getDuration() * 60.0);
            m_writer.writeNameValuePair(fieldName, seconds);
         }
      }
   }
}
 
Example 6
Source File: PrimaveraPMFileWriter.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Retrieve a duration in the form required by Primavera.
 *
 * @param duration Duration instance
 * @return formatted duration
 */
private Double getDuration(Duration duration)
{
   Double result;
   if (duration == null)
   {
      result = null;
   }
   else
   {
      if (duration.getUnits() != TimeUnit.HOURS)
      {
         duration = duration.convertUnits(TimeUnit.HOURS, m_projectFile.getProjectProperties());
      }

      result = Double.valueOf(duration.getDuration());
   }
   return result;
}
 
Example 7
Source File: MppXmlCompare.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Duration equality assertion.
 *
 * @param expected expected value
 * @param actual actual value
 * @throws Exception
 */
private void assertEquals(Duration expected, Duration actual) throws Exception
{
   if (expected != null || actual != null)
   {
      if (expected != null && actual != null)
      {
         if (expected.getDuration() != 0 || actual.getDuration() != 0)
         {
            if (expected.getUnits() != actual.getUnits())
            {
               actual = actual.convertUnits(expected.getUnits(), m_mpp.getProjectProperties());
            }

            assertEquals(expected.getDuration(), actual.getDuration(), 0.99);
         }
      }
      else
      {
         if ((actual == null && expected != null && expected.getDuration() != 0) || (actual != null && actual.getDuration() != 0 && expected == null))
         {
            assertEquals((Object) expected, (Object) actual);
         }
      }
   }
}
 
Example 8
Source File: SDEFWriter.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Write each predecessor for a task.
 *
 * @param record Task instance
 */
private void writeTaskPredecessors(Task record)
{
   m_buffer.setLength(0);
   //
   // Write the task predecessor
   //
   if (!record.getSummary() && !record.getPredecessors().isEmpty())
   { // I don't use summary tasks for SDEF
      m_buffer.append("PRED ");
      List<Relation> predecessors = record.getPredecessors();

      for (Relation pred : predecessors)
      {
         m_buffer.append(SDEFmethods.rset(pred.getSourceTask().getUniqueID().toString(), 10) + " ");
         m_buffer.append(SDEFmethods.rset(pred.getTargetTask().getUniqueID().toString(), 10) + " ");
         String type = "C"; // default finish-to-start
         if (!pred.getType().toString().equals("FS"))
         {
            type = pred.getType().toString().substring(0, 1);
         }
         m_buffer.append(type + " ");

         Duration dd = pred.getLag();
         double duration = dd.getDuration();
         if (dd.getUnits() != TimeUnit.DAYS)
         {
            dd = Duration.convertUnits(duration, dd.getUnits(), TimeUnit.DAYS, m_minutesPerDay, m_minutesPerWeek, m_daysPerMonth);
         }
         Double days = Double.valueOf(dd.getDuration() + 0.5); // Add 0.5 so half day rounds up upon truncation
         Integer est = Integer.valueOf(days.intValue());
         m_buffer.append(SDEFmethods.rset(est.toString(), 4) + " "); // task duration in days required by USACE
      }
      m_writer.println(m_buffer.toString());
   }
}
 
Example 9
Source File: MSPDIReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * This method extracts data for a single predecessor from an MSPDI file.
 *
 * @param currTask Current task object
 * @param link Predecessor data
 */
private void readPredecessor(Task currTask, Project.Tasks.Task.PredecessorLink link)
{
   BigInteger uid = link.getPredecessorUID();
   if (uid != null)
   {
      Task prevTask = m_projectFile.getTaskByUniqueID(Integer.valueOf(uid.intValue()));
      if (prevTask != null)
      {
         RelationType type;
         if (link.getType() != null)
         {
            type = RelationType.getInstance(link.getType().intValue());
         }
         else
         {
            type = RelationType.FINISH_START;
         }

         TimeUnit lagUnits = DatatypeConverter.parseDurationTimeUnits(link.getLagFormat());

         Duration lagDuration;
         int lag = NumberHelper.getInt(link.getLinkLag());
         if (lag == 0)
         {
            lagDuration = Duration.getInstance(0, lagUnits);
         }
         else
         {
            if (lagUnits == TimeUnit.PERCENT || lagUnits == TimeUnit.ELAPSED_PERCENT)
            {
               lagDuration = Duration.getInstance(lag, lagUnits);
            }
            else
            {
               lagDuration = Duration.convertUnits(lag / 10.0, TimeUnit.MINUTES, lagUnits, m_projectFile.getProjectProperties());
            }
         }

         Relation relation = currTask.addPredecessor(prevTask, type, lagDuration);
         m_eventManager.fireRelationReadEvent(relation);
      }
   }
}
 
Example 10
Source File: MPXReader.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Populate a resource assignment.
 *
 * @param record MPX record
 * @param assignment resource assignment
 * @throws MPXJException
 */
private void populateResourceAssignment(Record record, ResourceAssignment assignment) throws MPXJException
{
   //
   // Handle malformed MPX files - ensure that we can locate the resource
   // using either the Unique ID attribute or the ID attribute.
   //
   Resource resource = m_projectFile.getResourceByUniqueID(record.getInteger(12));
   if (resource == null)
   {
      resource = m_projectFile.getResourceByID(record.getInteger(0));
   }

   assignment.setUnits(record.getUnits(1));
   assignment.setWork(record.getDuration(2));
   assignment.setBaselineWork(record.getDuration(3));
   assignment.setActualWork(record.getDuration(4));
   assignment.setOvertimeWork(record.getDuration(5));
   assignment.setCost(record.getCurrency(6));
   assignment.setBaselineCost(record.getCurrency(7));
   assignment.setActualCost(record.getCurrency(8));
   assignment.setStart(record.getDateTime(9));
   assignment.setFinish(record.getDateTime(10));
   assignment.setDelay(record.getDuration(11));

   //
   // Calculate the remaining work
   //
   Duration work = assignment.getWork();
   Duration actualWork = assignment.getActualWork();
   if (work != null && actualWork != null)
   {
      if (work.getUnits() != actualWork.getUnits())
      {
         actualWork = actualWork.convertUnits(work.getUnits(), m_projectFile.getProjectProperties());
      }

      assignment.setRemainingWork(Duration.getInstance(work.getDuration() - actualWork.getDuration(), work.getUnits()));
   }

   if (resource != null)
   {
      assignment.setResourceUniqueID(resource.getUniqueID());
      resource.addResourceAssignment(assignment);
   }

   m_eventManager.fireAssignmentReadEvent(assignment);
}
 
Example 11
Source File: SDEFWriter.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Write a task.
 *
 * @param record task instance
 * @throws IOException
 */
private void writeTask(Task record) throws IOException
{
   m_buffer.setLength(0);
   if (!record.getSummary())
   {
      m_buffer.append("ACTV ");
      m_buffer.append(SDEFmethods.rset(record.getUniqueID().toString(), 10) + " ");
      m_buffer.append(SDEFmethods.lset(record.getName(), 30) + " ");

      // Following just makes certain we have days for duration, as per USACE spec.
      Duration dd = record.getDuration();
      double duration = dd.getDuration();
      if (dd.getUnits() != TimeUnit.DAYS)
      {
         dd = Duration.convertUnits(duration, dd.getUnits(), TimeUnit.DAYS, m_minutesPerDay, m_minutesPerWeek, m_daysPerMonth);
      }
      Double days = Double.valueOf(dd.getDuration() + 0.5); // Add 0.5 so half day rounds up upon truncation
      Integer est = Integer.valueOf(days.intValue());
      m_buffer.append(SDEFmethods.rset(est.toString(), 3) + " "); // task duration in days required by USACE

      String conType = "ES "; // assume early start
      Date conDate = record.getEarlyStart();
      int test = record.getConstraintType().getValue(); // test for other types
      if (test == 1 || test == 3 || test == 6 || test == 7 || test == 9)
      {
         conType = "LF "; // see ConstraintType enum for definitions
         conDate = record.getLateFinish();
      }
      m_buffer.append(m_formatter.format(conDate).toUpperCase() + " "); // Constraint Date
      m_buffer.append(conType); // Constraint Type
      if (record.getCalendar() == null)
      {
         m_buffer.append("1 ");
      }
      else
      {
         m_buffer.append(SDEFmethods.lset(record.getCalendar().getUniqueID().toString(), 1) + " ");
      }
      // skipping hammock code in here
      // use of text fields for extra USACE data is suggested at my web site: www.geocomputer.com
      // not documented on how to do this here, so I need to comment out at present
      //	      m_buffer.append(SDEFmethods.Lset(record.getText1(), 3) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText2(), 4) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText3(), 4) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText4(), 6) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText5(), 6) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText6(), 2) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText7(), 1) + " ");
      //	      m_buffer.append(SDEFmethods.Lset(record.getText8(), 30) + " ");
      m_writer.println(m_buffer.toString());
      m_eventManager.fireTaskWrittenEvent(record);
   }
}
 
Example 12
Source File: SDEFWriter.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Writes a progress line to the SDEF file.
 *
 * Progress lines in SDEF are a little tricky, you need to assume a percent complete
 * this could be physical or temporal, I don't know what you're using???
 * So in this version of SDEFwriter, I just put in 0.00 for cost progress to date, see *** below
 *
 * @param record Task instance
 */
private void writePROG(Task record)
{
   m_buffer.setLength(0);
   //
   // Write the progress record
   //
   if (!record.getSummary())
   { // I don't use summary tasks for SDEF
      m_buffer.append("PROG ");
      m_buffer.append(SDEFmethods.rset(record.getUniqueID().toString(), 10) + " ");
      Date temp = record.getActualStart();
      if (temp == null)
      {
         m_buffer.append("        "); // SDEf is column sensitive, so the number of blanks here is crucial
      }
      else
      {
         m_buffer.append(m_formatter.format(record.getActualStart()).toUpperCase() + " "); // ACTUAL START DATE
      }
      temp = record.getActualFinish();
      if (temp == null)
      {
         m_buffer.append("        ");
      }
      else
      {
         m_buffer.append(m_formatter.format(record.getActualFinish()).toUpperCase() + " "); // ACTUAL FINISH DATE
      }

      Duration dd = record.getRemainingDuration();
      double duration = dd.getDuration();
      if (dd.getUnits() != TimeUnit.DAYS)
      {
         dd = Duration.convertUnits(duration, dd.getUnits(), TimeUnit.DAYS, m_minutesPerDay, m_minutesPerWeek, m_daysPerMonth);
      }
      Double days = Double.valueOf(dd.getDuration() + 0.5); // Add 0.5 so half day rounds up upon truncation
      Integer est = Integer.valueOf(days.intValue());
      m_buffer.append(SDEFmethods.rset(est.toString(), 3) + " "); // task duration in days required by USACE

      DecimalFormat twoDec = new DecimalFormat("#0.00"); // USACE required currency format
      m_buffer.append(SDEFmethods.rset(twoDec.format(record.getCost().floatValue()), 12) + " ");
      m_buffer.append(SDEFmethods.rset(twoDec.format(0.00), 12) + " "); // *** assume zero progress on cost
      m_buffer.append(SDEFmethods.rset(twoDec.format(0.00), 12) + " "); // *** assume zero progress on cost
      m_buffer.append(m_formatter.format(record.getEarlyStart()).toUpperCase() + " ");
      m_buffer.append(m_formatter.format(record.getEarlyFinish()).toUpperCase() + " ");
      m_buffer.append(m_formatter.format(record.getLateStart()).toUpperCase() + " ");
      m_buffer.append(m_formatter.format(record.getLateFinish()).toUpperCase() + " ");

      dd = record.getTotalSlack();
      duration = dd.getDuration();
      if (dd.getUnits() != TimeUnit.DAYS)
      {
         dd = Duration.convertUnits(duration, dd.getUnits(), TimeUnit.DAYS, m_minutesPerDay, m_minutesPerWeek, m_daysPerMonth);
      }
      days = Double.valueOf(dd.getDuration() + 0.5); // Add 0.5 so half day rounds up upon truncation
      est = Integer.valueOf(days.intValue());
      char slack;
      if (est.intValue() >= 0)
      {
         slack = '+'; // USACE likes positive slack, so they separate the sign from the value
      }
      else
      {
         slack = '-'; // only write a negative when it's negative, i.e. can't be done in project management terms!!!
      }
      m_buffer.append(slack + " ");
      est = Integer.valueOf(Math.abs(days.intValue()));
      m_buffer.append(SDEFmethods.rset(est.toString(), 4)); // task duration in days required by USACE
      m_writer.println(m_buffer.toString());
      m_eventManager.fireTaskWrittenEvent(record);
   }
}