001/* 002 * (C) Copyright 2012-2018 Nuxeo (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.lang3.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.common.utils.IdUtils; 032import org.nuxeo.ecm.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentModelList; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.core.api.PropertyException; 037import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 038import org.nuxeo.runtime.api.Framework; 039import org.nuxeo.runtime.model.DefaultComponent; 040 041/** 042 * @author <a href="mailto:[email protected]">Arnaud Kervern</a> 043 * @since 5.6 044 */ 045public class AgendaComponent extends DefaultComponent implements AgendaService { 046 047 public static final String VEVENT_TYPE = "VEVENT"; 048 049 public static final String SCHEDULABLE_TYPE = "Schedulable"; 050 051 protected static final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime(); 052 053 protected static final String QUERY_BETWEEN_DATES = "SELECT * FROM Document WHERE " + "ecm:mixinType = '" 054 + SCHEDULABLE_TYPE + "' " + "AND ((vevent:dtstart BETWEEN TIMESTAMP '%s' AND TIMESTAMP '%s') " 055 + "OR (vevent:dtend BETWEEN TIMESTAMP '%s' AND TIMESTAMP '%s') " 056 + "OR (vevent:dtstart < TIMESTAMP '%s' AND vevent:dtend > TIMESTAMP '%s') " 057 + "OR (vevent:dtstart > TIMESTAMP '%s' AND vevent:dtend < TIMESTAMP '%s')) " 058 + "AND ecm:isTrashed = 0 " + "AND ecm:isVersion = 0 AND ecm:isProxy = 0 " 059 + "AND ecm:path STARTSWITH '%s' ORDER BY vevent:dtstart"; 060 061 protected static final String QUERY_LIMIT = "SELECT * FROM Document WHERE " + "ecm:mixinType = '" + SCHEDULABLE_TYPE 062 + "' " + "AND vevent:dtend > TIMESTAMP '%s' " + "AND ecm:isTrashed = 0 " 063 + "AND ecm:isVersion = 0 AND ecm:isProxy = 0 " 064 + "AND ecm:path STARTSWITH '%s' ORDER BY vevent:dtstart"; 065 066 private static final Log log = LogFactory.getLog(AgendaComponent.class); 067 068 @Override 069 public DocumentModelList listEvents(CoreSession session, String path, Date dtStart, Date dtEnd) { 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 if (StringUtils.isBlank(path) || "/".equals(path)) { 102 path = getCurrentUserWorkspacePath(session); 103 } 104 DocumentModel doc = session.createDocumentModel(VEVENT_TYPE); 105 doc.setPathInfo(path, IdUtils.generateStringId()); 106 for (String key : properties.keySet()) { 107 try { 108 doc.setPropertyValue(key, properties.get(key)); 109 } catch (PropertyException pe) { 110 log.info("Trying to set an unknown property " + key); 111 } 112 } 113 return session.createDocument(doc); 114 } 115 116 protected String getCurrentUserWorkspacePath(CoreSession session) { 117 UserWorkspaceService userWorkspaceService = Framework.getService(UserWorkspaceService.class); 118 DocumentModel userPersonalWorkspace = userWorkspaceService.getUserPersonalWorkspace( 119 session.getPrincipal().getName(), session.getRootDocument()); 120 return userPersonalWorkspace.getPathAsString(); 121 } 122}