/** * Creates a new Google Calendar Entry for each <code>item</code> and adds it to the processing * queue. The entries' title will either be the items name or <code>alias</code> if it is <code> * != null</code>. * * <p>The new Calendar Entry will contain a single command to be executed e.g.<br> * * <p><code>send <item.name> <item.state></code> * * @param item the item which state should be persisted. * @param alias the alias under which the item should be persisted. */ @Override public void store(final Item item, final String alias) { if (initialized) { String newAlias = alias != null ? alias : item.getName(); CalendarEventEntry myEntry = new CalendarEventEntry(); myEntry.setTitle(new PlainTextConstruct("[PresenceSimulation] " + newAlias)); myEntry.setContent( new PlainTextConstruct( String.format(executeScript, item.getName(), item.getState().toString()))); DateTime nowPlusOffset = new DateTime().plusDays(offset); com.google.gdata.data.DateTime time = com.google.gdata.data.DateTime.parseDateTime(nowPlusOffset.toString()); When eventTimes = new When(); eventTimes.setStartTime(time); eventTimes.setEndTime(time); myEntry.addTime(eventTimes); entries.offer(myEntry); logger.trace( "added new entry '{}' for item '{}' to upload queue", myEntry.getTitle().getPlainText(), item.getName()); } else { logger.debug( "GCal PresenceSimulation Service isn't initialized properly! No entries will be uploaded to your Google Calendar"); } }
@Override public void generate(XmlWriter w, ExtensionProfile extProfile) throws IOException { ArrayList<XmlWriter.Attribute> attrs = new ArrayList<XmlWriter.Attribute>(); if (label != null) { attrs.add(new XmlWriter.Attribute("label", label)); } if (lat != null) { attrs.add(new XmlWriter.Attribute("lat", lat.toString())); } if (lon != null) { attrs.add(new XmlWriter.Attribute("lon", lon.toString())); } if (elev != null) { attrs.add(new XmlWriter.Attribute("elev", elev.toString())); } if (time != null) { attrs.add(new XmlWriter.Attribute("time", time.toString())); } generateStartElement(w, Namespaces.gNs, "geoPt", attrs, null); // Invoke ExtensionPoint. generateExtensions(w, extProfile); w.endElement(Namespaces.gNs, "geoPt"); }
@Override public int hashCode() { int result = getClass().hashCode(); if (count != null) { result = 37 * result + count.hashCode(); } if (updated != null) { result = 37 * result + updated.hashCode(); } return result; }
public void accept(CalendarService calendarService) throws IOException, ServiceException { final URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/private/full"); final CalendarQuery calendarQuery = new CalendarQuery(feedUrl); calendarQuery.setMinimumStartTime(DateTime.parseDateTime("2011-04-16T00:00:00")); calendarQuery.setMaximumStartTime(DateTime.parseDateTime("2011-09-24T23:59:59")); final CalendarEventFeed resultFeed = calendarService.query(calendarQuery, CalendarEventFeed.class); int i = 0; for (final CalendarEventEntry eventEntry : resultFeed.getEntries()) { final StringBuilder s = new StringBuilder(); s.append("event #"); s.append(++i); s.append(": '"); s.append(eventEntry.getTitle().getPlainText()); s.append("', content: '"); s.append(eventEntry.getContent().toString()); s.append("'."); System.out.println(s.toString()); } }
@Override public void processAttribute(String namespace, String localName, String value) throws ParseException { if (namespace.equals("")) { if (localName.equals("label")) { label = value; } else if (localName.equals("lat")) { try { lat = Float.valueOf(value); } catch (NumberFormatException e) { throw new ParseException(CoreErrorDomain.ERR.invalidGeoPtLat, e); } } else if (localName.equals("lon")) { try { lon = Float.valueOf(value); } catch (NumberFormatException e) { throw new ParseException(CoreErrorDomain.ERR.invalidGeoPtLon, e); } } else if (localName.equals("elev")) { try { elev = Float.valueOf(value); } catch (NumberFormatException e) { throw new ParseException(CoreErrorDomain.ERR.invalidGeoPtElev, e); } } else if (localName.equals("time")) { try { time = DateTime.parseDateTime(value); } catch (NumberFormatException e) { throw new ParseException(CoreErrorDomain.ERR.invalidGeoPtTime); } } } }
/** * @param idc * @return -1 em caso de erro 0 : sucesso 1 : calendario nao existe 2 : credenciais invalidas */ int importEvent(String usernameEmp) { String idc = EWServer.dbm.companies.getIDC(usernameEmp); JSONObject empInfo = EWServer.dbm.companies.get(idc, 2); String username = ""; String password = ""; String nomeCalendario = ""; try { username = empInfo.getString("gc_username"); password = empInfo.getString("gc_password"); nomeCalendario = empInfo.getString("gc_nome"); } catch (JSONException e1) { // e1.printStackTrace(); return -1; } if (username.equals("") || password.equals("") || nomeCalendario.equals("")) { return -1; } String idCalendario = ""; try { URL feedUrlCalendarios = new URL("https://www.google.com/calendar/feeds/default"); CalendarService myService = new CalendarService("exampleCo-exampleApp-1"); myService.setUserCredentials(username, password); CalendarEventFeed myFeed = myService.getFeed(feedUrlCalendarios, CalendarEventFeed.class); for (int i = 0; i < myFeed.getEntries().size(); i++) { if (myFeed .getEntries() .get(i) .getTitle() .getPlainText() .toLowerCase() .equals(nomeCalendario.toLowerCase())) { String[] tokens = myFeed.getEntries().get(i).getId().toString().split("/"); idCalendario = tokens[tokens.length - 1]; break; } } if (idCalendario.equals("")) { System.out.println("Calendario do utilizador nao encontrado."); return 1; // calendario nao encontrado. } // data de hoje no formato certo String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); String time[] = sdf.format(cal.getTime()).split(" "); String startTime = time[0] + "T" + time[1]; URL feedUrl = new URL("https://www.google.com/calendar/feeds/" + idCalendario + "/private/full"); // query para mostrar eventos a partir de hoje! CalendarQuery myQuery = new CalendarQuery(feedUrl); myQuery.setMinimumStartTime(DateTime.parseDateTime(startTime)); // myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class); CalendarEventFeed resultFeed = myService.query(myQuery, CalendarEventFeed.class); String idEvento = ""; ArrayList<String> BDEventsID = findGcIds(usernameEmp); // percorrer os eventos for (int i = 0; i < resultFeed.getEntries().size(); i++) { String[] tokens = resultFeed.getEntries().get(i).getId().toString().split("/"); idEvento = tokens[tokens.length - 1]; // System.out.println("Idevento: "+BDEventsID.get(i)); // ver se o idEvento existe no if (BDEventsID.contains(idEvento)) { BDEventsID.remove(idEvento); } } // eliminar os eventos que ja nao estao no calendario for (int i = 0; i < BDEventsID.size(); i++) { delEvento(BDEventsID.get(i)); } ArrayList<String> BDEid = findGcIds(usernameEmp); // ver os eventos todos, se nao tiverem na base de dados adiciona, se tiverem e forem // alterados-> altera, se tiver e nao for alterado nao faz nada for (int i = 0; i < resultFeed.getEntries().size(); i++) { String[] tokens = resultFeed.getEntries().get(i).getId().toString().split("/"); idEvento = tokens[tokens.length - 1]; // se nao contem adiciona if (!BDEid.contains(idEvento)) { System.out.println("Nao tem evento, add."); String dataAlt = resultFeed.getEntries().get(i).getUpdated().toString(); String nomeEvnt = resultFeed.getEntries().get(i).getTitle().getPlainText(); String descEvnt = ""; if (resultFeed.getEntries().get(i).getPlainTextContent().equals("")) descEvnt = "Sem descricao."; else descEvnt = resultFeed.getEntries().get(i).getPlainTextContent(); String dInicio = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString(); // .split("T")[0]+" // "+resultFeed.getEntries().get(i).getTimes().get(0).getStartTime().toString().split("T")[1].split(":")[0]+"-"+resultFeed.getEntries().get(i).getTimes().get(0).getStartTime().toString().split("T")[1].split(":")[1]; String dFim = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString(); // .split("T")[0]+ " "+ // resultFeed.getEntries().get(i).getTimes().get(0).getEndTime().toString().split("T")[1].split(":")[0]+"-"+resultFeed.getEntries().get(i).getTimes().get(0).getEndTime().toString().split("T")[1].split(":")[1]; if (dInicio.contains("T")) { dFim = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString() .split("T")[0] + " " + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString() .split("T")[1] .split(":")[0] + "-" + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString() .split("T")[1] .split(":")[1]; dInicio = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString() .split("T")[0] + " " + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString() .split("T")[1] .split(":")[0] + "-" + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString() .split("T")[1] .split(":")[1]; } String onde = ""; if (resultFeed.getEntries().get(i).getLocations().get(0).getValueString().equals("")) onde = "Sem local definido."; else onde = resultFeed.getEntries().get(i).getLocations().get(0).getValueString(); addEvent(usernameEmp, idEvento, nomeEvnt, dataAlt, descEvnt, dInicio, dFim, onde); // adicionar a base de dados evento } else { // se contem if (resultFeed .getEntries() .get(i) .getUpdated() .toString() .equals(findDAlteracao(idEvento))) { // se nao foi alterado System.out.println("Ja existe."); continue; } else { // se foi alterado->alterar System.out.println("Alterar."); String dataAlt = resultFeed.getEntries().get(i).getUpdated().toString(); String nomeEvnt = resultFeed.getEntries().get(i).getTitle().getPlainText(); String descEvnt = ""; if (resultFeed.getEntries().get(i).getPlainTextContent().equals("")) descEvnt = "Sem descricao."; else descEvnt = resultFeed.getEntries().get(i).getPlainTextContent(); String dInicio = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString(); // .split("T")[0]+" // "+resultFeed.getEntries().get(i).getTimes().get(0).getStartTime().toString().split("T")[1].split(":")[0]+"-"+resultFeed.getEntries().get(i).getTimes().get(0).getStartTime().toString().split("T")[1].split(":")[1]; String dFim = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString(); // .split("T")[0]+ " "+ // resultFeed.getEntries().get(i).getTimes().get(0).getEndTime().toString().split("T")[1].split(":")[0]+"-"+resultFeed.getEntries().get(i).getTimes().get(0).getEndTime().toString().split("T")[1].split(":")[1]; if (dInicio.contains("T")) { dFim = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString() .split("T")[0] + " " + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString() .split("T")[1] .split(":")[0] + "-" + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getEndTime() .toString() .split("T")[1] .split(":")[1]; dInicio = resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString() .split("T")[0] + " " + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString() .split("T")[1] .split(":")[0] + "-" + resultFeed .getEntries() .get(i) .getTimes() .get(0) .getStartTime() .toString() .split("T")[1] .split(":")[1]; } String onde = ""; if (resultFeed.getEntries().get(i).getLocations().get(0).getValueString().equals("")) onde = "Sem local definido."; else onde = resultFeed.getEntries().get(i).getLocations().get(0).getValueString(); updateEvent(usernameEmp, idEvento, nomeEvnt, dataAlt, descEvnt, dInicio, dFim, onde); } } } System.out.println("Import feito com sucesso."); } catch (MalformedURLException e) { System.out.println("MalformedURLException Import Exception"); // e.printStackTrace(); return -1; } catch (AuthenticationException e) { System.out.println("Authentication Import Exception"); // e.printStackTrace(); return 2; } catch (IOException e) { System.out.println("IO Import Exception"); // e.printStackTrace(); return -1; } catch (ServiceException e) { System.out.println("Service Import Exception"); // e.printStackTrace(); return -1; } return 0; }