public Set<Contact> getContacts(int... ids) { boolean isRealId = false; /** @param isRealId stores whether or not we found a contact with the id */ int offendingId = 0; /** @param offendingId stores an id that does not correspond to a real contact */ Set<Contact> setToReturn = new HashSet<Contact>(); if (contactSet.isEmpty()) { throw new NullPointerException("No Contacts in Contact Manager!"); } else { for (int id : ids) { for (Contact contact : contactSet) { if (id == contact.getId()) { isRealId = true; setToReturn.add(contact); break; } else isRealId = false; offendingId = id; } if (!isRealId) { throw new IllegalArgumentException("Contact with id " + offendingId + " does not exist"); } } return setToReturn; } }
@Test public void testAddMultipleContacts() throws Exception { groupService.create(group); groupService.addAggregation(committee, group); group = groupService.findById(group.getId()); contactService.addToGroup(topLevel, group); Contact anotherContact = new Contact(); anotherContact.setFirstName("Another"); anotherContact.setEmail("*****@*****.**"); contactService.create(anotherContact); contactService.addToGroup(anotherContact, group); group = groupService.findById(group.getId()); assertEquals(2, group.getTopLevelMembers().size()); anotherContact = contactService.findById(anotherContact.getId()); topLevel = contactService.findById(topLevel.getId()); assertEquals(1, anotherContact.getGroups().size()); assertEquals(1, topLevel.getGroups().size()); groupService.delete(group); anotherContact = contactService.findById(anotherContact.getId()); topLevel = contactService.findById(topLevel.getId()); assertEquals(0, anotherContact.getGroups().size()); assertEquals(0, topLevel.getGroups().size()); }
@Test @Transactional public void testRemoveContactFromEventMultipleGroups() throws Exception { groupService.create(group); groupService.addAggregation(event, group); Group secondGroup = new Group(); secondGroup.setGroupName("Second Group"); groupService.create(secondGroup); groupService.addAggregation(event, secondGroup); Contact newContact = new Contact(); newContact.setFirstName("Fresh Contact"); newContact.setEmail("Fresh email"); contactService.create(newContact); event = eventService.findById(event.getId()); contactService.attendEvent(newContact, event); newContact = contactService.findById(newContact.getId()); contactService.unattendEvent(newContact, event); event = eventService.findById(event.getId()); assertFalse(event.getAttendees().contains(newContact)); newContact = contactService.findById(newContact.getId()); assertFalse(newContact.getAttendedEvents().contains(event)); }
@Test public void tooLongSender() throws Exception { Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID); Map<String, Object> fields = new HashMap<String, Object>(); fields.put(ContactConstants.A_firstName, Strings.repeat("F", 129)); Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null); DbConnection conn = DbPool.getConnection(mbox); Assert.assertEquals( Strings.repeat("F", 128), DbUtil.executeQuery( conn, "SELECT sender FROM mboxgroup1.mail_item WHERE mailbox_id = ? AND id = ?", mbox.getId(), contact.getId()) .getString(1)); fields.put(ContactConstants.A_firstName, null); fields.put(ContactConstants.A_lastName, Strings.repeat("L", 129)); mbox.modifyContact(null, contact.getId(), new ParsedContact(fields)); Assert.assertEquals( Strings.repeat("L", 128), DbUtil.executeQuery( conn, "SELECT sender FROM mboxgroup1.mail_item WHERE mailbox_id = ? AND id = ?", mbox.getId(), contact.getId()) .getString(1)); conn.closeQuietly(); }
@Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); Contact contact = ((ContactAdapter) getListAdapter()).getItem(info.position); switch (item.getItemId()) { case R.id.profile: Intent profileIntent = new Intent(getBaseContext(), ContactViewActivity.class); profileIntent.putExtra("ContactID", contact.getId()); startActivity(profileIntent); return true; case R.id.edit: Intent editIntent = new Intent(getBaseContext(), ContactEditActivity.class); editIntent.putExtra("ContactID", contact.getId()); startActivity(editIntent); return true; case R.id.delete: // TODO Maybe a confirmation??? ContactRepositoryInterface datasource = ContactRepositoryFactory.getInstance().getContactRepository(this, this); this.contact_adapter.remove(contact); datasource.open(); datasource.delete(contact); datasource.close(); refreshList(); return true; default: return super.onContextItemSelected(item); } }
/** * 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; } }
/** * This a method to test if a Contact is in a particular Set of contacts. This is to be leveraged * in the getFutureMeeting(Contact contact) method as well as the getPastMeetingListFor(Contact * contact) methods. */ public boolean contactInMeeting(Set<Contact> participants, Contact contact) { boolean result = false; for (Contact eachcontact : participants) { if (eachcontact.getId() == contact.getId()) { result = true; } } return result; }
public synchronized void save(Contact entry) { if (entry.getId() == null) { entry.setId(nextId++); } try { entry = (Contact) BeanUtils.cloneBean(entry); } catch (Exception ex) { throw new RuntimeException(ex); } contacts.put(entry.getId(), entry); }
/** * 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; } }
public boolean add(Contact contact) { int[] array = new int[contactVector.length + contactVector.length]; if (isMember(contact.getId())) return false; if (contactVector[contactVector.length - 1] != null) { System.arraycopy(contactVector, 0, array, 0, contactVector.length - 1); } }
public void testLoadContactReturnDirectoryContactWithoutDisplayName() throws JSONException { // Use lookup-style Uri that contains encoded json object which encapsulates the // directory contact. The test json object is: // { // display_name_source": 40, // "vnd.android.cursor.item\/contact":{"email":{"data1":"*****@*****.**" }} // } JSONObject itemJson = new JSONObject(); itemJson.put("email", new JSONObject().put("data1", "*****@*****.**")); JSONObject json = new JSONObject(); json.put(Contacts.NAME_RAW_CONTACT_ID, CONTACT_ID); json.put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.STRUCTURED_NAME); json.put(Contacts.CONTENT_ITEM_TYPE, itemJson); final Uri lookupUri = Contacts.CONTENT_LOOKUP_URI .buildUpon() .encodedFragment(json.toString()) .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, "1") .appendPath(Constants.LOOKUP_URI_ENCODED) .build(); mContactsProvider.expectTypeQuery(lookupUri, Contacts.CONTENT_ITEM_TYPE); Contact contact = assertLoadContact(lookupUri); assertEquals(-1, contact.getId()); assertEquals(-1, contact.getNameRawContactId()); assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource()); assertEquals("", contact.getDisplayName()); assertEquals(lookupUri, contact.getLookupUri()); assertEquals(1, contact.getRawContacts().size()); mContactsProvider.verify(); }
public void testLoadContactWithContactLookupWithIncorrectIdUri() { // Use lookup-style Uris that contain incorrect Contact-ID // (we want to ensure that still the correct contact is chosen) final long wrongContactId = 2; final long wrongRawContactId = 12; final String wrongLookupKey = "ab%12%@!"; final Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, CONTACT_ID); final Uri wrongBaseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, wrongContactId); final Uri lookupUri = ContentUris.withAppendedId( Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, LOOKUP_KEY), CONTACT_ID); final Uri lookupWithWrongIdUri = ContentUris.withAppendedId( Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, LOOKUP_KEY), wrongContactId); final Uri entityUri = Uri.withAppendedPath(lookupWithWrongIdUri, Contacts.Entity.CONTENT_DIRECTORY); ContactQueries queries = new ContactQueries(); mContactsProvider.expectTypeQuery(lookupWithWrongIdUri, Contacts.CONTENT_ITEM_TYPE); queries.fetchAllData(entityUri, CONTACT_ID, RAW_CONTACT_ID, DATA_ID, LOOKUP_KEY); Contact contact = assertLoadContact(lookupWithWrongIdUri); assertEquals(CONTACT_ID, contact.getId()); assertEquals(RAW_CONTACT_ID, contact.getNameRawContactId()); assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource()); assertEquals(LOOKUP_KEY, contact.getLookupKey()); assertEquals(lookupUri, contact.getLookupUri()); assertEquals(1, contact.getRawContacts().size()); assertEquals(1, contact.getStatuses().size()); mContactsProvider.verify(); }
public void testLoadContactWithRawContactIdUri() { // Use content Uris that only contain the ID but use the format used in Donut final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, RAW_CONTACT_ID); final Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, CONTACT_ID); final Uri lookupUri = ContentUris.withAppendedId( Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, LOOKUP_KEY), CONTACT_ID); final Uri entityUri = Uri.withAppendedPath(lookupUri, Contacts.Entity.CONTENT_DIRECTORY); ContactQueries queries = new ContactQueries(); mContactsProvider.expectTypeQuery(rawContactUri, RawContacts.CONTENT_ITEM_TYPE); queries.fetchContactIdAndLookupFromRawContactUri(rawContactUri, CONTACT_ID, LOOKUP_KEY); queries.fetchAllData(entityUri, CONTACT_ID, RAW_CONTACT_ID, DATA_ID, LOOKUP_KEY); Contact contact = assertLoadContact(rawContactUri); assertEquals(CONTACT_ID, contact.getId()); assertEquals(RAW_CONTACT_ID, contact.getNameRawContactId()); assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource()); assertEquals(LOOKUP_KEY, contact.getLookupKey()); assertEquals(lookupUri, contact.getLookupUri()); assertEquals(1, contact.getRawContacts().size()); assertEquals(1, contact.getStatuses().size()); mContactsProvider.verify(); }
private void setContact() { Contact contact = new Contact(id); firstName = contact.getName(); surname = contact.getSurname(); designation = contact.getDesignation(); phone = new Phone(contact.getId()).getNumber(); }
// ***************************************************************************** // Delete Contact using HTTP DELETE method with singleContactUrl. // ***************************************************************************** public Integer DeleteContact(Contact deleteContact) { Integer statusCode = 0; HttpResponse response; try { boolean isValid = true; // Data validation goes here if (isValid) { HttpDelete request = new HttpDelete(singleContactUrl + deleteContact.getId()); request.setHeader("User-Agent", "dev.ronlemire.contactClient"); request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json"); // Send request to WCF service DefaultHttpClient httpClient = new DefaultHttpClient(); response = httpClient.execute(request); Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode()); statusCode = response.getStatusLine().getStatusCode(); } } catch (Exception e) { e.printStackTrace(); } return statusCode; }
public Contact findById(int id) { for (Contact contact : contacts) { if (contact.getId() == id) return contact; } return null; }
@Test @Transactional public void testAddContactToGroupAndGroupConstituent() throws Exception { groupService.create(group); groupService.addAggregation(committee, group); Contact contact = new Contact(); contact.setFirstName("Test Contact"); contact.setEmail("*****@*****.**"); contactService.create(contact); contactService.addContactToCommittee(contact, committee); contactService.addToGroup(contact, group); contact = contactService.findById(contact.getId()); assertTrue(contact.getGroups().contains(group)); assertTrue(contact.getCommittees().contains(committee)); committee = committeeService.findById(committee.getId()); assertTrue(committee.getMembers().contains(contact)); group = groupService.findById(group.getId()); assertTrue(group.getTopLevelMembers().contains(contact)); }
@Test @Transactional public void testAddContactToOrganizationMultipleGroups() throws Exception { groupService.create(group); groupService.addAggregation(organization, group); Group secondGroup = new Group(); secondGroup.setGroupName("Second Group"); groupService.create(secondGroup); groupService.addAggregation(organization, secondGroup); Contact newContact = new Contact(); newContact.setFirstName("Fresh Contact"); newContact.setEmail("Fresh email"); contactService.create(newContact); contactService.addContactToOrganization(newContact, organization); newContact = contactService.findById(newContact.getId()); assertTrue(newContact.getOrganizations().contains(organization)); group = groupService.findById(group.getId()); assertTrue(group.getAggregations().contains(organization)); secondGroup = groupService.findById(secondGroup.getId()); assertTrue(secondGroup.getAggregations().contains(organization)); organization = organizationService.findById(organization.getId()); assertTrue(organization.getMembers().contains(newContact)); }
public void deleteContact(Contact contact) { SQLiteDatabase db = this.getWritableDatabase(); db.delete( CONTACTS_TABLE_NAME, CONTACTS_COLUMN_ID + " = ?", new String[] {String.valueOf(contact.getId())}); db.close(); }
// This method allows to get the item associated to a particular id, // uniquely generated by the method getId defined below public static Contact getItem(int id) { for (Contact item : CONTACTS) { if (item.getId() == id) { return item; } } return null; }
/** * This an auxiliary method to test if all Set<Contacts> are in the contact list. If they all are, * then return is true, if not return is false. */ public boolean validContact(Set<Contact> contacts) { boolean result = true; for (Contact eachContact : contacts) { int eachContactid = eachContact.getId(); if (eachContactid > 0 && eachContactid <= contactidcount) { } else { result = false; } } return result; }
@Test @Transactional public void testAddContactToMultipleGroupsMultipleConstituents() throws Exception { groupService.create(group); groupService.addAggregation(committee, group); groupService.addAggregation(event, group); Group secondGroup = new Group(); secondGroup.setGroupName("Second Group"); groupService.create(secondGroup); groupService.addAggregation(committee, secondGroup); groupService.addAggregation(event, secondGroup); Contact contact = new Contact(); contact.setFirstName("Test Contact"); contact.setEmail("*****@*****.**"); contactService.create(contact); contactService.addContactToCommittee(contact, committee); contactService.attendEvent(contact, event); contactService.addToGroup(contact, group); contactService.addToGroup(contact, secondGroup); contact = contactService.findById(contact.getId()); group = groupService.findById(group.getId()); secondGroup = groupService.findById(secondGroup.getId()); event = eventService.findById(event.getId()); committee = committeeService.findById(committee.getId()); assertTrue(contact.getGroups().contains(group)); assertTrue(contact.getGroups().contains(secondGroup)); assertTrue(contact.getCommittees().contains(committee)); assertTrue(contact.getAttendedEvents().contains(event)); assertTrue(event.getAttendees().contains(contact)); assertTrue(event.getGroups().contains(group)); assertTrue(event.getGroups().contains(secondGroup)); assertTrue(committee.getMembers().contains(contact)); assertTrue(committee.getGroups().contains(group)); assertTrue(committee.getGroups().contains(secondGroup)); assertTrue(group.getTopLevelMembers().contains(contact)); assertTrue(group.getAggregations().contains(committee)); assertTrue(group.getAggregations().contains(event)); assertTrue(secondGroup.getTopLevelMembers().contains(contact)); assertTrue(secondGroup.getAggregations().contains(committee)); assertTrue(secondGroup.getAggregations().contains(event)); }
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 addContact(Contact contact) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(CONTACTS_COLUMN_CONTACT_ID, contact.getId()); values.put(CONTACTS_COLUMN_NAME, contact.getName()); // Contact Name values.put(CONTACTS_COLUMN_PHONE, contact.getPhone()); // Contact Phone // Number values.put(CONTACTS_COLUMN_EMAIL, contact.getEmail()); values.put(CONTACTS_COLUMN_CATEGORY, contact.getCategory()); // Inserting Row db.insert(CONTACTS_TABLE_NAME, null, values); db.close(); // Closing database connection }
public int updateContact(Contact contact) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(CONTACTS_COLUMN_NAME, contact.getName()); values.put(CONTACTS_COLUMN_PHONE, contact.getPhone()); values.put(CONTACTS_COLUMN_EMAIL, contact.getEmail()); values.put(CONTACTS_COLUMN_CATEGORY, contact.getCategory()); // updating row return db.update( CONTACTS_TABLE_NAME, values, CONTACTS_COLUMN_ID + " = ?", new String[] {String.valueOf(contact.getId())}); }
@Test public void testRemoveContactFromGroup() throws Exception { groupService.create(group); groupService.addAggregation(committee, group); contactService.addToGroup(topLevel, group); assertEquals(1, group.getTopLevelMembers().size()); contactService.removeFromGroup(topLevel, group); Group fromDb = groupService.findById(group.getId()); assertEquals(0, fromDb.getTopLevelMembers().size()); Contact contactFromDb = contactService.findById(topLevel.getId()); assertEquals(0, contactFromDb.getGroups().size()); }
@Test public void testDeleteGroup() throws Exception { groupService.create(group); groupService.addAggregation(committee, group); group = groupService.findById(group.getId()); contactService.addToGroup(topLevel, group); groupService.delete(group); Group groupFromDb = groupService.findById(group.getId()); assertNull(groupFromDb); Aggregation fromDb = committeeService.findById(committee.getId()); assertNotNull(fromDb); assertEquals(0, fromDb.getGroups().size()); assertEquals(committee.getAggregationMembers().size(), fromDb.getAggregationMembers().size()); Contact topLevelFromDb = contactService.findById(topLevel.getId()); assertNotNull(topLevelFromDb); assertEquals(0, topLevelFromDb.getGroups().size()); }
@Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View item = inflater.inflate(R.layout.contact_list_item, parent, false); Contact contact = getItem(position); ((TextView) item.findViewById(R.id.item_name)).setText(contact.getName()); ((TextView) item.findViewById(R.id.item_title)).setText(contact.getTitle()); // Check if we have gravatar on disk String filename = contact.getId() + "-gravatar.jpg"; try { File imgFile = getFileStreamPath(filename); if (imgFile.exists()) { ImageView iv = (ImageView) item.findViewById(R.id.item_profile_image); Bitmap gravatar = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); iv.setImageBitmap(gravatar); } } catch (Exception e) { Log.e("gravatar", e.getMessage()); } return item; }
// ***************************************************************************** // 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; }
public synchronized void delete(Contact value) { contacts.remove(value.getId()); }