Example usage for java.util SortedSet contains

List of usage examples for java.util SortedSet contains

Introduction

In this page you can find the example usage for java.util SortedSet contains.

Prototype

boolean contains(Object o);

Source Link

Document

Returns true if this set contains the specified element.

Usage

From source file:org.sakaiproject.content.impl.BaseContentService.java

/**
 * Create a new resource with the given resource name used as a resource id within the specified collection or (if that id is already in use) with a resource id based on a variation on the name to achieve a unique id, provided a unique id can be found
 * before a limit is reached on the number of attempts to achieve uniqueness.  Used to create a group-aware resource.
 * /*from  w  ww.ja  v  a2 s  . co  m*/
 * @param name
 *        The name of the new resource (such as a filename).
 * @param collectionId
 *        The id of the collection to which the resource should be added.
 * @param limit
 *        The maximum number of attempts at finding a unique id based on the given name.
 * @param type
 *        The mime type string of the resource.
 * @param content
 *        An array containing the bytes of the resource's content.
 * @param properties
 *        A ResourceProperties object with the properties to add to the new resource.
 * @param groups
 *        A collection (String) of references to Group objects representing the site subgroups that should have access to this entity.
 *        May be empty to indicate access is not limited to a group or groups.
 * @param priority
 *        The notification priority for this commit.
 * @exception PermissionException
 *            if the user does not have permission to add a resource to the containing collection.
 * @exception IdUniquenessException
 *            if a unique resource id cannot be found before the limit on the number of attempts is reached.
 * @exception IdLengthException
 *            if the resource id exceeds the maximum number of characters for a valid resource id.
 * @exception IdInvalidException
 *            if the resource id is invalid.
 * @exception InconsistentException
 *            if the containing collection does not exist.
 * @exception OverQuotaException
 *            if this would result in being over quota.
 * @exception ServerOverloadException
 *            if the server is configured to write the resource body to the filesystem and the save fails.
 * @return a new ContentResource object.
 */
public ContentResource addResource(String name, String collectionId, int limit, String type, byte[] content,
        ResourceProperties properties, Collection groups, int priority)
        throws PermissionException, IdUniquenessException, IdLengthException, IdInvalidException,
        InconsistentException, OverQuotaException, ServerOverloadException {
    try {
        collectionId = collectionId.trim();
        name = Validator.escapeResourceName(name.trim());
        checkCollection(collectionId);
    } catch (IdUnusedException e) {
        throw new InconsistentException(collectionId);
    } catch (TypeException e) {
        throw new InconsistentException(collectionId);
    }

    String id = collectionId + name;
    id = (String) fixTypeAndId(id, type).get("id");
    if (id.length() > MAXIMUM_RESOURCE_ID_LENGTH) {
        throw new IdLengthException(id);
    }

    ContentResourceEdit edit = null;

    try {
        edit = addResource(id);
        edit.setContentType(type);
        edit.setContent(content);
        addProperties(edit.getPropertiesEdit(), properties);
        if (groups == null || groups.isEmpty()) {
            // access is inherited (the default)
        } else {
            edit.setGroupAccess(groups);
            // TODO: Need to deal with failure here
        }

        try {
            // commit the change
            commitResource(edit, priority);
        } catch (OverQuotaException e) {
            M_log.debug("OverQuotaException " + e);
            try {
                removeResource(edit.getId());
            } catch (Exception e1) {
                // ignore -- no need to remove the resource if it doesn't exist
                M_log.debug("Unable to remove partially completed resource: " + edit.getId() + "\n" + e1);
            }
            throw e;
        } catch (ServerOverloadException e) {
            M_log.debug("ServerOverloadException " + e);
            try {
                removeResource(edit.getId());
            } catch (Exception e1) {
                // ignore -- no need to remove the resource if it doesn't exist
                M_log.debug("Unable to remove partially completed resource: " + edit.getId() + "\n" + e1);
            }
            throw e;
        }
    } catch (IdUsedException e) {
        try {
            checkResource(id);
        } catch (IdUnusedException inner_e) {
            // TODO: What does this condition actually represent? What exception should be thrown?
            throw new IdUniquenessException(id);
        } catch (TypeException inner_e) {
            throw new InconsistentException(id);
        }

        SortedSet<String> siblings = new TreeSet<String>();
        try {
            ContentCollection collection = findCollection(collectionId);
            siblings.addAll(collection.getMembers());
        } catch (TypeException inner_e) {
            throw new InconsistentException(collectionId);
        }

        int index = name.lastIndexOf(".");
        String base = name;
        String ext = "";
        if (index > 0 && !"Url".equalsIgnoreCase(type)) {
            base = name.substring(0, index);
            ext = name.substring(index);
        }
        boolean trying = true;
        int attempts = 1;
        while (trying) // see end of loop for condition that enforces attempts <= limit)
        {
            String new_id = collectionId + base + "-" + attempts + ext;
            if (new_id.length() > MAXIMUM_RESOURCE_ID_LENGTH) {
                throw new IdLengthException(new_id);
            }
            if (!siblings.contains(new_id)) {
                try {
                    edit = addResource(new_id);
                    edit.setContentType(type);
                    edit.setContent(content);
                    if (groups == null || groups.isEmpty()) {
                        // access is inherited (the default)
                    } else {
                        edit.setGroupAccess(groups);
                        // TODO: Need to deal with failure here
                    }

                    addProperties(edit.getPropertiesEdit(), properties);
                    // commit the change
                    commitResource(edit, priority);

                    trying = false;
                } catch (IdUsedException ignore) {
                    // try again
                }
            }
            attempts++;
            if (attempts > limit) {
                throw new IdUniquenessException(new_id);
            }
        }
    }
    return edit;

}

From source file:org.sakaiproject.content.impl.BaseContentService.java

/**
 * @see org.sakaiproject.content.api.ContentHostingService#addResource(java.lang.String, java.lang.String, int, java.lang.String, java.io.InputStream, org.sakaiproject.entity.api.ResourceProperties, java.util.Collection, boolean, org.sakaiproject.time.api.Time, org.sakaiproject.time.api.Time, int)
 *///from  w  w  w . ja v  a2s .  c o  m
public ContentResource addResource(String name, String collectionId, int limit, String type,
        InputStream content, ResourceProperties properties, Collection groups, boolean hidden, Time releaseDate,
        Time retractDate, int priority) throws PermissionException, IdUniquenessException, IdLengthException,
        IdInvalidException, InconsistentException, OverQuotaException, ServerOverloadException {
    try {
        collectionId = collectionId.trim();
        name = Validator.escapeResourceName(name.trim());
        checkCollection(collectionId);
    } catch (IdUnusedException e) {
        throw new InconsistentException(collectionId);
    } catch (TypeException e) {
        throw new InconsistentException(collectionId);
    }

    String id = collectionId + name;
    id = (String) fixTypeAndId(id, type).get("id");
    if (id.length() > MAXIMUM_RESOURCE_ID_LENGTH) {
        throw new IdLengthException(id);
    }

    ContentResourceEdit edit = null;

    try {
        edit = addResource(id);
        edit.setContentType(type);
        edit.setContent(content);
        addProperties(edit.getPropertiesEdit(), properties);
        if (groups == null || groups.isEmpty()) {
            // access is inherited (the default)
        } else {
            edit.setGroupAccess(groups);
            // TODO: Need to deal with failure here
        }
        edit.setAvailability(hidden, releaseDate, retractDate);

        try {
            // commit the change
            commitResource(edit, priority);
        } catch (OverQuotaException e) {
            M_log.debug("OverQuotaException " + e);
            try {
                removeResource(edit.getId());
            } catch (Exception e1) {
                // ignore -- no need to remove the resource if it doesn't exist
                M_log.debug("Unable to remove partially completed resource: " + edit.getId() + "\n" + e1);
            }
            throw e;
        } catch (ServerOverloadException e) {
            M_log.debug("ServerOverloadException " + e);
            try {
                removeResource(edit.getId());
            } catch (Exception e1) {
                // ignore -- no need to remove the resource if it doesn't exist
                M_log.debug("Unable to remove partially completed resource: " + edit.getId() + "\n" + e1);
            }
            throw e;
        }
    } catch (IdUsedException e) {
        try {
            checkResource(id);
        } catch (IdUnusedException inner_e) {
            // TODO: What does this condition actually represent? What exception should be thrown?
            throw new IdUniquenessException(id);
        } catch (TypeException inner_e) {
            throw new InconsistentException(id);
        }

        SortedSet<String> siblings = new TreeSet<String>();
        try {
            ContentCollection collection = findCollection(collectionId);
            siblings.addAll(collection.getMembers());
        } catch (TypeException inner_e) {
            throw new InconsistentException(collectionId);
        }

        int index = name.lastIndexOf(".");
        String base = name;
        String ext = "";
        if (index > 0 && !"Url".equalsIgnoreCase(type)) {
            base = name.substring(0, index);
            ext = name.substring(index);
        }
        boolean trying = true;
        int attempts = 1;
        while (trying) // see end of loop for condition that enforces attempts <= limit)
        {
            String new_id = collectionId + base + "-" + attempts + ext;
            if (new_id.length() > MAXIMUM_RESOURCE_ID_LENGTH) {
                throw new IdLengthException(new_id);
            }
            if (!siblings.contains(new_id)) {
                try {
                    edit = addResource(new_id);
                    edit.setContentType(type);
                    edit.setContent(content);
                    if (groups == null || groups.isEmpty()) {
                        // access is inherited (the default)
                    } else {
                        edit.setGroupAccess(groups);
                        // TODO: Need to deal with failure here
                    }

                    addProperties(edit.getPropertiesEdit(), properties);
                    // commit the change
                    commitResource(edit, priority);

                    trying = false;
                } catch (IdUsedException ignore) {
                    // try again
                }
            }
            attempts++;
            if (attempts > limit) {
                throw new IdUniquenessException(new_id);
            }
        }
    }
    return edit;

}