Search in sources :

Example 6 with BwCategory

use of org.bedework.calfacade.BwCategory in project bw-calendar-engine by Bedework.

the class Events method updateEntities.

/* ====================================================================
   *                   Package private methods
   * ==================================================================== */
void updateEntities(final UpdateResult updResult, final BwEvent event) throws CalFacadeException {
    final EventProperties<BwCategory> cathdlr = getSvc().getCategoriesHandler();
    final Set<BwCategory> cats = event.getCategories();
    final Set<BwCategory> removeCats = new TreeSet<>();
    final Set<BwCategory> addCats = new TreeSet<>();
    if (cats != null) {
        for (final BwCategory cat : cats) {
            if (cat.unsaved()) {
                final EnsureEntityExistsResult<BwCategory> eeer = cathdlr.ensureExists(cat, event.getOwnerHref());
                removeCats.add(cat);
                if (eeer.entity != null) {
                    addCats.add(eeer.entity);
                }
            }
        }
        for (final BwCategory cat : removeCats) {
            event.removeCategory(cat);
        }
        for (final BwCategory cat : addCats) {
            event.addCategory(cat);
        }
    }
    final BwContact ct = event.getContact();
    if (ct != null) {
        final EnsureEntityExistsResult<BwContact> eeers = getSvc().getContactsHandler().ensureExists(ct, ct.getOwnerHref());
        if (eeers.added) {
            updResult.contactsAdded++;
        }
        // XXX only do this if we know it changed
        event.setContact(eeers.entity);
    }
    final BwLocation loc = event.getLocation();
    if (loc != null) {
        final EnsureEntityExistsResult<BwLocation> eeerl = getSvc().getLocationsHandler().ensureExists(loc, loc.getOwnerHref());
        if (eeerl.added) {
            updResult.locationsAdded++;
        }
        // XXX only do this if we know it changed
        event.setLocation(eeerl.entity);
    }
}
Also used : BwLocation(org.bedework.calfacade.BwLocation) TreeSet(java.util.TreeSet) BwCategory(org.bedework.calfacade.BwCategory) BwContact(org.bedework.calfacade.BwContact)

Example 7 with BwCategory

use of org.bedework.calfacade.BwCategory in project bw-calendar-engine by Bedework.

the class Events method setEntityCategories.

@Override
public SetEntityCategoriesResult setEntityCategories(final CategorisedEntity ent, final Set<BwCategory> extraCats, final Set<String> defCatUids, final Set<String> allDefCatUids, final Collection<String> strCatUids, final ChangeTable changes) throws CalFacadeException {
    // XXX We should use the change table code for this.
    final SetEntityCategoriesResult secr = new SetEntityCategoriesResult();
    /* categories already set in event */
    final Set<BwCategory> entcats = ent.getCategories();
    final Map<String, BwCategory> entcatMap = new HashMap<>();
    if (!Util.isEmpty(entcats)) {
        for (final BwCategory entcat : entcats) {
            entcatMap.put(entcat.getUid(), entcat);
        }
    }
    if (Util.isEmpty(strCatUids) && Util.isEmpty(extraCats) && Util.isEmpty(defCatUids) && Util.isEmpty(allDefCatUids)) {
        if (!Util.isEmpty(entcats)) {
            if (changes != null) {
                final ChangeTableEntry cte = changes.getEntry(PropertyInfoIndex.CATEGORIES);
                cte.setRemovedValues(new ArrayList<>(entcats));
            }
            secr.numRemoved = entcats.size();
            entcats.clear();
        }
        secr.rcode = success;
        return secr;
    }
    final Set<BwCategory> cats = new TreeSet<>();
    if (extraCats != null) {
        cats.addAll(extraCats);
    }
    if (!Util.isEmpty(defCatUids)) {
        for (final String uid : defCatUids) {
            final BwCategory cat = getSvc().getCategoriesHandler().getPersistent(uid);
            if (cat != null) {
                cats.add(cat);
            }
        }
    }
    if (!Util.isEmpty(allDefCatUids) && (entcats != null)) {
        for (final String catUid : allDefCatUids) {
            /* If it's in the event add it to the list we're building then move on
         * to the next requested category.
         */
            final BwCategory entcat = entcatMap.get(catUid);
            if (entcat != null) {
                cats.add(entcat);
            }
        }
    }
    if (!Util.isEmpty(strCatUids)) {
        buildList: for (final String catUid : strCatUids) {
            /* If it's in the event add it to the list we're building then move on
         * to the next requested category.
         */
            final BwCategory entcat = entcatMap.get(catUid);
            if (entcat != null) {
                cats.add(entcat);
                continue buildList;
            }
            final BwCategory cat = getSvc().getCategoriesHandler().getPersistent(catUid);
            if (cat != null) {
                cats.add(cat);
            }
        }
    }
    /* cats now contains category objects corresponding to the parameters
     *
     * Now we need to add or remove any in the event but not in our list.
     */
    /* First make a list to remove - to avoid concurrent update
     * problems with the iterator
     */
    final ArrayList<BwCategory> toRemove = new ArrayList<>();
    if (entcats != null) {
        for (final BwCategory evcat : entcats) {
            if (cats.contains(evcat)) {
                cats.remove(evcat);
                continue;
            }
            toRemove.add(evcat);
        }
    }
    for (final BwCategory cat : cats) {
        ent.addCategory(cat);
        secr.numAdded++;
    }
    for (final BwCategory cat : toRemove) {
        if (entcats.remove(cat)) {
            secr.numRemoved++;
        }
    }
    if ((changes != null) && (secr.numAdded > 0) && (secr.numRemoved > 0)) {
        final ChangeTableEntry cte = changes.getEntry(PropertyInfoIndex.CATEGORIES);
        cte.setRemovedValues(toRemove);
        cte.setAddedValues(cats);
    }
    secr.rcode = success;
    return secr;
}
Also used : HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) BwCategory(org.bedework.calfacade.BwCategory) ArrayList(java.util.ArrayList) ChangeTableEntry(org.bedework.calfacade.util.ChangeTableEntry)

Example 8 with BwCategory

use of org.bedework.calfacade.BwCategory in project bw-calendar-engine by Bedework.

the class BwEventUtil method checkCategory.

/* Return true if value matches a category - which may be added as
   * a result
   */
private static boolean checkCategory(final IcalCallback cb, final ChangeTable chg, final BwEvent ev, final String lang, final String val) throws CalFacadeException {
    final BwString sval = new BwString(lang, val);
    final BwCategory cat = cb.findCategory(sval);
    if (cat == null) {
        return false;
    }
    final Set<BwCategory> cats = ev.getCategories();
    if (cats != null) {
        for (final BwCategory c : cats) {
            if (c.getWord().equals(sval)) {
                // Already present
                return true;
            }
        }
    }
    ev.addCategory(cat);
    chg.addValue(PropertyIndex.PropertyInfoIndex.CATEGORIES, cat);
    return true;
}
Also used : BwCategory(org.bedework.calfacade.BwCategory) BwString(org.bedework.calfacade.BwString)

Example 9 with BwCategory

use of org.bedework.calfacade.BwCategory in project bw-calendar-engine by Bedework.

the class Preferences method updateAdminPrefs.

/* (non-Javadoc)
   * @see org.bedework.calsvci.PreferencesI#updateAdminPrefs(boolean, org.bedework.calfacade.BwCalendar, org.bedework.calfacade.BwCategory, org.bedework.calfacade.BwLocation, org.bedework.calfacade.BwContact)
   */
public void updateAdminPrefs(final boolean remove, final BwCalendar cal, final Collection<BwCategory> cats, final BwLocation loc, final BwContact ctct) throws CalFacadeException {
    BwCommonUserPrefs prefs = null;
    boolean update = false;
    BwAuthUser au = null;
    if (getPars().getPublicAdmin()) {
        au = getSvc().getUserAuth().getUser(getPars().getAuthUser());
        if (au != null) {
            prefs = au.getPrefs();
        }
    }
    if (prefs == null) {
        // XXX until we get non admin user preferred calendars etc
        return;
    }
    if (cal != null) {
        if (!remove) {
            if (cal.getCalendarCollection()) {
                CalendarPref p = prefs.getCalendarPrefs();
                if (p.getAutoAdd() && p.add(cal)) {
                    update = true;
                }
            }
        } else {
            getSvc().removeFromAllPrefs(cal);
        }
    }
    if (!Util.isEmpty(cats)) {
        for (final BwCategory cat : cats) {
            if (!remove) {
                CategoryPref p = prefs.getCategoryPrefs();
                if (p.getAutoAdd() && p.add(cat)) {
                    update = true;
                }
            } else {
                getSvc().removeFromAllPrefs(cat);
            }
        }
    }
    if (loc != null) {
        if (!remove) {
            LocationPref p = prefs.getLocationPrefs();
            if (p.getAutoAdd() && p.add(loc)) {
                update = true;
            }
        } else {
            getSvc().removeFromAllPrefs(loc);
        }
    }
    if (ctct != null) {
        if (!remove) {
            ContactPref p = prefs.getContactPrefs();
            if (p.getAutoAdd() && p.add(ctct)) {
                update = true;
            }
        } else {
            getSvc().removeFromAllPrefs(ctct);
        }
    }
    if (update) {
        if (getPars().getPublicAdmin()) {
            getSvc().getUserAuth().updateUser(au);
        }
    }
}
Also used : BwAuthUser(org.bedework.calfacade.svc.BwAuthUser) CategoryPref(org.bedework.calfacade.svc.prefs.CategoryPref) ContactPref(org.bedework.calfacade.svc.prefs.ContactPref) BwCommonUserPrefs(org.bedework.calfacade.svc.prefs.BwCommonUserPrefs) CalendarPref(org.bedework.calfacade.svc.prefs.CalendarPref) BwCategory(org.bedework.calfacade.BwCategory) LocationPref(org.bedework.calfacade.svc.prefs.LocationPref)

Example 10 with BwCategory

use of org.bedework.calfacade.BwCategory in project bw-calendar-engine by Bedework.

the class AccessUtil method getAclChars.

/* ====================================================================
   *                   Private methods
   * ==================================================================== */
/* If the entity is not a collection we merge the access in with the container
   * access then return the merged aces. We do this because we call getPathInfo
   * with a collection entity. That method will recurse up to the root.
   *
   * For a calendar we just use the access for the calendar.
   *
   * The calendar/container access might be cached in the pathInfoTable.
   */
private char[] getAclChars(final BwShareableDbentity<?> ent) throws CalFacadeException {
    if ((!(ent instanceof BwEventProperty)) && (ent instanceof BwShareableContainedDbentity)) {
        BwCalendar container;
        if (ent instanceof BwCalendar) {
            container = (BwCalendar) ent;
        } else {
            container = getParent((BwShareableContainedDbentity<?>) ent);
        }
        if (container == null) {
            return null;
        }
        final String path = container.getPath();
        CalendarWrapper wcol = (CalendarWrapper) container;
        String aclStr;
        char[] aclChars = null;
        /* Get access for the parent first if we have one */
        BwCalendar parent = getParent(wcol);
        if (parent != null) {
            aclStr = new String(merged(getAclChars(parent), parent.getPath(), wcol.getAccess()));
        } else if (wcol.getAccess() != null) {
            aclStr = wcol.getAccess();
        } else {
            // At root
            throw new CalFacadeException("Collections must have default access set at root");
        }
        if (aclStr != null) {
            aclChars = aclStr.toCharArray();
        }
        if (ent instanceof BwCalendar) {
            return aclChars;
        }
        return merged(aclChars, path, ent.getAccess());
    }
    /* This is a way of making other objects sort of shareable.
     * The objects are locations, sponsors and categories.
     * (also calsuite)
     *
     * We store the default access in the owner principal and manipulate that to give
     * us some degree of sharing.
     *
     * In effect, the owner becomes the container for the object.
     */
    String aclString = null;
    String entAccess = ent.getAccess();
    BwPrincipal owner = (BwPrincipal) cb.getPrincipal(ent.getOwnerHref());
    if (ent instanceof BwCategory) {
        aclString = owner.getCategoryAccess();
    } else if (ent instanceof BwLocation) {
        aclString = owner.getLocationAccess();
    } else if (ent instanceof BwContact) {
        aclString = owner.getContactAccess();
    }
    if (aclString == null) {
        if (entAccess == null) {
            if (ent.getPublick()) {
                return Access.getDefaultPublicAccess().toCharArray();
            }
            return Access.getDefaultPersonalAccess().toCharArray();
        }
        return entAccess.toCharArray();
    }
    if (entAccess == null) {
        return aclString.toCharArray();
    }
    try {
        Acl acl = Acl.decode(entAccess.toCharArray());
        acl = acl.merge(aclString.toCharArray(), "/owner");
        return acl.getEncoded();
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : BwLocation(org.bedework.calfacade.BwLocation) BwShareableContainedDbentity(org.bedework.calfacade.base.BwShareableContainedDbentity) BwCategory(org.bedework.calfacade.BwCategory) BwEventProperty(org.bedework.calfacade.BwEventProperty) BwCalendar(org.bedework.calfacade.BwCalendar) BwContact(org.bedework.calfacade.BwContact) Acl(org.bedework.access.Acl) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BwPrincipal(org.bedework.calfacade.BwPrincipal) CalendarWrapper(org.bedework.calfacade.wrappers.CalendarWrapper)

Aggregations

BwCategory (org.bedework.calfacade.BwCategory)42 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)13 BwContact (org.bedework.calfacade.BwContact)11 BwString (org.bedework.calfacade.BwString)11 BwCalendar (org.bedework.calfacade.BwCalendar)10 BwEvent (org.bedework.calfacade.BwEvent)10 BwLocation (org.bedework.calfacade.BwLocation)8 BwXproperty (org.bedework.calfacade.BwXproperty)7 ArrayList (java.util.ArrayList)6 EventInfo (org.bedework.calfacade.svc.EventInfo)6 BwDateTime (org.bedework.calfacade.BwDateTime)5 TreeSet (java.util.TreeSet)4 BwAttendee (org.bedework.calfacade.BwAttendee)4 ChangeTableEntry (org.bedework.calfacade.util.ChangeTableEntry)4 PropertyInfoIndex (org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex)4 Period (net.fortuna.ical4j.model.Period)3 BwEventProxy (org.bedework.calfacade.BwEventProxy)3 BwIcalPropertyInfoEntry (org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)3 FilterBase (org.bedework.caldav.util.filter.FilterBase)2 ObjectFilter (org.bedework.caldav.util.filter.ObjectFilter)2