public void addContact(Contact c, String[] groups) {
    try {
      // Create the entry to insert
      ContactEntry contact = new ContactEntry();
      Name name = new Name();
      final String NO_YOMI = null;
      name.setFullName(new FullName(c.getName(), NO_YOMI));
      name.setGivenName(new GivenName(c.getName(), NO_YOMI));
      name.setFamilyName(new FamilyName(c.getSurname(), NO_YOMI));
      contact.setName(name);

      Email primaryMail = new Email();
      primaryMail.setAddress(c.getEmail());

      primaryMail.setPrimary(true);
      contact.addEmailAddress(primaryMail);

      URL postUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
      try {
        myService.insert(postUrl, contact);
      } catch (IOException ex) {
        Logger.getLogger(ContactsHandlerImpl.class.getName()).log(Level.SEVERE, null, ex);
      } catch (ServiceException ex) {
        Logger.getLogger(ContactsHandlerImpl.class.getName()).log(Level.SEVERE, null, ex);
      }
    } catch (MalformedURLException ex) {
      Logger.getLogger(ContactsHandlerImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
Example #2
0
  public void importAllContacts() throws IOException, ServiceException {
    // import contacts
    setService();

    URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/[email protected]/full");
    Query myQuery = new Query(feedUrl);
    myQuery.setMaxResults(10);

    ContactFeed resultFeed = myService.query(myQuery, ContactFeed.class);
    // Print the results
    System.out.println(resultFeed.getTitle().getPlainText());
    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
      ContactEntry entry = resultFeed.getEntries().get(i);
      // Determine if this contact entry already exists in database (check Google ID):
      System.out.println(entry.getEtag());
      System.out.println(entry.getId());
      // if individual exists with Google ID, check to see if that invididual has all the emails
      // listed:
      //   People tryPerosn = peopleRepo.findByPropertyValue("googleId", "googleId", entry.getId());
      if (peopleRepo.findByPropertyValue("googleId", "googleId", entry.getEtag()) != null) {

        System.out.println(
            peopleRepo
                    .findByPropertyValue("googleId", "googleId", entry.getId())
                    .getNodeId()
                    .toString()
                + " He exists!");
      } else {
        importContact(entry);
      }
      entry.getEmailAddresses();
    }

    // If entry exists, check to see if entry
  }
Example #3
0
  public People importContact(ContactEntry entry) {
    People newContact = new People();
    counter++;
    System.out.println(counter);
    //  System.out.println(entry.getName().getFullName().getValue().toString().isEmpty());
    if (entry.hasName() == true) {
      if (entry.getBirthday() != null) {

        try {
          Date bDate =
              new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(entry.getBirthday().getWhen());
          newContact.setBirthdate(bDate);
        } catch (ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      //  newContact.setAge()
      if (entry.getName().getFullName().getValue().toString() != null) {
        System.out.println(entry.getName().getFullName().getValue().toString());
        newContact.setDisplayName(entry.getName().getFullName().getValue().toString());
      }

      if (entry.getName().getGivenName() != null) {
        newContact.setFirstName(entry.getName().getGivenName().getValue().toString());
        System.out.println(entry.getName().getGivenName().getValue().toString());
      }

      if (entry.getName().getFamilyName() != null) {
        newContact.setLastName(entry.getName().getFamilyName().getValue());
        System.out.println(entry.getName().getFamilyName().getValue());
      }
      newContact.setGoogleId(entry.getId());

      People commitContact = pplServ.createEntity(newContact);

      System.out.println(commitContact.getNodeId().toString());

      Iterator<com.google.gdata.data.extensions.Email> emailList =
          entry.getEmailAddresses().iterator();
      while (emailList.hasNext()) {
        com.google.gdata.data.extensions.Email thisEmail = emailList.next();
        System.out.println(thisEmail.getAddress());
        if (emailRepo.findByPropertyValue("email", "email", thisEmail.getAddress()) == null) {
          System.out.println(thisEmail.getAddress());
          Email newEmail = new Email(thisEmail.getAddress());
          Email commitEmail = pplServ.createEntity(newEmail);
          commitContact.setAddressOf(commitEmail);
          pplServ.createEntity(commitContact);
        }
      }

      Iterator<com.google.gdata.data.extensions.PhoneNumber> numbers =
          entry.getPhoneNumbers().iterator();
      while (numbers.hasNext()) {
        com.google.gdata.data.extensions.PhoneNumber thisNum = numbers.next();
        System.out.println(thisNum.getPhoneNumber());
        System.out.println(thisNum.getLabel());
        if (numRepo.findByPropertyValue("phoneNum", "phoneNum", thisNum.getPhoneNumber()) == null) {
          com.clarknoah.neo.domain.PhoneNumber newPhone =
              new com.clarknoah.neo.domain.PhoneNumber(
                  thisNum.getPhoneNumber(), thisNum.getLabel());
          com.clarknoah.neo.domain.PhoneNumber commitNum = pplServ.createEntity(newPhone);
          //	 commitNum.setPhoneNumberOf(commitContact);
          commitContact.setPhoneNum(commitNum);
          pplServ.createEntity(commitNum);
        }
      }
    }
    // newContact.setAddressOf(entry.)

    return newContact;
  }
Example #4
0
  public void printAllContacts() throws ServiceException, IOException {
    // Request the feed

    setService();
    URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/[email protected]/full");
    Query myQuery = new Query(feedUrl);
    myQuery.setMaxResults(8);
    myQuery.setStringCustomParameter("group", "Business Contacts");
    ContactFeed resultFeed = myService.query(myQuery, ContactFeed.class);
    // Print the results
    System.out.println(resultFeed.getTitle().getPlainText());
    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
      ContactEntry entry = resultFeed.getEntries().get(i);

      Name test = entry.getName();
      FullName namee = test.getFullName();
      People person =
          peopleRepo.save(
              new People(23, entry.getTitle().getPlainText(), entry.getTitle().getPlainText()));
      System.out.println(person.getNodeId().toString());
      System.out.println("\t" + entry.getTitle().getPlainText());

      System.out.println("Email addresses:");
      for (com.google.gdata.data.extensions.Email email : entry.getEmailAddresses()) {
        System.out.print(" " + email.getAddress());
        if (email.getRel() != null) {
          System.out.print(" rel:" + email.getRel());
        }
        if (email.getLabel() != null) {
          System.out.print(" label:" + email.getLabel());
        }
        if (email.getPrimary()) {
          System.out.print(" (primary) ");
        }
        System.out.print("\n");
      }

      System.out.println("IM addresses:");
      for (Im im : entry.getImAddresses()) {
        System.out.print(" " + im.getAddress());
        if (im.getLabel() != null) {
          System.out.print(" label:" + im.getLabel());
        }
        if (im.getRel() != null) {
          System.out.print(" rel:" + im.getRel());
        }
        if (im.getProtocol() != null) {
          System.out.print(" protocol:" + im.getProtocol());
        }
        if (im.getPrimary()) {
          System.out.print(" (primary) ");
        }
        System.out.print("\n");
      }

      System.out.println("Groups:");
      for (GroupMembershipInfo group : entry.getGroupMembershipInfos()) {
        String groupHref = group.getHref();
        System.out.println("  Id: " + groupHref);
      }

      System.out.println("Extended Properties:");
      for (ExtendedProperty property : entry.getExtendedProperties()) {
        if (property.getValue() != null) {
          System.out.println("  " + property.getName() + "(value) = " + property.getValue());
        } else if (property.getXmlBlob() != null) {
          System.out.println(
              "  " + property.getName() + "(xmlBlob)= " + property.getXmlBlob().getBlob());
        }
      }

      String photoLink = entry.getContactPhotoLink().getHref();
      System.out.println("Photo Link: " + photoLink);

      System.out.println("Contact's ETag: " + entry.getEtag());
    }
  }
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    UserService userService = UserServiceFactory.getUserService();

    RequestDispatcher disp = null;
    if (userService.isUserLoggedIn()) {
      User user = userService.getCurrentUser();

      PersistenceManager pm = PMF.get();
      ContactsToken token = null;
      try {
        token = pm.getObjectById(ContactsToken.class, user.getEmail());

        String teamName = req.getParameter("teamName");
        if (teamName != null) {
          Team team = TeamDAO.getTeam(teamName);

          if (team != null) {

            // Get contacts
            GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
            oauthParameters.setOAuthConsumerKey("anonymous");
            oauthParameters.setOAuthConsumerSecret("anonymous");
            oauthParameters.setOAuthToken(token.getToken());
            oauthParameters.setOAuthTokenSecret(token.getTokenSecret());
            OAuthSigner signer = new OAuthHmacSha1Signer();

            ContactsService service = new ContactsService("ContactFeedDemo");
            List<ContactEntry> result = null;
            List<String> mails = null;
            try {
              service.setOAuthCredentials(oauthParameters, signer);
              ContactFeed resultFeed =
                  service.getFeed(
                      new URL("https://www.google.com/m8/feeds/contacts/default/full"),
                      ContactFeed.class);

              result = new ArrayList<ContactEntry>(resultFeed.getEntries());
              mails = new ArrayList<String>();
              for (ContactEntry ce : result) {
                for (Email em : ce.getEmailAddresses()) {
                  mails.add(em.getAddress());
                }
              }
              for (TMember tm : team.getMembers()) {
                mails.remove(tm.getName());
              }

            } catch (OAuthException e) {
              e.printStackTrace();
            } catch (ServiceException e) {
              e.printStackTrace();
            }
            // end get contact list

            req.setAttribute("teamName", teamName);
            // req.setAttribute("teamMembers", teamMembers);
            req.setAttribute("contacts", mails);
            RequestDispatcher rd = req.getRequestDispatcher("/AddMember.jsp");
            rd.forward(req, resp);

          } else {
            String errText = "team with team name " + teamName + "doesnt exist.";
            req.setAttribute("err", errText);
            disp = req.getRequestDispatcher("err.jsp");
            disp.forward(req, resp);
          }

        } else {
          String errText = "parametru teamName is null";
          req.setAttribute("err", errText);
          disp = req.getRequestDispatcher("err.jsp");
          disp.forward(req, resp);
        }

      } catch (JDOObjectNotFoundException e) {
        String consumerKey = "anonymous";
        String consumerSecret = "anonymous";
        String scope = "https://www.google.com/m8/feeds";
        String callback = "http://vrchlpet-pc.appspot.com/callbackservlet";

        GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
        oauthParameters.setOAuthConsumerKey(consumerKey);
        oauthParameters.setOAuthConsumerSecret(consumerSecret);
        oauthParameters.setScope(scope);
        oauthParameters.setOAuthCallback(callback);

        OAuthSigner signer = new OAuthHmacSha1Signer();
        GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);

        try {
          oauthHelper.getUnauthorizedRequestToken(oauthParameters);
          String approvalPageUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);
          req.getSession().setAttribute("tokenSecret", oauthParameters.getOAuthTokenSecret());
          resp.sendRedirect(approvalPageUrl);
          return;
        } catch (OAuthException ee) {
          ee.printStackTrace();
        }
      }

    } else {
      disp = req.getRequestDispatcher("/projectcontrol");
      disp.forward(req, resp);
    }
  }