Search in sources :

Example 6 with FormattedName

use of ezvcard.property.FormattedName in project ring-client-android by savoirfairelinux.

the class RingNavigationPresenter method getAlias.

public String getAlias(Account account) {
    if (account == null) {
        Log.e(TAG, "Not able to get alias");
        return null;
    }
    VCard vcard = VCardUtils.loadLocalProfileFromDisk(mDeviceRuntimeService.provideFilesDir(), account.getAccountID());
    FormattedName name = vcard.getFormattedName();
    if (name != null) {
        String name_value = name.getValue();
        if (name_value != null && !name_value.isEmpty()) {
            return name_value;
        }
    }
    return null;
}
Also used : FormattedName(ezvcard.property.FormattedName) VCard(ezvcard.VCard)

Example 7 with FormattedName

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

the class VCard method importVCard.

/**
 * import a vcard from byteBuffer. the reader use is ez-vcard, see official site https://github.com/mangstadt/ez-vcard/
 * @param dctx
 * @param context
 * @return
 * @throws IOException
 */
public static Map<String, Object> importVCard(DispatchContext dctx, Map<String, ? extends Object> context) throws IOException {
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    Map<String, Object> result = ServiceUtil.returnSuccess();
    ByteBuffer byteBuffer = (ByteBuffer) context.get("infile");
    byte[] inputByteArray = byteBuffer.array();
    InputStream in = new ByteArrayInputStream(inputByteArray);
    Map<String, Object> serviceCtx = new HashMap<String, Object>();
    boolean isGroup = false;
    List<Map<String, String>> partiesCreated = new ArrayList<Map<String, String>>();
    List<Map<String, String>> partiesExist = new ArrayList<Map<String, String>>();
    // TODO this is not used yet
    String partyName = "";
    try (VCardReader vCardReader = new VCardReader(in)) {
        ezvcard.VCard vcard = null;
        while ((vcard = vCardReader.readNext()) != null) {
            // Todo create a generic service to resolve duplicate party
            FormattedName formattedName = vcard.getFormattedName();
            if (formattedName != null) {
                String refCardId = formattedName.getValue();
                GenericValue partyIdentification = EntityQuery.use(delegator).from("PartyIdentification").where("partyIdentificationTypeId", "VCARD_FN_ORIGIN", "idValue", refCardId).queryFirst();
                if (partyIdentification != null) {
                    partiesExist.add(UtilMisc.toMap("partyId", (String) partyIdentification.get("partyId")));
                    continue;
                }
            // TODO manage update
            }
            // check if it's already load
            isGroup = false;
            if (vcard.getKind() != null)
                isGroup = vcard.getKind().isGroup();
            StructuredName structuredName = vcard.getStructuredName();
            if (UtilValidate.isEmpty(structuredName))
                continue;
            if (!isGroup) {
                serviceCtx.put("firstName", structuredName.getGiven());
                serviceCtx.put("lastName", structuredName.getFamily());
                partyName = structuredName.getGiven() + " " + structuredName.getFamily();
            }
            // Resolve all postal Address
            for (Address address : vcard.getAddresses()) {
                boolean workAddress = false;
                for (AddressType addressType : address.getTypes()) {
                    if (AddressType.PREF.equals(addressType) || AddressType.WORK.equals(addressType)) {
                        workAddress = true;
                        break;
                    }
                }
                if (!workAddress)
                    continue;
                serviceCtx.put("address1", address.getStreetAddressFull());
                serviceCtx.put("city", address.getLocality());
                serviceCtx.put("postalCode", address.getPostalCode());
                GenericValue countryGeo = EntityQuery.use(delegator).from("Geo").where(EntityCondition.makeCondition("geoTypeId", EntityOperator.EQUALS, "COUNTRY"), EntityCondition.makeCondition("geoName", EntityOperator.LIKE, address.getCountry())).cache().queryFirst();
                if (countryGeo != null) {
                    serviceCtx.put("countryGeoId", countryGeo.get("geoId"));
                }
                GenericValue stateGeo = EntityQuery.use(delegator).from("Geo").where(EntityCondition.makeCondition("geoTypeId", EntityOperator.EQUALS, "STATE"), EntityCondition.makeCondition("geoName", EntityOperator.LIKE, address.getRegion())).cache().queryFirst();
                if (stateGeo != null) {
                    serviceCtx.put("stateProvinceGeoId", stateGeo.get("geoId"));
                }
            }
            int nbEmailAddr = (vcard.getEmails() != null) ? vcard.getEmails().size() : 0;
            for (Email email : vcard.getEmails()) {
                if (nbEmailAddr > 1) {
                    boolean workEmail = false;
                    for (EmailType emailType : email.getTypes()) {
                        if (EmailType.PREF.equals(emailType) || EmailType.WORK.equals(emailType)) {
                            workEmail = true;
                            break;
                        }
                    }
                    if (!workEmail)
                        continue;
                }
                String emailAddr = email.getValue();
                if (UtilValidate.isEmail(emailAddr)) {
                    serviceCtx.put("emailAddress", emailAddr);
                } else {
                    // TODO change uncorrect labellisation
                    String emailFormatErrMsg = UtilProperties.getMessage(resourceError, "SfaImportVCardEmailFormatError", locale);
                    vCardReader.close();
                    return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "MarketingEmailFormatError", UtilMisc.toMap("firstName", structuredName.getGiven(), "lastName", structuredName.getFamily(), "emailFOrmatErrMsg", emailFormatErrMsg), locale));
                }
            }
            int nbPhone = (vcard.getTelephoneNumbers() != null) ? vcard.getTelephoneNumbers().size() : 0;
            for (Telephone phone : vcard.getTelephoneNumbers()) {
                if (nbPhone > 1) {
                    boolean workPhone = false;
                    for (TelephoneType phoneType : phone.getTypes()) {
                        if (TelephoneType.PREF.equals(phoneType) || TelephoneType.WORK.equals(phoneType)) {
                            workPhone = true;
                            break;
                        }
                    }
                    if (!workPhone)
                        continue;
                }
                String phoneAddr = phone.getText();
                boolean internationalPhone = phoneAddr.startsWith("+") || phoneAddr.startsWith("00");
                phoneAddr = StringUtil.removeNonNumeric(phoneAddr);
                int indexLocal = 0;
                if (internationalPhone) {
                    indexLocal = 4;
                    if (!phoneAddr.startsWith("00")) {
                        phoneAddr = phoneAddr.concat("00");
                    }
                    serviceCtx.put("areaCode", phoneAddr.substring(0, indexLocal));
                }
                serviceCtx.put("contactNumber", phoneAddr.substring(indexLocal));
            }
            /* TODO improve this part to manage party organization */
            GenericValue userLogin = (GenericValue) context.get("userLogin");
            serviceCtx.put("userLogin", userLogin);
            String serviceName = (String) context.get("serviceName");
            Map<String, Object> serviceContext = UtilGenerics.cast(context.get("serviceContext"));
            if (UtilValidate.isNotEmpty(serviceContext)) {
                for (Map.Entry<String, Object> entry : serviceContext.entrySet()) {
                    serviceCtx.put(entry.getKey(), entry.getValue());
                }
            }
            Map<String, Object> resp = dispatcher.runSync(serviceName, serviceCtx);
            if (ServiceUtil.isError(resp)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(resp));
            }
            partiesCreated.add(UtilMisc.toMap("partyId", (String) resp.get("partyId")));
            if (formattedName != null) {
                // store the origin creation
                Map<String, Object> createPartyIdentificationMap = dctx.makeValidContext("createPartyIdentification", ModelService.IN_PARAM, context);
                createPartyIdentificationMap.put("partyId", resp.get("partyId"));
                createPartyIdentificationMap.put("partyIdentificationTypeId", "VCARD_FN_ORIGIN");
                createPartyIdentificationMap.put("idValue", formattedName.getValue());
                resp = dispatcher.runSync("createPartyIdentification", createPartyIdentificationMap);
                if (ServiceUtil.isError(resp)) {
                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(resp));
                }
            }
        }
    } catch (IOException | GenericEntityException | GenericServiceException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "SfaImportVCardError", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    result.put("partiesCreated", partiesCreated);
    result.put("partiesExist", partiesExist);
    return result;
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Email(ezvcard.property.Email) EmailType(ezvcard.parameter.EmailType) Address(ezvcard.property.Address) Telephone(ezvcard.property.Telephone) HashMap(java.util.HashMap) FormattedName(ezvcard.property.FormattedName) ArrayList(java.util.ArrayList) TelephoneType(ezvcard.parameter.TelephoneType) StructuredName(ezvcard.property.StructuredName) GenericValue(org.apache.ofbiz.entity.GenericValue) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) VCardReader(ezvcard.io.text.VCardReader) Delegator(org.apache.ofbiz.entity.Delegator) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) AddressType(ezvcard.parameter.AddressType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with FormattedName

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

the class EzvcardTest method write_one.

@Test
public void write_one() throws Exception {
    VCard vcard = new VCard();
    vcard.setVersion(VCardVersion.V2_1);
    vcard.setFormattedName(new FormattedName("John Doe"));
    String actual = Ezvcard.write(vcard).go();
    assertTrue(actual.contains("VERSION:2.1"));
    assertTrue(actual.contains("FN:John Doe"));
}
Also used : FormattedName(ezvcard.property.FormattedName) Test(org.junit.Test)

Example 9 with FormattedName

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

the class EzvcardTest method writeJson_multiple.

@Test
public void writeJson_multiple() {
    VCard vcard1 = new VCard();
    vcard1.setFormattedName(new FormattedName("John Doe"));
    VCard vcard2 = new VCard();
    vcard2.setFormattedName(new FormattedName("Jane Doe"));
    VCard vcard3 = new VCard();
    vcard3.setFormattedName(new FormattedName("Janet Doe"));
    // @formatter:off
    String expected = "[" + "[\"vcard\"," + "[" + "[\"version\",{},\"text\",\"4.0\"]," + "[\"fn\",{},\"text\",\"John Doe\"]" + "]" + "]," + "[\"vcard\"," + "[" + "[\"version\",{},\"text\",\"4.0\"]," + "[\"fn\",{},\"text\",\"Jane Doe\"]" + "]" + "]," + "[\"vcard\"," + "[" + "[\"version\",{},\"text\",\"4.0\"]," + "[\"fn\",{},\"text\",\"Janet Doe\"]" + "]" + "]" + "]";
    // @formatter:on
    String actual = Ezvcard.writeJson(vcard1, vcard2, vcard3).prodId(false).go();
    assertEquals(expected, actual);
}
Also used : FormattedName(ezvcard.property.FormattedName) Test(org.junit.Test)

Example 10 with FormattedName

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

the class EzvcardTest method writeHtml_multiple.

@Test
public void writeHtml_multiple() throws Exception {
    VCard vcard1 = new VCard();
    vcard1.setFormattedName(new FormattedName("John Doe"));
    VCard vcard2 = new VCard();
    vcard2.setFormattedName(new FormattedName("Jane Doe"));
    VCard vcard3 = new VCard();
    vcard3.setFormattedName(new FormattedName("Janet Doe"));
    String actual = Ezvcard.writeHtml(vcard1, vcard2, vcard3).go();
    org.jsoup.nodes.Document document = Jsoup.parse(actual);
    assertEquals(3, document.select(".vcard").size());
    assertEquals(3, document.select(".vcard .fn").size());
}
Also used : FormattedName(ezvcard.property.FormattedName) Test(org.junit.Test)

Aggregations

FormattedName (ezvcard.property.FormattedName)19 Test (org.junit.Test)11 VCard (ezvcard.VCard)6 RawProperty (ezvcard.property.RawProperty)3 StructuredName (ezvcard.property.StructuredName)3 Uid (ezvcard.property.Uid)3 Photo (ezvcard.property.Photo)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Bitmap (android.graphics.Bitmap)1 RingAccountViewModelImpl (cx.ring.account.RingAccountViewModelImpl)1 VCardVersion (ezvcard.VCardVersion)1 VCardReader (ezvcard.io.text.VCardReader)1 AddressType (ezvcard.parameter.AddressType)1 EmailType (ezvcard.parameter.EmailType)1 TelephoneType (ezvcard.parameter.TelephoneType)1 Address (ezvcard.property.Address)1 Email (ezvcard.property.Email)1 Note (ezvcard.property.Note)1 ProductId (ezvcard.property.ProductId)1 Telephone (ezvcard.property.Telephone)1