ews-java-api icon indicating copy to clipboard operation
ews-java-api copied to clipboard

Values of type 'java.util.GregorianCalendar' can't be used for the 'FirstDayOfWeek' element.

Open stulife opened this issue 6 years ago • 6 comments

Recurrence.WeeklyPattern weeklyPattern = new Recurrence.WeeklyPattern(startsDate, eventDto.getInterval(), dayOfTheWeeks.toArray(new DayOfTheWeek[dayOfTheWeeks.size()])); weeklyPattern.setStartDate(new Date(eventDto.getStartDate())); weeklyPattern.setEndDate(new Date(eventDto.getEndDate()));

Calendar cal=Calendar.getInstance(); cal.setFirstDayOfWeek(Calendar.SUNDAY); weeklyPattern.setFirstDayOfWeek(cal);

ServiceResponseCollection<ServiceResponse> response = service.createItems( list, folderId, MessageDisposition.SendOnly, SendInvitationsMode.SendToAllAndSaveCopy); Caused by: microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException: Values of type 'java.util.GregorianCalendar' can't be used for the 'FirstDayOfWeek' element.

stulife avatar Jul 03 '18 11:07 stulife

Check following testcase:


import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Calendar;

import org.junit.Test;

import microsoft.exchange.webservices.base.BaseTest;
import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
import microsoft.exchange.webservices.data.property.complex.recurrence.pattern.Recurrence.WeeklyPattern;

public class RecurrenceWriterTest extends BaseTest{

  @Test
  public void testWeeklyPattern() throws Exception {
    OutputStream output = new ByteArrayOutputStream();
    EwsServiceXmlWriter writer = new EwsServiceXmlWriter(exchangeServiceMock, output);

    WeeklyPattern weekly = new WeeklyPattern();
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(1);
    weekly.setFirstDayOfWeek(c);
    weekly.writeElementsToXml(writer);

    assertEquals(c, weekly.getFirstDayOfWeek());
  }
}

You are going to get this ServiceXmlSerializationException as below screenshot:

screen shot 2018-07-05 at 4 34 19 pm

leftstick avatar Jul 05 '18 08:07 leftstick

Hi @stulife, I could reproduce it. It's a bug in the java-ews-api. It's currently difficult with fixes, cause this repo is nearly dead. There is a plan to move/fork a 'community' repo. But here is a qucikfix, hopefully you can use it: `/**

  • Represents a recurrence pattern where each occurrence happens on specific
  • days a specific number of weeks after the previous one. */ public final static class WeeklyPattern extends IntervalPattern implements IComplexPropertyChangedDelegate {
/**
 * The days of the week.
 */
private DayOfTheWeekCollection daysOfTheWeek =
    new DayOfTheWeekCollection();

private DayOfTheWeek firstDayOfWeek;

/**
 * Initializes a new instance of the WeeklyPattern class. specific days
 * a specific number of weeks after the previous one.
 */
public WeeklyPattern() {
  super();

  this.daysOfTheWeek.addOnChangeEvent(this);
}

/**
 * Initializes a new instance of the WeeklyPattern class.
 *
 * @param startDate     the start date
 * @param interval      the interval
 * @param daysOfTheWeek the days of the week
 * @throws ArgumentOutOfRangeException the argument out of range exception
 */
public WeeklyPattern(Date startDate, int interval,
    DayOfTheWeek... daysOfTheWeek)
    throws ArgumentOutOfRangeException {
  super(startDate, interval);

  ArrayList<DayOfTheWeek> toProcess = new ArrayList<DayOfTheWeek>(
      Arrays.asList(daysOfTheWeek));
  Iterator<DayOfTheWeek> idaysOfTheWeek = toProcess.iterator();
  this.daysOfTheWeek.addRange(idaysOfTheWeek);
}

/**
 * Change event handler.
 *
 * @param complexProperty the complex property
 */
private void daysOfTheWeekChanged(ComplexProperty complexProperty) {
  this.changed();
}

/**
 * Gets the name of the XML element. <value>The name of the XML
 * element.</value>
 *
 * @return the xml element name
 */
@Override
public String getXmlElementName() {
  return XmlElementNames.WeeklyRecurrence;
}

/**
 * Write property to XML.
 *
 * @param writer the writer
 * @throws Exception the exception
 */
@Override
public void internalWritePropertiesToXml(EwsServiceXmlWriter writer)
    throws Exception {
  super.internalWritePropertiesToXml(writer);

  this.getDaysOfTheWeek().writeToXml(writer,
      XmlElementNames.DaysOfWeek);

  EwsUtilities.validatePropertyVersion((ExchangeService) writer.getService(), ExchangeVersion.Exchange2010_SP1,
                                 "FirstDayOfWeek");

  writer.writeElementValue(
        XmlNamespace.Types,
        XmlElementNames.FirstDayOfWeek,
        this.firstDayOfWeek);

}

/**
 * Tries to read element from XML.
 *
 * @param reader the reader
 * @return True if appropriate element was read.
 * @throws Exception the exception
 */
@Override
public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
    throws Exception {
  if (super.tryReadElementFromXml(reader)) {
    return true;
  } else {
    if (reader.getLocalName().equals(XmlElementNames.DaysOfWeek)) {

      this.getDaysOfTheWeek().loadFromXml(reader,
          reader.getLocalName());
      return true;
    } else if (reader.getLocalName().equals(XmlElementNames.FirstDayOfWeek)) {
      this.firstDayOfWeek = reader.
          readElementValue(DayOfTheWeek.class,
              XmlNamespace.Types,
              XmlElementNames.FirstDayOfWeek);
      return true;
    } else {

      return false;
    }
  }
}

/**
 * Validates this instance.
 *
 * @throws Exception
 */
@Override
public void internalValidate() throws Exception {
  super.internalValidate();

  if (this.getDaysOfTheWeek().getCount() == 0) {
    throw new ServiceValidationException(
        "The recurrence pattern's property DaysOfTheWeek must contain at least one day of the week.");
  }
}

/**
 * Gets the list of the days of the week when occurrences happen.
 *
 * @return the days of the week
 */
public DayOfTheWeekCollection getDaysOfTheWeek() {
  return this.daysOfTheWeek;
}

public DayOfTheWeek getFirstDayOfWeek() throws ServiceValidationException {
  return this.getFieldValueOrThrowIfNull(DayOfTheWeek.class,
      this.firstDayOfWeek, "FirstDayOfWeek");
}

public void setFirstDayOfWeek(DayOfTheWeek value) {
  if (this.canSetFieldValue(this.firstDayOfWeek, value)) {
    this.firstDayOfWeek = value;
    this.changed();
  }
}

/*
 * (non-Javadoc)
 *
 * @see
 * microsoft.exchange.webservices.
 * ComplexPropertyChangedDelegateInterface#
 * complexPropertyChanged(microsoft.exchange.webservices.ComplexProperty
 * )
 */
@Override
public void complexPropertyChanged(ComplexProperty complexProperty) {
  this.daysOfTheWeekChanged(complexProperty);
}

}`

@leftstick: Thanks, for your unit test. I added some testcases on my fork, I hope to merge it as soon as possible to share this with you!

Thanks, Jan

OS-JaR avatar Jul 06 '18 12:07 OS-JaR

it's right ,with my solution as same,thank's

stulife avatar Jul 06 '18 12:07 stulife

Hey, I understand that the repo is almost dead, but is there any chance this will be fixed anytime soon?

lpld avatar Nov 28 '19 14:11 lpld

@lpld you can check the fix @OS-JaR provided. And re-compile the source code, publish it to your own maven repo.

You and your team should fetch this library from your own maven repo instead. That's what we did in our team

leftstick avatar Nov 29 '19 06:11 leftstick

@leftstick Thanks for the answer. Probably that's what we'll do in the future too, 'cause it's not the first issue with this library that we face. For now I felt like it's easier to me to just extend couple classes, create new Recurrence PropertyDefinition with this issue fixed and replace it in AppointmentSchema via reflection.

lpld avatar Nov 29 '19 09:11 lpld