Index: contrib/psync/src/main/java/br/com/prognus/psync/items/dao/PIMCalendarDAO.java
===================================================================
--- contrib/psync/src/main/java/br/com/prognus/psync/items/dao/PIMCalendarDAO.java	(revision 1009)
+++ contrib/psync/src/main/java/br/com/prognus/psync/items/dao/PIMCalendarDAO.java	(revision 1103)
@@ -135,12 +135,21 @@
 	private Sync4jPrincipal principal = null;
 
+	private Boolean sourceQuery = false;
+	private Timestamp date1 = null;
+	private Timestamp date2 = null;
+
 	/*
 	 * @see PIMEntityDAO#PIMEntityDAO(String, String)
 	 */
-	public PIMCalendarDAO(String jndiDataSourceName, Sync4jPrincipal principal) {
+	public PIMCalendarDAO(String jndiDataSourceName, Sync4jPrincipal principal, String sourceQuery) throws Exception {
 
 		super(jndiDataSourceName, principal.getUsername());
 		log.info("\n\n=> Created new PIMCalendarDAO for data source " + jndiDataSourceName + " and user ID " + userId);
 		this.principal = principal;
+
+		if(sourceQuery != null){
+			this.sourceQuery = true;
+			findDates(sourceQuery);
+		}
 	}
 
@@ -178,4 +187,8 @@
 		long id_cal  = 0;
 
+		long datea1 = 0;
+		long datea2 = 0;
+		long dateb1 = 0;
+
 		try {
 
@@ -202,72 +215,79 @@
 			}
 
-			rp          = c.getRecurrencePattern(); // Recebe a repetiÃ§Ã£o do alarme
-			reminder    = c.getReminder(); // Recebe o alarme do evento
-			body 		= stringFrom(c.getDescription()); // Descricao completa do evento
-			location 	= stringFrom(c.getLocation()); // Localizacao do evento
-			is_public 	= (stringFrom(c.getAccessClass())).equals("2") ? "0" : "1"; // Evento particular ou publico
-			subject 	= stringFrom(c.getSummary()); // Titulo do evento
-			category    = stringFrom(c.getCategories());
-
-			// Verifica o parametro uid necessario para o expresso
-			ps = con.prepareStatement("select config_value from phpgw_config WHERE config_name = 'hostname' AND config_app = 'phpgwapi'");
-			rs = ps.executeQuery();
-			String uid = rs.next() ? ("-@" + rs.getString(1)) : "";
-
-			// Verifica a categoria do evento
-			if(category != null && (!category.equals(""))){
-				category = addCategory(category);
-			}
-
-			// Prepara os dados para inserir o evento no banco de dados
-			ps = con.prepareStatement(SQL_INSERT_INTO_EXP_PIM_CALENDAR);
-			ps.setString(1, uid); // -@hostname
-			ps.setLong(2, Long.parseLong(userId)); // owner
-			ps.setString(3, category); // category
-			ps.setLong(4, new Long(Long.toString(dtstart.getTime()).substring(0, 10))); // datetime
-			ps.setLong(5, new Long(Long.toString(lastUpdate.getTime()).substring(0, 10))); // mdatetime
-			ps.setLong(6, new Long(Long.toString(dend.getTime()).substring(0, 10))); // edatetime
-			ps.setString(7, (rp == null) ? "E" : "M"); // cal_type
-			ps.setLong(8, Long.parseLong(is_public)); // is_plublic
-			ps.setString(9, truncate(subject, SQL_SUBJECT_DIM)); // title
-			ps.setString(10, truncate(body, SQL_BODY_DIM)); // body of event, a little description of it
-			ps.setString(11, truncate(location, SQL_LOCATION_DIM)); // location of the event
-			ps.setString(12, ""); // Participants outside
-			ps.setLong(13, lastUpdate.getTime()); // last update
-			ps.setString(14, String.valueOf(Def.PIM_STATE_NEW)); // status
-			ps.executeUpdate();
-
-			// Verifica o id maximo do evento inserido
-			ps = con.prepareStatement("select max(cal_id) from phpgw_cal limit 1");
-			rs = ps.executeQuery();
-
-			if (rs.next()) {
-				long id = rs.getInt(1);
-				id_cal = (id == 0) ? 1 : id;
-			}
-
-			cw.setId(Long.toString(id_cal));
-
-			ps = con.prepareStatement(SQL_INSERT_INTO_EXP_PIM_CALENDAR_USER);
-			ps.setLong(1, id_cal);
-			ps.setLong(2, Long.parseLong(userId)); // owner
-			ps.executeUpdate();
-
-			// Evento com repetiÃ§Ã£o
-			if (rp != null) {
-				addRecurrencePattern(rp, id_cal, sd);
-			}
-
-			// Evento com Alarme
-			if (reminder != null && reminder.isActive()) {
-				addReminder(reminder, id_cal);
-			}
-
-			con.commit();
-			con.setAutoCommit(true);
+//			datea1 = this.date1.getTime(); // 23
+//			datea2 = this.date2.getTime(); // 29
+//			dateb1 = dtstart.getTime(); // 28
+//
+//			if(dateb1 >= datea1 && dateb1 <= datea2){
+
+				rp          = c.getRecurrencePattern(); // Recebe a repetiÃ§Ã£o do alarme
+				reminder    = c.getReminder(); // Recebe o alarme do evento
+				body 		= stringFrom(c.getDescription()); // Descricao completa do evento
+				location 	= stringFrom(c.getLocation()); // Localizacao do evento
+				is_public 	= (stringFrom(c.getAccessClass())).equals("2") ? "0" : "1"; // Evento particular ou publico
+				subject 	= stringFrom(c.getSummary()); // Titulo do evento
+				category    = stringFrom(c.getCategories());
+
+				// Verifica o parametro uid necessario para o expresso
+				ps = con.prepareStatement("select config_value from phpgw_config WHERE config_name = 'hostname' AND config_app = 'phpgwapi'");
+				rs = ps.executeQuery();
+				String uid = rs.next() ? ("-@" + rs.getString(1)) : "";
+
+				// Verifica a categoria do evento
+				if(category != null && (!category.equals(""))){
+					category = addCategory(category);
+				}
+
+				// Prepara os dados para inserir o evento no banco de dados
+				ps = con.prepareStatement(SQL_INSERT_INTO_EXP_PIM_CALENDAR);
+				ps.setString(1, uid); // -@hostname
+				ps.setLong(2, Long.parseLong(userId)); // owner
+				ps.setString(3, category); // category
+				ps.setLong(4, new Long(Long.toString(dtstart.getTime()).substring(0, 10))); // datetime
+				ps.setLong(5, new Long(Long.toString(lastUpdate.getTime()).substring(0, 10))); // mdatetime
+				ps.setLong(6, new Long(Long.toString(dend.getTime()).substring(0, 10))); // edatetime
+				ps.setString(7, (rp == null) ? "E" : "M"); // cal_type
+				ps.setLong(8, Long.parseLong(is_public)); // is_plublic
+				ps.setString(9, truncate(subject, SQL_SUBJECT_DIM)); // title
+				ps.setString(10, truncate(body, SQL_BODY_DIM)); // body of event, a little description of it
+				ps.setString(11, truncate(location, SQL_LOCATION_DIM)); // location of the event
+				ps.setString(12, ""); // Participants outside
+				ps.setLong(13, lastUpdate.getTime()); // last update
+				ps.setString(14, String.valueOf(Def.PIM_STATE_NEW)); // status
+				ps.executeUpdate();
+
+				// Verifica o id maximo do evento inserido
+				ps = con.prepareStatement("select max(cal_id) from phpgw_cal limit 1");
+				rs = ps.executeQuery();
+
+				if (rs.next()) {
+					long id = rs.getInt(1);
+					id_cal = (id == 0) ? 1 : id;
+				}
+
+				cw.setId(Long.toString(id_cal));
+
+				ps = con.prepareStatement(SQL_INSERT_INTO_EXP_PIM_CALENDAR_USER);
+				ps.setLong(1, id_cal);
+				ps.setLong(2, Long.parseLong(userId)); // owner
+				ps.executeUpdate();
+
+				// Evento com repetiÃ§Ã£o
+				if (rp != null) {
+					addRecurrencePattern(rp, id_cal, sd);
+				}
+
+				// Evento com Alarme
+				if (reminder != null && reminder.isActive()) {
+					addReminder(reminder, id_cal);
+				}
+
+				con.commit();
+				con.setAutoCommit(true);
+
+//			}
 
 		} catch (Exception e) {
-			throw new PIMDBAccessException("Error adding a calendar item: "
-					+ e.getMessage());
+			throw new PIMDBAccessException("Error adding a calendar item: " + e.getMessage());
 		} finally {
 			DBTools.close(con, ps, rs);
@@ -810,4 +830,9 @@
 			dtStart = getDateFromString(stringFrom(c.getCalendarContent().getDtStart()));
 			dtEnd = getDateFromString(stringFrom(c.getCalendarContent().getDtEnd()));
+
+			long hj = System.currentTimeMillis();
+			if(dtStart.getTime() > hj){
+				return twins;
+			}
 
 			StringBuffer sqlGetCalendarTwinList = new StringBuffer(SQL_GET_EXP_PIM_CALENDAR_ID_LIST_BY_USER);
@@ -1437,3 +1462,41 @@
 	}
 
+	private void findDates(String source) throws Exception {
+
+		String aux = null;
+		StringTokenizer tokens = null;
+		String primeira = null;
+		String segunda = null;
+
+		int i = 0;
+		int j = 0;
+
+		java.util.Calendar calendar1 = null;
+		java.util.Calendar calendar2 = null;
+
+		try {
+
+			aux = source.substring(source.indexOf("(")+1, source.indexOf(")"));
+			tokens = new StringTokenizer(aux, ",");
+
+			primeira = tokens.nextToken();
+			segunda = tokens.nextToken();
+
+			i = Integer.parseInt(primeira);
+			j = Integer.parseInt(segunda);
+
+			calendar1 = java.util.Calendar.getInstance();
+			calendar2 = java.util.Calendar.getInstance();
+
+			calendar1.add(java.util.Calendar.DAY_OF_MONTH, i);
+			calendar2.add(java.util.Calendar.DAY_OF_MONTH, j);
+
+			this.date1 = new Timestamp(calendar1.getTimeInMillis());
+			this.date2 = new Timestamp(calendar2.getTimeInMillis());
+
+		} catch (Exception e) {
+			throw new PIMDBAccessException("Error convert dates findDates  " + e.getMessage());
+		}
+	}
+
 }
