Search in sources :

Example 6 with Email

use of ezvcard.property.Email in project ofbiz-framework by apache.

the class VCard method exportVCard.

public static Map<String, Object> exportVCard(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String partyId = (String) context.get("partyId");
    Locale locale = (Locale) context.get("locale");
    File file = null;
    try {
        ezvcard.VCard vcard = new ezvcard.VCard();
        StructuredName structuredName = new StructuredName();
        GenericValue person = EntityQuery.use(delegator).from("Person").where("partyId", partyId).queryOne();
        if (person != null) {
            if (UtilValidate.isNotEmpty(person.getString("firstName")))
                structuredName.setGiven(person.getString("firstName"));
            if (UtilValidate.isNotEmpty(person.getString("lastName")))
                structuredName.setFamily(person.getString("lastName"));
            vcard.setStructuredName(structuredName);
        }
        String fullName = PartyHelper.getPartyName(delegator, partyId, false);
        vcard.setFormattedName(fullName);
        GenericValue postalAddress = PartyWorker.findPartyLatestPostalAddress(partyId, delegator);
        if (postalAddress != null) {
            Address address = new Address();
            address.setStreetAddress(postalAddress.getString("address1"));
            address.setLocality(postalAddress.getString("city"));
            address.setPostalCode(postalAddress.getString("postalCode"));
            GenericValue state = postalAddress.getRelatedOne("StateProvinceGeo", false);
            if (state != null) {
                address.setRegion(state.getString("geoName"));
            }
            GenericValue countryGeo = postalAddress.getRelatedOne("CountryGeo", false);
            if (countryGeo != null) {
                String country = postalAddress.getRelatedOne("CountryGeo", false).getString("geoName");
                address.setCountry(country);
                address.getTypes().add(AddressType.WORK);
                ;
            // TODO : this can be better set by checking contactMechPurposeTypeId
            }
            vcard.addAddress(address);
        }
        GenericValue telecomNumber = PartyWorker.findPartyLatestTelecomNumber(partyId, delegator);
        if (telecomNumber != null) {
            Telephone tel = new Telephone(telecomNumber.getString("areaCode") + telecomNumber.getString("contactNumber"));
            tel.getTypes().add(TelephoneType.WORK);
            vcard.addTelephoneNumber(tel);
        // TODO : this can be better set by checking contactMechPurposeTypeId
        }
        GenericValue emailAddress = PartyWorker.findPartyLatestContactMech(partyId, "EMAIL_ADDRESS", delegator);
        if (emailAddress != null && UtilValidate.isNotEmpty(emailAddress.getString("infoString"))) {
            vcard.addEmail(new Email(emailAddress.getString("infoString")));
        }
        // TODO : convert to directdownload of a vcf file
        String saveToDirectory = EntityUtilProperties.getPropertyValue("sfa", "save.outgoing.directory", "", delegator);
        if (UtilValidate.isEmpty(saveToDirectory)) {
            saveToDirectory = System.getProperty("ofbiz.home");
        }
        String saveToFilename = fullName + ".vcf";
        file = FileUtil.getFile(saveToDirectory + "/" + saveToFilename);
        Ezvcard.write(vcard).go(file);
    } catch (FileNotFoundException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "SfaExportVCardErrorOpeningFile", UtilMisc.toMap("errorString", file.getAbsolutePath()), locale));
    } catch (IOException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "SfaExportVCardErrorWritingFile", UtilMisc.toMap("errorString", file.getAbsolutePath()), locale));
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "SfaExportVCardError", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Email(ezvcard.property.Email) Address(ezvcard.property.Address) Telephone(ezvcard.property.Telephone) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) File(java.io.File) StructuredName(ezvcard.property.StructuredName)

Example 7 with Email

use of ezvcard.property.Email in project ez-vcard by mangstadt.

the class EmailScribe method _parseHtml.

@Override
protected Email _parseHtml(HCardElement element, ParseContext context) {
    String href = element.attr("href");
    String email = extractEmailFromHrefAttribute(href);
    if (email == null) {
        email = element.value();
    }
    Email property = new Email(email);
    List<String> types = element.types();
    property.getParameters().putAll(VCardParameters.TYPE, types);
    return property;
}
Also used : Email(ezvcard.property.Email)

Example 8 with Email

use of ezvcard.property.Email in project ez-vcard by mangstadt.

the class EmailScribeTest method parseText.

@Test
public void parseText() {
    Email expected = new Email("johndoe@example.com");
    sensei.assertParseText("johndoe@example.com").run(expected);
}
Also used : Email(ezvcard.property.Email) Test(org.junit.Test)

Example 9 with Email

use of ezvcard.property.Email in project ez-vcard by mangstadt.

the class EmailScribeTest method prepareParameters_type_pref.

@Test
public void prepareParameters_type_pref() {
    Email property = new Email("johndoe@example.com");
    property.getTypes().add(EmailType.PREF);
    sensei.assertPrepareParams(property).versions(V2_1, V3_0).expected("TYPE", "pref").run();
    sensei.assertPrepareParams(property).versions(V4_0).expected("PREF", "1").run();
}
Also used : Email(ezvcard.property.Email) Test(org.junit.Test)

Example 10 with Email

use of ezvcard.property.Email in project ez-vcard by mangstadt.

the class EmailScribeTest method prepareParameters_pref_parameter.

@Test
public void prepareParameters_pref_parameter() {
    Email property = new Email("johndoe@example.com");
    property.setPref(1);
    VCard vcard = new VCard();
    vcard.addEmail(property);
    sensei.assertPrepareParams(property).versions(V2_1, V3_0).vcard(vcard).expected("TYPE", "pref").run();
    sensei.assertPrepareParams(property).versions(V4_0).vcard(vcard).expected("PREF", "1").run();
}
Also used : Email(ezvcard.property.Email) VCard(ezvcard.VCard) Test(org.junit.Test)

Aggregations

Email (ezvcard.property.Email)18 Test (org.junit.Test)10 VCard (ezvcard.VCard)6 StructuredName (ezvcard.property.StructuredName)5 Telephone (ezvcard.property.Telephone)5 EmailAddress (com.google.api.services.people.v1.model.EmailAddress)4 Person (com.google.api.services.people.v1.model.Person)4 Address (ezvcard.property.Address)3 Address (com.google.api.services.people.v1.model.Address)2 Name (com.google.api.services.people.v1.model.Name)2 PhoneNumber (com.google.api.services.people.v1.model.PhoneNumber)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Nullable (com.google.gdata.util.common.base.Nullable)2 Pair (com.google.gdata.util.common.base.Pair)2 File (java.io.File)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Locale (java.util.Locale)2 Function (java.util.function.Function)2