001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Arnaud Kervern 018 */ 019package org.nuxeo.ecm.agenda; 020 021import java.io.Serializable; 022import java.util.Date; 023import java.util.Map; 024 025import org.apache.commons.lang.StringUtils; 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.joda.time.DateTime; 029import org.joda.time.format.DateTimeFormatter; 030import org.joda.time.format.ISODateTimeFormat; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.DocumentModelList; 034import org.nuxeo.ecm.core.api.NuxeoException; 035import org.nuxeo.ecm.core.api.PropertyException; 036import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 037import org.nuxeo.runtime.api.Framework; 038import org.nuxeo.runtime.model.DefaultComponent; 039 040/** 041 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a> 042 * @since 5.6 043 */ 044public class AgendaComponent extends DefaultComponent implements AgendaService { 045 046 public static final String VEVENT_TYPE = "VEVENT"; 047 048 public static final String SCHEDULABLE_TYPE = "Schedulable"; 049 050 protected static final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime(); 051 052 protected static final String QUERY_BETWEEN_DATES = "SELECT * FROM Document WHERE " + "ecm:mixinType = '" 053 + SCHEDULABLE_TYPE + "' " + "AND ((vevent:dtstart BETWEEN TIMESTAMP '%s' AND TIMESTAMP '%s') " 054 + "OR (vevent:dtend BETWEEN TIMESTAMP '%s' AND TIMESTAMP '%s') " 055 + "OR (vevent:dtstart < TIMESTAMP '%s' AND vevent:dtend > TIMESTAMP '%s') " 056 + "OR (vevent:dtstart > TIMESTAMP '%s' AND vevent:dtend < TIMESTAMP '%s')) " 057 + "AND ecm:currentLifeCycleState != 'deleted' " + "AND ecm:isCheckedInVersion = 0 AND ecm:isProxy = 0 " 058 + "AND ecm:path STARTSWITH '%s' ORDER BY vevent:dtstart"; 059 060 protected static final String QUERY_LIMIT = "SELECT * FROM Document WHERE " + "ecm:mixinType = '" 061 + SCHEDULABLE_TYPE + "' " + "AND vevent:dtend > TIMESTAMP '%s' " 062 + "AND ecm:currentLifeCycleState != 'deleted' " + "AND ecm:isCheckedInVersion = 0 AND ecm:isProxy = 0 " 063 + "AND ecm:path STARTSWITH '%s' ORDER BY vevent:dtstart"; 064 065 private static final Log log = LogFactory.getLog(AgendaComponent.class); 066 067 @Override 068 public DocumentModelList listEvents(CoreSession session, String path, Date dtStart, Date dtEnd) 069 { 070 if (dtStart == null) { 071 throw new NuxeoException("Start datetime should not be null"); 072 } 073 if (dtEnd == null) { 074 dtEnd = new Date(dtStart.getTime() + 24 * 3600); 075 } 076 if (dtEnd.before(dtStart)) { 077 throw new NuxeoException("End datetime is before start datetime"); 078 } 079 080 String strStart = formatDate(dtStart); 081 String strEnd = formatDate(dtEnd); 082 return session.query(String.format(QUERY_BETWEEN_DATES, strStart, strEnd, strStart, strEnd, strStart, strEnd, 083 strStart, strEnd, path)); 084 } 085 086 @Override 087 public DocumentModelList listEvents(CoreSession session, String path, int limit) { 088 if (limit <= 0) { 089 throw new NuxeoException("Limit must be greater than 0"); 090 } 091 092 return session.query(String.format(QUERY_LIMIT, formatDate(new Date()), path), limit); 093 } 094 095 protected static String formatDate(Date date) { 096 return new DateTime(date.getTime()).toString(dateTimeFormatter); 097 } 098 099 @Override 100 public DocumentModel createEvent(CoreSession session, String path, Map<String, Serializable> properties) 101 { 102 if (StringUtils.isBlank(path) || "/".equals(path)) { 103 path = getCurrentUserWorkspacePath(session); 104 } 105 DocumentModel doc = session.createDocumentModel(VEVENT_TYPE); 106 doc.setPathInfo(path, null); 107 for (String key : properties.keySet()) { 108 try { 109 doc.setPropertyValue(key, properties.get(key)); 110 } catch (PropertyException pe) { 111 log.info("Trying to set an unknown property " + key); 112 } 113 } 114 return session.createDocument(doc); 115 } 116 117 protected String getCurrentUserWorkspacePath(CoreSession session) { 118 UserWorkspaceService userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class); 119 DocumentModel userPersonalWorkspace = userWorkspaceService.getUserPersonalWorkspace( 120 session.getPrincipal().getName(), session.getRootDocument()); 121 return userPersonalWorkspace.getPathAsString(); 122 } 123}