public static ContactService createDemoService() {
    if (instance == null) {

      final ContactService contactService = new ContactService();

      Random r = new Random(0);
      Calendar cal = Calendar.getInstance();
      for (int i = 0; i < 100; i++) {
        Contact contact = new Contact();
        contact.setFirstName(fnames[r.nextInt(fnames.length)]);
        contact.setLastName(lnames[r.nextInt(fnames.length)]);
        contact.setEmail(
            contact.getFirstName().toLowerCase()
                + "@"
                + contact.getLastName().toLowerCase()
                + ".com");
        contact.setPhone("+ 358 555 " + (100 + r.nextInt(900)));
        cal.set(1930 + r.nextInt(70), r.nextInt(11), r.nextInt(28));
        contact.setBirthDate(cal.getTime());
        contactService.save(contact);
      }

      instance = contactService;
    }

    return instance;
  }
  // Save data to output file
  public void saveDataToFile(String sFileName) {
    try {

      FileWriter writer = new FileWriter(sFileName, true);

      for (Contact entry : list) {
        writer.append(Integer.toString(entry.getiD()));
        writer.append(',');
        writer.append(entry.getFirstName());
        writer.append(',');
        writer.append(entry.getLastName());
        writer.append(',');
        writer.append(entry.getEmailAddress());
        writer.append(',');
        writer.append(Integer.toString(entry.getAge()));
        writer.append(',');
        writer.append(entry.getCellPhone());
        writer.append('\n');
      }
      writer.flush();
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * Handle HTTP GET method / Json
   *
   * @return a JSON list of contacts.
   */
  @Get("json")
  public Representation toJSON() {
    // System.out.println("Request Original Ref " + getOriginalRef());
    // System.out.println("Request Entity " + getRequest().getEntityAsText()
    // +
    // " entity mediaType " + getRequest().getEntity().getMediaType());

    try {
      JSONArray jcontacts = new JSONArray();
      Reference ref = getRequest().getResourceRef();
      final String baseURL = ref.getHierarchicalPart();
      Form formQuery = ref.getQueryAsForm();

      Iterator<Contact> it =
          getSortedContacts(formQuery.getFirstValue(REQUEST_QUERY_SORT, LAST_NAME)).iterator();

      while (it.hasNext()) {
        Contact contact = it.next();

        JSONObject jcontact = new JSONObject();
        jcontact.put(ID, String.format("%s", contact.getId()));
        jcontact.put(URL, String.format("%s/%s", baseURL, contact.getId()));
        jcontact.put(FIRST_NAME, contact.getFirstName());
        jcontact.put(LAST_NAME, contact.getLastName());
        jcontacts.put(jcontact);
      }

      JSONObject contacts = new JSONObject();
      contacts.put(CONTACTS, jcontacts);
      return new JsonRepresentation(contacts);
    } catch (Exception e) {
      setStatus(Status.SERVER_ERROR_INTERNAL);
      return null;
    }
  }
  /**
   * Handle HTTP GET Metod / xml
   *
   * @return an XML list of contacts.
   */
  @Get("xml")
  public Representation toXML() {
    // System.out.println("Request Original Ref " + getOriginalRef());
    // System.out.println("Request Entity " + getRequest().getEntityAsText()
    // +
    // " entity mediaType " + getRequest().getEntity().getMediaType());

    Reference ref = getRequest().getResourceRef();
    final String baseURL = ref.getHierarchicalPart();
    Form formQuery = ref.getQueryAsForm();

    try {
      DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML);

      Document d = representation.getDocument();
      Element elContacts = d.createElement(CONTACTS);
      d.appendChild(elContacts);

      Iterator<Contact> it =
          getSortedContacts(formQuery.getFirstValue(REQUEST_QUERY_SORT, LAST_NAME)).iterator();
      while (it.hasNext()) {
        Contact contact = it.next();

        Element el = d.createElement(CONTACT);

        Element id = d.createElement(ID);
        id.appendChild(d.createTextNode(String.format("%s", contact.getId())));
        el.appendChild(id);

        Element firstname = d.createElement(FIRST_NAME);
        firstname.appendChild(d.createTextNode(contact.getFirstName()));
        el.appendChild(firstname);

        Element lastname = d.createElement(LAST_NAME);
        lastname.appendChild(d.createTextNode(contact.getLastName()));
        el.appendChild(lastname);

        Element url = d.createElement(URL);
        url.appendChild(d.createTextNode(String.format("%s/%s", baseURL, contact.getId())));
        el.appendChild(url);

        elContacts.appendChild(el);
      }

      d.normalizeDocument();
      return representation;
    } catch (Exception e) {
      setStatus(Status.SERVER_ERROR_INTERNAL);
      return null;
    }
  }
 private void StartContactUpdateFragment() {
   if (contactLoaded != null) {
     ContactUpdateFragment contactUpdateFragment =
         ContactUpdateFragment.newInstance(
             new Contact(
                 contactLoaded.getId(),
                 contactLoaded.getFirstName(),
                 contactLoaded.getLastName(),
                 contactLoaded.getEmail()));
     getSupportFragmentManager()
         .beginTransaction()
         .replace(android.R.id.content, contactUpdateFragment)
         .commit();
   }
 }
 public void syncFbFriends(View v) {
   ArrayList<Contact> contacts = (ArrayList) MainActivity.db.dbGetAllContacts();
   GraphObjectList<GraphUser> fbFriends = FbLoginFragment.fbObjectList;
   URL url = null;
   for (GraphUser usr : fbFriends) {
     for (Contact contData : contacts) {
       if (contData.getFirstName() == usr.getFirstName()) {
         if (contData.getLastName() == usr.getLastName()) {
           //                        try {
           //                            url = new URL("http://graph.facebook.com/" + "");
           //                        } catch (MalformedURLException){
           //
           //                        }
         }
       }
     }
   }
 }
  public List<Contact> searchContacts(String[] keywords) {
    List<Contact> matches = new ArrayList<Contact>();
    if (keywords != null && keywords.length > 0) {
      outer:
      for (Contact contact : contacts.values()) {
        for (String keyword : keywords) {
          keyword = keyword.toLowerCase();
          if (!(contact.getFirstName().toLowerCase().contains(keyword)
              || contact.getLastName().toLowerCase().contains(keyword))) {
            // keyword not found on contact
            continue outer;
          }
        }
        matches.add(contact);
      }
    } else {
      matches.addAll(contacts.values());
    }

    return matches;
  }
Exemple #8
0
  public static ContactSoap toSoapModel(Contact model) {
    ContactSoap soapModel = new ContactSoap();

    soapModel.setContactId(model.getContactId());
    soapModel.setCompanyId(model.getCompanyId());
    soapModel.setUserId(model.getUserId());
    soapModel.setUserName(model.getUserName());
    soapModel.setCreateDate(model.getCreateDate());
    soapModel.setModifiedDate(model.getModifiedDate());
    soapModel.setClassNameId(model.getClassNameId());
    soapModel.setClassPK(model.getClassPK());
    soapModel.setAccountId(model.getAccountId());
    soapModel.setParentContactId(model.getParentContactId());
    soapModel.setEmailAddress(model.getEmailAddress());
    soapModel.setFirstName(model.getFirstName());
    soapModel.setMiddleName(model.getMiddleName());
    soapModel.setLastName(model.getLastName());
    soapModel.setPrefixId(model.getPrefixId());
    soapModel.setSuffixId(model.getSuffixId());
    soapModel.setMale(model.getMale());
    soapModel.setBirthday(model.getBirthday());
    soapModel.setSmsSn(model.getSmsSn());
    soapModel.setAimSn(model.getAimSn());
    soapModel.setFacebookSn(model.getFacebookSn());
    soapModel.setIcqSn(model.getIcqSn());
    soapModel.setJabberSn(model.getJabberSn());
    soapModel.setMsnSn(model.getMsnSn());
    soapModel.setMySpaceSn(model.getMySpaceSn());
    soapModel.setSkypeSn(model.getSkypeSn());
    soapModel.setTwitterSn(model.getTwitterSn());
    soapModel.setYmSn(model.getYmSn());
    soapModel.setEmployeeStatusId(model.getEmployeeStatusId());
    soapModel.setEmployeeNumber(model.getEmployeeNumber());
    soapModel.setJobTitle(model.getJobTitle());
    soapModel.setJobClass(model.getJobClass());
    soapModel.setHoursOfOperation(model.getHoursOfOperation());

    return soapModel;
  }
  // *****************************************************************************
  // Save Contact using HTTP PUT method with singleContactUrl.
  // If Id is zero(0) a new Contact will be added.
  // If Id is non-zero an existing Contact will be updated.
  // HTTP POST could be used to add a new Contact but the ContactService knows
  // an Id of zero means a new Contact so in this case the HTTP PUT is used.
  // *****************************************************************************
  public Integer SaveContact(Contact saveContact) {
    Integer statusCode = 0;
    HttpResponse response;

    try {
      boolean isValid = true;

      // Data validation goes here

      if (isValid) {

        // POST request to <service>/SaveVehicle
        HttpPut request = new HttpPut(singleContactUrl + saveContact.getId());
        request.setHeader("User-Agent", "dev.ronlemire.contactClient");
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        // Build JSON string
        JSONStringer contact =
            new JSONStringer()
                .object()
                .key("Id")
                .value(Integer.parseInt(saveContact.getId()))
                .key("FirstName")
                .value(saveContact.getFirstName())
                .key("LastName")
                .value(saveContact.getLastName())
                .key("Email")
                .value(saveContact.getEmail())
                .endObject();
        StringEntity entity = new StringEntity(contact.toString());

        request.setEntity(entity);

        // Send request to WCF service
        DefaultHttpClient httpClient = new DefaultHttpClient();
        response = httpClient.execute(request);

        Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());

        // statusCode =
        // Integer.toString(response.getStatusLine().getStatusCode());
        statusCode = response.getStatusLine().getStatusCode();

        if (saveContact.getId().equals("0")) {
          // New Contact.Id is in buffer
          HttpEntity responseEntity = response.getEntity();
          char[] buffer = new char[(int) responseEntity.getContentLength()];
          InputStream stream = responseEntity.getContent();
          InputStreamReader reader = new InputStreamReader(stream);
          reader.read(buffer);
          stream.close();
          statusCode = Integer.parseInt(new String(buffer));
        } else {
          statusCode = response.getStatusLine().getStatusCode();
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return statusCode;
  }
Exemple #10
0
 public static Contact createContact(String firstName, String lastName) {
   Contact contact = new Contact(firstName, lastName);
   String email = contact.getLastName() + (contactEmailNum++) + "@test.com";
   contact.setEmail(email.toLowerCase());
   return contact;
 }
Exemple #11
0
 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {
   Contact search = listContact.get(position);
   TextView textView = viewHolder.textView;
   textView.setText(search.getFirstName() + " " + search.getLastName());
 }
Exemple #12
0
  public static void main(String[] args) {
    Contact testContact = new Contact();
    String input = null; // variable for users input
    Scanner keyboard = new Scanner(System.in); // creates Scanner Object

    System.out.print("Enter the last name: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setLastName(input); // sends input to setLastName method to be validated

    System.out.println("Last name: " + testContact.getLastName()); // displays users input

    System.out.print("Enter the first name: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setFirstName(input); // sends input to setFirstName method to be validated

    System.out.println("First name: " + testContact.getFirstName()); // displays users input

    System.out.print("Enter the Middle name: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setMiddleName(input); // sends input to setMiddleName method to be validated

    System.out.println("Middle name: " + testContact.getMiddleName()); // displays users input

    System.out.print("Enter the prefix (if you have none, input none): ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setPrefix(input); // sends input to setPrefix method to be validated

    System.out.println("Prefix: " + testContact.getPrefix()); // displays users input

    System.out.print("Enter the phone number: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setPhoneNum(input); // sends input to setPhoneNum method to be validated

    System.out.println("Phone number: " + testContact.getPhoneNum()); // displays users input

    System.out.print("Enter the Email: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setEmail(input); // sends input to setEmail method to be validated

    System.out.println("Email: " + testContact.getEmail()); // displays users input

    System.out.print("Enter the Street: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setStreet(input); // sends input to setStreet method to be validated

    System.out.println("Street: " + testContact.getEmail()); // displays users input

    System.out.print("Enter the City: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setCity(input); // sends input to setCity method to be validated

    System.out.println("City: " + testContact.getCity()); // displays users input

    System.out.print("Enter the State: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setState(input); // sends input to setState method to be validated

    System.out.println("State: " + testContact.getState()); // displays users input

    System.out.print("Enter the Zip Code: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setZipCode(input); // sends input to setZipCode method to be validated

    System.out.println("Zip code: " + testContact.getZipCode()); // displays users input

    System.out.print("Enter the Occupation: ");
    input = keyboard.nextLine(); // changes value of input
    testContact.setOcupation(input); // sends input to setOccupation method to be validated

    System.out.println("Occupation: " + testContact.getOccupation()); // displays users input
  }
Exemple #13
0
 public int compareTo(Contact anotherContact) {
   // using CompareToIgnoreCase would be more robust, but using CompareTo is about 3x faster.
   return this.lastName.compareTo(anotherContact.getLastName());
 }
 public boolean equals(Contact contact) {
   return (this.getFirstName().equals(contact.getFirstName())
       && this.getLastName().equals(contact.getLastName())
       && this.getCellPhone() == contact.getCellPhone());
 }