Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWebDAV for Exchange
>
Tutorial
> Search for appointments with category "Holiday"
The following example shows you how to search for appointments belong to category "Holiday".
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.TimeZone; import com.independentsoft.webdav.exchange.AppointmentPropertyName; import com.independentsoft.webdav.exchange.Condition; import com.independentsoft.webdav.exchange.ContentClassType; import com.independentsoft.webdav.exchange.From; import com.independentsoft.webdav.exchange.Like; import com.independentsoft.webdav.exchange.LogicalOperator; import com.independentsoft.webdav.exchange.Mailbox; import com.independentsoft.webdav.exchange.MultiStatus; import com.independentsoft.webdav.exchange.Operator; import com.independentsoft.webdav.exchange.Property; import com.independentsoft.webdav.exchange.PropertyName; import com.independentsoft.webdav.exchange.SearchResult; import com.independentsoft.webdav.exchange.SearchResultRecord; import com.independentsoft.webdav.exchange.Select; import com.independentsoft.webdav.exchange.SqlQuery; import com.independentsoft.webdav.exchange.WebdavClient; import com.independentsoft.webdav.exchange.WebdavException; import com.independentsoft.webdav.exchange.Where; public class Example { public static void main(final String[] args) { try { WebdavClient client = new WebdavClient("https://myserver/exchange/emailaddress", "username", "password"); Mailbox myMailbox = client.getMailbox("https://myserver/exchange/emailaddress"); PropertyName keywordsPropertyName = new PropertyName("Keywords", "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/"); List
propertyNames = new ArrayList
(); propertyNames.add(AppointmentPropertyName.CONTENT_CLASS); propertyNames.add(AppointmentPropertyName.START_DATE); propertyNames.add(AppointmentPropertyName.END_DATE); propertyNames.add(AppointmentPropertyName.SUBJECT); propertyNames.add(AppointmentPropertyName.BODY); propertyNames.add(keywordsPropertyName); Select select = new Select(propertyNames); From from = new From(myMailbox.getCalendar()); Where where = new Where(); Condition condition1 = new Condition(AppointmentPropertyName.CONTENT_CLASS, Operator.EQUALS, ContentClassType.APPOINTMENT); Like condition2 = new Like(keywordsPropertyName, "Holiday"); where.add(condition1); where.add(LogicalOperator.AND); where.add(condition2); SqlQuery sqlQuery = new SqlQuery(select, from, where); MultiStatus multiStatus = client.search(sqlQuery); SearchResult searchResult = new SearchResult(multiStatus, propertyNames); SearchResultRecord[] allRecords = searchResult.getRecords(); for (int i = 0; i < allRecords.length; i++) { System.out.println("Appointment URL=" + allRecords[i].getUrl()); //appoitnment's properties Property[] property = allRecords[i].getProperties(); System.out.println("ContentClass=" + property[0].getValue()); System.out.println("StartDate=" + toLocalTime(property[1].getValue())); System.out.println("EndDate=" + toLocalTime(property[2].getValue())); System.out.println("Subject=" + property[3].getValue()); System.out.println("Body=" + property[4].getValue()); } } catch (WebdavException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } static Date toLocalTime(String utcTime) throws ParseException { if (utcTime != null && utcTime.length() > 0) { String format = "yyyy-MM-dd'T'HH:mm:ss.sss'Z'"; DateFormat dateFormat = new SimpleDateFormat(format, Locale.US); Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); dateFormat.setCalendar(utcCalendar); return dateFormat.parse(utcTime); } else { return null; } } }