if (getAddresses() != null && !getAddresses().isEmpty()) { for (PersonAddress addr : getAddresses()) { if (addr.getPreferred() && !addr.getVoided()) { return addr; for (PersonAddress addr : getAddresses()) { if (!addr.getVoided()) { return addr; return getAddresses().iterator().next();
for (PersonAddress address : person.getAddresses()) { if (address.getDateCreated() == null) { sessionFactory.getCurrentSession().evict(address);
/** * This test verifies that {@link PersonAddress}es are fetched correctly from the hibernate * cache. (Or really, not fetched from the cache but instead are mapped with lazy=false. For * some reason Hibernate isn't able to find objects in the cache if a parent object was the one * that loaded them) * * @throws Exception */ @Test public void shouldFetchAddressesForPersonsThatWereFirstFetchedAsPatients() throws Exception { Person person = Context.getPersonService().getPerson(2); Patient patient = Context.getPatientService().getPatient(2); patient.getAddresses().size(); person.getAddresses().size(); }
private void setPreferredPersonAddress(Person person) { PersonAddress preferredAddress = null; PersonAddress possiblePreferredAddress = person.getPersonAddress(); if (possiblePreferredAddress != null && possiblePreferredAddress.getPreferred() && !possiblePreferredAddress.getVoided()) { preferredAddress = possiblePreferredAddress; } for (PersonAddress address : person.getAddresses()) { if (preferredAddress == null && !address.getVoided()) { address.setPreferred(true); preferredAddress = address; continue; } if (!address.equals(preferredAddress)) { address.setPreferred(false); } } }
assertNotNull(p.getAddresses()); assertTrue("There should be 1 address in the person object but there is actually : " + p.getAddresses().size(), p .getAddresses().size() == 1); assertTrue("There should be 1 address in the person object but there is actually : " + p.getAddresses().size(), p .getAddresses().size() == 1); assertTrue("There should be 2 addresses in the person object but there is actually : " + p.getAddresses().size(), p .getAddresses().size() == 2); assertTrue("There should be 2 addresses in the person object but there is actually : " + p.getAddresses().size(), p .getAddresses().size() == 2); assertTrue("There should be 3 addresses in the person object but there is actually : " + p.getAddresses().size(), p .getAddresses().size() == 3); assertTrue("There should be only 2 address in the person object now", p.getAddresses().size() == 2); assertTrue("There should be 3 addresses in the person object but there is actually : " + p.getAddresses().size(), p .getAddresses().size() == 3); assertTrue("There should be only 2 address in the person object now", p.getAddresses().size() == 2); p.removeAddress(pa2); assertTrue("There should be only 1 address in the person object now", p.getAddresses().size() == 1); p.removeAddress(pa2); assertTrue("There should still be only 1 address in the person object now", p.getAddresses().size() == 1); p.removeAddress(pa1); assertTrue("There shouldn't be any addresses in the person object now", p.getAddresses().size() == 0);
if (person.getAddresses() != null && !person.getAddresses().isEmpty()) { for (PersonAddress pAddress : person.getAddresses()) { if (pAddress.isBlank()){ person.removeAddress(pAddress);
/** * @see Person#addAddress(PersonAddress) */ @Test public void addAddress_shouldNotAddAPersonAddressWithBlankFields() { Person p = new Person(); PersonAddress pa1 = new PersonAddress(); pa1.setAddress1("address1"); p.addAddress(pa1); PersonAddress pa2 = new PersonAddress(); pa2.setAddress1(""); p.addAddress(pa2); Assert.assertEquals(1, p.getAddresses().size()); }
for (PersonAddress address : person.getAddresses()) { try { errors.pushNestedPath("addresses[" + index + "]");
/** * @see PersonSaveHandler#handle(Person,User, Date,String) */ @Test public void handle_shouldIgnoreBlankAddresses() { PersonSaveHandler handler = new PersonSaveHandler(); Person person = new Person(); PersonName personName = new PersonName("John","","Smith"); person.addName(personName); person.setGender("M"); PersonAddress personAddress = new PersonAddress(); personAddress.setAddress1(" "); person.addAddress(personAddress); handler.handle(person,null,null,null); Assert.assertEquals(0,person.getAddresses().size()); } }
private static String getAddress(Person person) { String address = null; final Set<PersonAddress> addresses = person.getAddresses(); if (!addresses.isEmpty()) { address = addresses.iterator().next().getAddress1(); } return address; }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource#doGetAll(java.lang.Object, * org.openmrs.module.webservices.rest.web.RequestContext) */ @Override public NeedsPaging<PersonAddress> doGetAll(Person parent, RequestContext context) throws ResponseException { //We don't return voided addresses List<PersonAddress> nonVoidedAddresses = new ArrayList<PersonAddress>(parent.getAddresses().size()); for (PersonAddress personAddress : parent.getAddresses()) { if (!personAddress.isVoided()) nonVoidedAddresses.add(personAddress); } return new NeedsPaging<PersonAddress>(nonVoidedAddresses, context); }
/** * Returns non-voided addresses for a person * * @param instance * @return */ @PropertyGetter("addresses") public static Set<PersonAddress> getAddresses(Person instance) { return RestUtil.removeVoidedData(instance.getAddresses()); }
addresses = person.getAddresses(); names = person.getNames(); attributes = person.getAttributes();
/** * Sets addresses and marks the first one as preferred if none is marked. It also makes sure * that only one address is marked as preferred and changes the rest to non-preferred. * <p/> * It takes the list so that the order is preserved. * * @param instance * @param addresses */ @PropertySetter("addresses") public static void setAddresses(Person instance, List<PersonAddress> addresses) { for (PersonAddress existingAddress : instance.getAddresses()) { existingAddress.setPreferred(false); } setFirstAddressAsPreferred(addresses); for (PersonAddress address : addresses) { PersonAddress existingAddress = getMatchingAddress(address, instance.getAddresses()); if (existingAddress != null) { copyAddressFields(existingAddress, address); } else { instance.addAddress(address); } } }
/** * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getAllCount() */ @Override public long getAllCount() { int count = 0; Person person = service.getPersonByUuid(RestTestConstants1_8.PERSON_UUID); for (PersonAddress addr : person.getAddresses()) { if (!addr.isVoided()) { count++; } } return count; }
@Test public void shouldAddAnAddressToAPerson() throws Exception { int before = service.getPersonByUuid(RestTestConstants1_8.PERSON_UUID).getAddresses().size(); SimpleObject address = new SimpleObject(); address.add("address1", "test address"); address.add("country", "USA"); address.add("preferred", "true"); MockHttpServletRequest req = newPostRequest(getURI(), address); handle(req); int after = service.getPersonByUuid(RestTestConstants1_8.PERSON_UUID).getAddresses().size(); Assert.assertEquals(before + 1, after); }
/** * Sets the preferred address for a person. If no address exists new address is set as * preferred. * * @param instance * @param address * @throws ResourceDoesNotSupportOperationException */ @PropertySetter("preferredAddress") public static void setPreferredAddress(Person instance, PersonAddress address) throws ResourceDoesNotSupportOperationException { if (address.getPersonAddressId() == null) { throw new ResourceDoesNotSupportOperationException("Only an existing address can be marked as preferred!"); } //un mark the current preferred address as preferred if any for (PersonAddress existing : instance.getAddresses()) { if (existing.isPreferred() && !OpenmrsUtil.nullSafeEquals(existing, address)) existing.setPreferred(false); } address.setPreferred(true); }
@SuppressWarnings("unchecked") @Test public void shouldNotShowVoidedAddressesInFullRepresentation() throws Exception { executeDataSet("PersonControllerTest-otherPersonData.xml"); Person person = service.getPersonByUuid(getUuid()); assertEquals(2, person.getAddresses().size()); PersonAddress voidedAddress = service.getPersonAddressByUuid("8a806d8c-822d-11e0-872f-18a905e044dc"); String voidedAddressUuid = voidedAddress.getUuid(); assertTrue(voidedAddress.isVoided()); MockHttpServletRequest req = newGetRequest(getURI() + "/" + getUuid(), new Parameter( RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL)); req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL); SimpleObject result = deserialize(handle(req)); List<SimpleObject> addresses = (List<SimpleObject>) PropertyUtils.getProperty(result, "addresses"); assertEquals(1, addresses.size()); assertFalse(voidedAddressUuid.equals(PropertyUtils.getProperty(addresses.get(0), "uuid"))); }
@Test public void shouldExcludeVoidedAddressesForAPerson() throws Exception { // For test purposes, we need to void all addresses Person person = service.getPersonByUuid(RestTestConstants1_8.PERSON_UUID); for (PersonAddress addr : person.getAddresses()) { addr.setVoided(true); } service.savePerson(person); MockHttpServletRequest req = request(RequestMethod.GET, getURI()); SimpleObject result = deserialize(handle(req)); Assert.assertEquals(0, Util.getResultsSize(result)); }
assertThat(updated.isPreferred(), is(true)); assertThat(updated.getPerson().getAddresses(), (Matcher) hasItem(allOf( hasProperty("preferred", is(false)), hasProperty("cityVillage", is("Seattle"))