Search in sources :

Example 1 with ZModificationList

use of com.zimbra.cs.ldap.ZModificationList in project zm-mailbox by Zimbra.

the class ZLdapHelper method getModList.

private ZModificationList getModList(ZLdapContext zlc, String dn, Map<String, ? extends Object> attrs, Entry entry) throws ServiceException {
    ZModificationList modList = zlc.createModificationList();
    AttributeManager attrMgr = AttributeManager.getInst();
    for (Map.Entry<String, ? extends Object> attr : attrs.entrySet()) {
        Object v = attr.getValue();
        String key = attr.getKey();
        boolean doAdd = key.charAt(0) == '+';
        boolean doRemove = key.charAt(0) == '-';
        if (doAdd || doRemove) {
            // make sure there aren't other changes without +/- going on at the same time
            key = key.substring(1);
            if (attrs.containsKey(key))
                throw ServiceException.INVALID_REQUEST("can't mix +attrName/-attrName with attrName", null);
        }
        boolean containsBinaryData = attrMgr == null ? false : attrMgr.containsBinaryData(key);
        boolean isBinaryTransfer = attrMgr == null ? false : attrMgr.isBinaryTransfer(key);
        // Convert array to List so it can be treated as a Collection
        if (v instanceof Object[]) {
            // Note: Object[] cast is required, so that asList() knows to create a List
            // that contains the contents of the object array, as opposed to a List with one
            // element, which is the entire Object[].  Ick.
            v = Arrays.asList((Object[]) v);
        }
        if (v instanceof Collection) {
            Collection c = (Collection) v;
            if (c.size() == 0) {
                // make sure it exists
                if (entry.getAttr(key, false) != null) {
                    modList.removeAttr(key, isBinaryTransfer);
                }
            } else {
                // Convert values Collection to a String array
                String[] sa = new String[c.size()];
                int i = 0;
                for (Object o : c) {
                    sa[i++] = (o == null ? null : o.toString());
                }
                // Add attrs
                if (doAdd) {
                    modList.addAttr(key, sa, entry, containsBinaryData, isBinaryTransfer);
                } else if (doRemove) {
                    modList.removeAttr(key, sa, entry, containsBinaryData, isBinaryTransfer);
                } else {
                    modList.modifyAttr(key, sa, containsBinaryData, isBinaryTransfer);
                }
            }
        } else if (v instanceof Map) {
            throw ServiceException.FAILURE("Map is not a supported value type", null);
        } else {
            String s = (v == null ? null : v.toString());
            if (doAdd) {
                modList.addAttr(key, s, entry, containsBinaryData, isBinaryTransfer);
            } else if (doRemove) {
                modList.removeAttr(key, s, entry, containsBinaryData, isBinaryTransfer);
            } else {
                modList.modifyAttr(key, s, entry, containsBinaryData, isBinaryTransfer);
            }
        }
    }
    return modList;
}
Also used : AttributeManager(com.zimbra.cs.account.AttributeManager) Collection(java.util.Collection) ZModificationList(com.zimbra.cs.ldap.ZModificationList) Map(java.util.Map)

Aggregations

AttributeManager (com.zimbra.cs.account.AttributeManager)1 ZModificationList (com.zimbra.cs.ldap.ZModificationList)1 Collection (java.util.Collection)1 Map (java.util.Map)1