Example usage for java.util SortedSet iterator

List of usage examples for java.util SortedSet iterator

Introduction

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

Prototype

Iterator<E> iterator();

Source Link

Document

Returns an iterator over the elements in this set.

Usage

From source file:br.gov.jfrj.siga.ex.bl.ExBL.java

private void encaixar(SortedSet<ExMarca> setA, SortedSet<ExMarca> setB, Set<ExMarca> incluir,
        Set<ExMarca> excluir, Set<Par<ExMarca, ExMarca>> atualizar) {
    Iterator<ExMarca> ia = setA.iterator();
    Iterator<ExMarca> ib = setB.iterator();

    ExMarca a = null;/*from w  w w.java 2 s  .  c  o  m*/
    ExMarca b = null;

    if (ia.hasNext())
        a = ia.next();
    if (ib.hasNext())
        b = ib.next();
    while (a != null || b != null) {
        if ((a == null) || (b != null && a.compareTo(b) > 0)) {
            // Existe em setB, mas nao existe em setA
            incluir.add(b);
            if (ib.hasNext())
                b = ib.next();
            else
                b = null;
        } else if (b == null || (a != null && b.compareTo(a) > 0)) {
            // Existe em setA, mas nao existe em setB
            excluir.add(a);
            if (ia.hasNext())
                a = ia.next();
            else
                a = null;
        } else {
            // O registro existe nos dois
            atualizar.add(new Par<ExMarca, ExMarca>(a, b));
            if (ib.hasNext())
                b = ib.next();
            else
                b = null;
            if (ia.hasNext())
                a = ia.next();
            else
                a = null;
        }
    }
    ib = null;
    ia = null;
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.owlapi.OwlApi2LG.java

/**
 * Resolve and assign all property information contained by the given RDF
 * resource to the EMF Entity.//from w  ww  . j  av  a  2s  . co m
 * 
 * @param lgEntity
 * @param owlClass
 * 
 *            Last updated: 05/28/2008
 */
protected void resolveEntityProperties(Entity lgEntity, OWLEntity owlClass) {
    String rdfName = getLocalName(owlClass);

    // Temporary container for EMF properties.
    // Note: The EMF object does not enforce order. However, the XSD models
    // have required properties to occur in the XML in a specific order.
    // The comparator ensures a compatible order is maintained.
    SortedSet sortedProps = new TreeSet(propertyComparator);
    // Counters to use in property ID assignment to keep things unique
    // and track assigned presentation count.
    int i = 0;
    int presentationCount = 0;

    for (OWLAnnotationAssertionAxiom annotationAxiom : ontology
            .getAnnotationAssertionAxioms(owlClass.getIRI())) {

        String propName = getLocalName(annotationAxiom.getProperty());
        // Do we care about this rdf property?
        if (isNoop(propName) || isNoopNamespace(propName))
            continue;

        // Do we have an class match?
        String propClass = owlDatatypeName2lgPropClass_.get(propName);
        if (isNoop(propClass))
            continue;

        // Determine the property name and datatype ...
        String lgDType = owlDatatypeName2lgDatatype_.get(propName);
        String lgLabel = owlDatatypeName2label_.get(propName);
        OWLAnnotationValue value = annotationAxiom.getValue();
        String resolvedText = "";
        if (value instanceof OWLLiteral) {
            OWLLiteral literal = (OWLLiteral) value;
            resolvedText = literal.getLiteral();
        } else if (value instanceof IRI) {
            IRI iri_v = (IRI) value;
            resolvedText = iri_v.toString();
        } else {
            resolvedText = renderer.render(value);
        }

        // Interpret RDF property value(s) ...
        // Special case for handling concept code and status, which are
        // set directly as attributes on the LexGrid concept.
        if (propName.matches(prefManager.getMatchPattern_conceptCode())) {
            lgEntity.setEntityCode(resolvedText);
        } else if (lgLabel != null && lgLabel.matches(prefManager.getMatchPattern_conceptStatus())) {
            lgEntity.setStatus(resolvedText);
            if (resolvedText.matches(prefManager.getMatchPattern_inactiveStatus()))
                lgEntity.setIsActive(false);
        }
        // Otherwise instantiate a new EMF property and add the new
        // property to the list to eventually add to the concept.
        else {
            Property newProp = resolveProp(annotationAxiom, propClass, generatePropertyID(++i), lgLabel,
                    lgDType, getNameSpace(annotationAxiom.getProperty()), resolvedText, null);
            if (newProp.getValue() != null) {
                sortedProps.add(newProp);
                if (newProp instanceof Presentation)
                    presentationCount++;
            }
        }

    }

    // The LexGrid model requires a matching presentation for the entity
    // description. If no presentations exist, manufacture a default to
    // satisfy the requirement. If created, the name of the new property
    // is set to indicate where the value was found. Also support
    // explicit requests for "rdfs:label" as the preferred property.
    boolean generatePreferred = prefManager.getPrioritized_presentation_names().size() == 0
            || OwlApi2LGConstants.PROPNAME_RDFS_LABEL
                    .equalsIgnoreCase(prefManager.getPrioritized_presentation_names().get(0))
            || presentationCount == 0;
    if (generatePreferred) {
        String entityDesc = lgEntity.getEntityDescription().getContent();
        sortedProps.add(CreateUtils.createPresentation(generatePropertyID(++i),
                rdfName.equals(entityDesc) ? OwlApi2LGConstants.PROPNAME_RDF_ID
                        : OwlApi2LGConstants.PROPNAME_RDFS_LABEL,
                entityDesc, true, lgSupportedMappings_, null, null));
    }

    // Track assignment of preferred presentation and definition.
    // For presentation, check to see if preference was set above.
    boolean assignedPreferredPres = generatePreferred;
    boolean assignedPreferredDefn = false;

    // Iterate through properties; stop when complete or if both a preferred
    // presentation and definition have been assigned ...
    for (Iterator props = sortedProps.iterator(); props.hasNext()
            && !(assignedPreferredPres && assignedPreferredDefn);) {
        Object prop = props.next();
        if (!assignedPreferredPres && (prop instanceof Presentation)) {
            // Tag the property
            Presentation pres = (Presentation) prop;
            pres.setIsPreferred(Boolean.TRUE);

            // Entity description on concept should match preferred
            // presentation.
            EntityDescription ed = new EntityDescription();
            ed.setContent(((Presentation) prop).getValue().getContent());
            lgEntity.setEntityDescription(ed);

            // Remember that a preferred presentation was assigned ...
            assignedPreferredPres = true;
        }
        if (!assignedPreferredDefn && (prop instanceof Definition)) {
            // Tag the definition
            ((Definition) prop).setIsPreferred(Boolean.TRUE);

            // Remember that a preferred definition was assigned ...
            assignedPreferredDefn = true;
        }
    }

    // Updated on 05/28/2008: It was decided that we also need to
    // hook all the OWLDatatypeProperties for a particular concept
    // as "Concept Properties". This will assist in the visualization
    // of the concepts in a browser. The idea is that the "concept" is
    // stored as the "Range", and the "PropertyText" corresponds to the
    // "range" of the property. However, note that these
    // OWLDatatypeProperties are also represented in the "Relations"
    // container as "Associations", since they may have relations
    // between themselves: e.g., subPropertyOf relations.
    // OWLNamedClass myOWLNamedClass = (OWLNamedClass) rdfResource;

    // Added on 01/14/2009 by Satya as Concept and its
    // properties can be created for OWLObjectProperty's as well.

    for (OWLDatatype prop : owlClass.getDatatypesInSignature()) {
        String propertyName = getLocalName(prop);
        DataRangeType range = prop.getDataRangeType();
        if (range != null) {
            String propertyRangeName = range.getName();
            Property lgProp = CreateUtils.createProperty(generatePropertyID(++i), propertyName,
                    propertyRangeName, lgSupportedMappings_, prop.getIRI().toString(), null);
            sortedProps.add(lgProp);
        }
    }

    //Giving the One of data properties full definition status.
    for (OWLDataProperty prop : owlClass.getDataPropertiesInSignature()) {
        String propertyName = prop.getIRI().getFragment();
        Set<OWLDataRange> ranges = prop.getRanges(ontology);
        if (!ranges.isEmpty()) {
            OWLDataRange range = prop.getRanges(ontology).iterator().next();
            if (range instanceof OWLDataOneOf) {
                OWLDataOneOfImpl oneOf = (OWLDataOneOfImpl) range;
                for (OWLLiteral lit : oneOf.getValues()) {
                    String literal = lit.getLiteral();
                    Property lgProp = CreateUtils.createDefinition(generatePropertyID(++i), propertyName,
                            literal, false, lgSupportedMappings_, prop.getIRI().toString(), "");
                    sortedProps.add(lgProp);
                }
            }
        }
    }

    // Now add all the sorted properties to the concept.
    for (Iterator<? extends Property> lgProps = sortedProps.iterator(); lgProps.hasNext();) {
        Property lgProp = lgProps.next();
        lgEntity.addAnyProperty(lgProp);
    }

}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.protegeOwl.ProtegeOwl2LG.java

/**
 * Resolve and assign all property information contained by the given RDF
 * resource to the EMF Entity.//from ww  w.j av  a2s .com
 * 
 * @param lgEntity
 * @param rdfResource
 * 
 *            Last updated: 05/28/2008
 */
protected void resolveEntityProperties(Entity lgEntity, RDFResource rdfResource) {
    String rdfName = rdfResource.getLocalName();

    // Temporary container for EMF properties.
    // Note: The EMF object does not enforce order. However, the XSD models
    // have required properties to occur in the XML in a specific order.
    // The comparator ensures a compatible order is maintained.
    SortedSet sortedProps = new TreeSet(propertyComparator);
    // Counters to use in property ID assignment to keep things unique
    // and track assigned presentation count.
    int i = 0;
    int presentationCount = 0;

    // Start with properties from RDF-defined property set ...
    for (Iterator props = rdfResource.getRDFProperties().iterator(); props.hasNext();) {
        RDFProperty prop = (RDFProperty) props.next();
        String propName = getRDFResourceLocalName(prop);

        // Do we care about this rdf property?
        if (isNoop(propName) || isNoopNamespace(propName))
            continue;

        // Do we have an EMF class match?
        String propClass = owlDatatypeName2lgPropClass_.get(propName);
        if (isNoop(propClass))
            continue;

        // Determine the EMF property name and datatype ...
        String lgDType = owlDatatypeName2lgDatatype_.get(propName);
        String lgLabel = owlDatatypeName2label_.get(propName);

        // Interpret RDF property value(s) ...
        for (Iterator values = rdfResource.getPropertyValues(prop).iterator(); values.hasNext();) {
            Object value = values.next();
            String resolvedText = resolveRDFText(rdfResource, value);
            String lang = null;
            if (value instanceof DefaultRDFSLiteral)
                lang = ((DefaultRDFSLiteral) value).getLanguage();

            // Special case for handling concept code and status, which are
            // set directly as attributes on the LexGrid concept.
            if (propName.matches(prefManager.getMatchPattern_conceptCode())) {
                lgEntity.setEntityCode(resolvedText);
            } else if (lgLabel != null && lgLabel.matches(prefManager.getMatchPattern_conceptStatus())) {
                lgEntity.setStatus(resolvedText);
                if (resolvedText.matches(prefManager.getMatchPattern_inactiveStatus()))
                    lgEntity.setIsActive(false);
            }
            // Otherwise instantiate a new EMF property and add the new
            // property to the list to eventually add to the concept.
            else {
                Property newProp = resolveProp(prop, propClass, generatePropertyID(++i), lgLabel, lgDType,
                        prop.getNamespace(), resolvedText, lang);
                if (newProp.getValue() != null) {
                    sortedProps.add(newProp);
                    if (newProp instanceof Presentation)
                        presentationCount++;
                }
            }
        }
    }

    // Indicate whether the concept is primitive (no equivalent classes) ...
    Collection values = rdfResource
            .getPropertyValues(rdfResource.getOWLModel().getOWLEquivalentClassProperty());
    if (values == null || values.isEmpty()) {
        Property lgProp = CreateUtils.createProperty(generatePropertyID(++i),
                prefManager.getPropertyName_primitive(), "true", lgSupportedMappings_, null, null);
        sortedProps.add(lgProp);
    } else {
        Property lgProp = CreateUtils.createProperty(generatePropertyID(++i),
                prefManager.getPropertyName_primitive(), "false", lgSupportedMappings_, null, null);
        sortedProps.add(lgProp);
    }

    // The LexGrid model requires a matching presentation for the entity
    // description. If no presentations exist, manufacture a default to
    // satisfy the requirement. If created, the name of the new property
    // is set to indicate where the value was found. Also support
    // explicit requests for "rdfs:label" as the preferred property.
    boolean generatePreferred = prefManager.getPrioritized_presentation_names().size() == 0
            || ProtegeOwl2LGConstants.PROPNAME_RDFS_LABEL
                    .equalsIgnoreCase(prefManager.getPrioritized_presentation_names().get(0))
            || presentationCount == 0;
    if (generatePreferred) {
        String entityDesc = lgEntity.getEntityDescription().getContent();
        sortedProps.add(CreateUtils.createPresentation(generatePropertyID(++i),
                rdfName.equals(entityDesc) ? ProtegeOwl2LGConstants.PROPNAME_RDF_ID
                        : ProtegeOwl2LGConstants.PROPNAME_RDFS_LABEL,
                entityDesc, true, lgSupportedMappings_, null, null));
    }

    // Track assignment of preferred presentation and definition.
    // For presentation, check to see if preference was set above.
    boolean assignedPreferredPres = generatePreferred;
    boolean assignedPreferredDefn = false;

    // Iterate through properties; stop when complete or if both a preferred
    // presentation and definition have been assigned ...
    for (Iterator props = sortedProps.iterator(); props.hasNext()
            && !(assignedPreferredPres && assignedPreferredDefn);) {
        Object prop = props.next();
        if (!assignedPreferredPres && (prop instanceof Presentation)) {
            // Tag the property
            Presentation pres = (Presentation) prop;
            pres.setIsPreferred(Boolean.TRUE);

            // Entity description on concept should match preferred
            // presentation.
            EntityDescription ed = new EntityDescription();
            ed.setContent(((Presentation) prop).getValue().getContent());
            lgEntity.setEntityDescription(ed);

            // Remember that a preferred presentation was assigned ...
            assignedPreferredPres = true;
        }
        if (!assignedPreferredDefn && (prop instanceof Definition)) {
            // Tag the definition
            ((Definition) prop).setIsPreferred(Boolean.TRUE);

            // Remember that a preferred definition was assigned ...
            assignedPreferredDefn = true;
        }
    }

    // Updated on 05/28/2008: It was decided that we also need to
    // hook all the OWLDatatypeProperties for a particular concept
    // as "Concept Properties". This will assist in the visualization
    // of the concepts in a browser. The idea is that the "concept" is
    // stored as the "Range", and the "PropertyText" corresponds to the
    // "range" of the property. However, note that these
    // OWLDatatypeProperties are also represented in the "Relations"
    // container as "Associations", since they may have relations
    // between themselves: e.g., subPropertyOf relations.
    // OWLNamedClass myOWLNamedClass = (OWLNamedClass) rdfResource;

    // Added on 01/14/2009 by Satya as Concept and its
    // properties can be created for OWLObjectProperty's as well.
    if (rdfResource instanceof RDFSNamedClass) {
        RDFSNamedClass rdfsNamedClass = (RDFSNamedClass) rdfResource;
        for (Iterator classProps = rdfsNamedClass.getAssociatedProperties().iterator(); classProps.hasNext();) {
            RDFProperty prop = (RDFProperty) classProps.next();
            if (prop instanceof OWLDatatypeProperty) {
                String propertyName = getFromLastIndexOfColonOrHash(prop.getBrowserText());

                RDFSDatatype range = prop.getRangeDatatype();
                if (range != null) {
                    String propertyRangeName = getRDFResourceLocalName(range);
                    Property lgProp = CreateUtils.createProperty(generatePropertyID(++i), propertyName,
                            propertyRangeName, lgSupportedMappings_, prop.getURI(), null);
                    sortedProps.add(lgProp);
                }
            }
        }
    }

    // Now add all the sorted properties to the concept.
    for (Iterator<? extends Property> lgProps = sortedProps.iterator(); lgProps.hasNext();) {
        Property lgProp = lgProps.next();
        lgEntity.addAnyProperty(lgProp);
    }

}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.protegeOwl.ProtegeOwl2LG.java

/**
 * This method associates OWLIndividuals and their properties: OWLDatatype
 * and OWLObject. Essentially, the is to tag each individual with the
 * property so that it helps in visualization and navigation.
 * /*from  w  w  w. j a  v  a2  s  .  c  om*/
 * @param lgInstance
 * @param rdfResource
 */
protected void resolveIndividualProperties(Entity lgInstance, RDFResource rdfResource) {

    // Temporary container for EMF properties.
    // Note: The EMF object does not enforce order. However, the XSD models
    // have required properties to occur in the XML in a specific order.
    // The comparator ensures a compatible order is maintained.
    SortedSet sortedProps = new TreeSet(propertyComparator);

    // Counter to use in property ID assignment to keep things unique.
    int i = 0;

    RDFSNamedClass myClass = null;
    for (Iterator userDefinedClasses = owlModel_.getUserDefinedOWLNamedClasses().iterator(); userDefinedClasses
            .hasNext();) {
        myClass = (RDFSNamedClass) userDefinedClasses.next();

        if (rdfResource.hasRDFType(myClass, false)) {
            String className = getRDFResourceLocalName(myClass);

            // Add this information as an instanceProperty.
            Property lgProp = CreateUtils.createProperty(generatePropertyID(++i), "isInstanceOf", className,
                    lgSupportedMappings_, null, null);
            sortedProps.add(lgProp);

            break;
        }
    }

    String rdfName = rdfResource.getLocalName();

    int presentationCount = 0;

    // Start with properties from RDF-defined property set ...
    for (Iterator props = rdfResource.getRDFProperties().iterator(); props.hasNext();) {
        RDFProperty prop = (RDFProperty) props.next();
        String propName = getRDFResourceLocalName(prop);
        // Do we care about this rdf property?
        if (isNoop(propName) || isNoopNamespace(propName))
            continue;

        // Do we have an EMF class match?
        String propClass = owlDatatypeName2lgPropClass_.get(propName);
        if (isNoop(propClass))
            continue;

        // Determine the EMF property name and datatype ...
        String lgDType = owlDatatypeName2lgDatatype_.get(propName);
        String lgLabel = owlDatatypeName2label_.get(propName);

        // Interpret RDF property value(s) ...
        for (Iterator values = rdfResource.getPropertyValues(prop).iterator(); values.hasNext();) {
            Object value = values.next();
            String resolvedText = resolveRDFText(rdfResource, value);

            // Special case for handling concept code and status, which are
            // set directly as attributes on the LexGrid instance.
            if (propName.matches(prefManager.getMatchPattern_conceptCode())) {
                lgInstance.setEntityCode(resolvedText);
            } else if (lgLabel != null && lgLabel.matches(prefManager.getMatchPattern_conceptStatus())) {
                lgInstance.setStatus(resolvedText);
                if (resolvedText.matches(prefManager.getMatchPattern_inactiveStatus()))
                    lgInstance.setIsActive(false);
            }
            // Otherwise instantiate a new EMF property and add the new
            // property to the list to eventually add to the instance.
            else {
                Property newProp = resolveProp(prop, propClass, generatePropertyID(++i), lgLabel, lgDType,
                        prop.getNamespace(), resolvedText, null);
                if (newProp.getValue() != null) {
                    sortedProps.add(newProp);
                    if (newProp instanceof Presentation)
                        presentationCount++;
                }
            }
        }
    }

    // Indicate whether the instance is primitive (no equivalent classes)
    // ...
    Collection values = rdfResource
            .getPropertyValues(rdfResource.getOWLModel().getOWLEquivalentClassProperty());
    if (values == null || values.isEmpty()) {
        Property lgProp = CreateUtils.createProperty(generatePropertyID(++i),
                prefManager.getPropertyName_primitive(), "true", lgSupportedMappings_, null, null);
        sortedProps.add(lgProp);
    } else {
        Property lgProp = CreateUtils.createProperty(generatePropertyID(++i),
                prefManager.getPropertyName_primitive(), "false", lgSupportedMappings_, null, null);
        sortedProps.add(lgProp);
    }

    // The LexGrid model requires a matching presentation for the entity
    // description. If no presentations exist, manufacture a default to
    // satisfy the requirement. If created, the name of the new property
    // is set to indicate where the value was found. Also support
    // explicit requests for "rdfs:label" as the preferred property.
    boolean generatePreferred = prefManager.getPrioritized_presentation_names().size() == 0
            || ProtegeOwl2LGConstants.PROPNAME_RDFS_LABEL
                    .equalsIgnoreCase(prefManager.getPrioritized_presentation_names().get(0))
            || presentationCount == 0;
    if (generatePreferred) {
        String entityDesc = lgInstance.getEntityDescription().getContent();
        sortedProps.add(CreateUtils.createPresentation(generatePropertyID(++i),
                rdfName.equals(entityDesc) ? ProtegeOwl2LGConstants.PROPNAME_RDF_ID
                        : ProtegeOwl2LGConstants.PROPNAME_RDFS_LABEL,
                entityDesc, true, lgSupportedMappings_, null, null));
    }

    // Track assignment of preferred presentation and definition.
    // For presentation, check to see if preference was set above.
    boolean assignedPreferredPres = generatePreferred;
    boolean assignedPreferredDefn = false;

    // Iterate through properties; stop when complete or if both a preferred
    // presentation and definition have been assigned ...
    for (Iterator<? extends Property> props = sortedProps.iterator(); props.hasNext()
            && !(assignedPreferredPres && assignedPreferredDefn);) {
        Object prop = props.next();
        if (!assignedPreferredPres && (prop instanceof Presentation)) {
            // Tag the property
            Presentation pres = (Presentation) prop;
            pres.setIsPreferred(Boolean.TRUE);
            // Entity description on instance should match preferred
            // presentation.
            EntityDescription ed = new EntityDescription();
            ed.setContent(((Presentation) prop).getValue().getContent());
            lgInstance.setEntityDescription(ed);
            // Remember that a preferred presentation was assigned ...
            assignedPreferredPres = true;
        }
        if (!assignedPreferredDefn && (prop instanceof Definition)) {
            // Tag the definition
            ((Definition) prop).setIsPreferred(Boolean.TRUE);
            // Remember that a preferred definition was assigned ...
            assignedPreferredDefn = true;
        }
    }

    // Now add all the sorted properties to the instance.
    for (Iterator<? extends Property> lgProps = sortedProps.iterator(); lgProps.hasNext();) {
        Property lgProp = (Property) lgProps.next();
        lgInstance.addAnyProperty(lgProp);
    }
}

From source file:org.sakaiproject.content.tool.ResourcesAction.java

/**
* Develop a list of all the site collections that there are to page.
* Sort them as appropriate, and apply search criteria.
*///from  w ww  . j  av  a  2 s  .c om
protected List<ListItem> readAllResources(SessionState state) {
    logger.debug(this + ".readAllResources()");
    ResourceTypeRegistry registry = (ResourceTypeRegistry) state.getAttribute(STATE_RESOURCES_TYPE_REGISTRY);
    if (registry == null) {
        registry = (ResourceTypeRegistry) ComponentManager
                .get("org.sakaiproject.content.api.ResourceTypeRegistry");
        state.setAttribute(STATE_RESOURCES_TYPE_REGISTRY, registry);
    }

    List<ListItem> other_sites = new ArrayList<ListItem>();

    String collectionId = (String) state.getAttribute(STATE_COLLECTION_ID);
    Set<String> expandedCollections = getExpandedCollections(state);

    Comparator userSelectedSort = (Comparator) state.getAttribute(STATE_LIST_VIEW_SORT);

    Boolean showRemove = (Boolean) state.getAttribute(STATE_SHOW_REMOVE_ACTION);
    boolean showRemoveAction = showRemove != null && showRemove.booleanValue();

    Boolean showMove = (Boolean) state.getAttribute(STATE_SHOW_MOVE_ACTION);
    boolean showMoveAction = showMove != null && showMove.booleanValue();

    Boolean showCopy = (Boolean) state.getAttribute(STATE_SHOW_COPY_ACTION);
    boolean showCopyAction = showCopy != null && showCopy.booleanValue();

    // add user's personal workspace
    User user = UserDirectoryService.getCurrentUser();
    String userId = user.getId();
    String wsId = SiteService.getUserSiteId(userId);
    String wsCollectionId = ContentHostingService.getSiteCollection(wsId);
    List<String> items_to_be_copied = (List<String>) state.getAttribute(STATE_ITEMS_TO_BE_COPIED);
    List<String> items_to_be_moved = (List<String>) state.getAttribute(STATE_ITEMS_TO_BE_MOVED);

    if (!collectionId.equals(wsCollectionId)) {
        try {
            ContentCollection wsCollection = ContentHostingService.getCollection(wsCollectionId);
            ListItem wsRoot = ListItem.getListItem(wsCollection, null, registry, false, expandedCollections,
                    items_to_be_moved, items_to_be_copied, 0, userSelectedSort, false, null);
            other_sites.add(wsRoot);
        } catch (IdUnusedException e) {
            // TODO Auto-generated catch block
            logger.warn("IdUnusedException ", e);
        } catch (TypeException e) {
            // TODO Auto-generated catch block
            logger.warn("TypeException ", e);
        } catch (PermissionException e) {
            // TODO Auto-generated catch block
            logger.warn("PermissionException ", e);
        }
    }

    /*
    * add all other sites user has access to
    * NOTE: This does not (and should not) get all sites for admin.  
    *       Getting all sites for admin is too big a request and
    *       would result in too big a display to render in html.
    */
    Map othersites = ContentHostingService.getCollectionMap();
    SortedSet sort = new TreeSet();
    for (Iterator<Entry<String, String>> mapIter = othersites.entrySet().iterator(); mapIter.hasNext();) {
        Entry<String, String> entry = mapIter.next();
        sort.add(entry.getValue() + DELIM + entry.getKey());
    }

    Iterator sortIt = sort.iterator();
    while (sortIt.hasNext()) {
        String keyvalue = (String) sortIt.next();
        String displayName = keyvalue.substring(0, keyvalue.lastIndexOf(DELIM));
        String collId = keyvalue.substring(keyvalue.lastIndexOf(DELIM) + 1);
        if (!collectionId.equals(collId) && !wsCollectionId.equals(collId)) {
            ContentCollection collection;
            try {
                collection = ContentHostingService.getCollection(collId);
                ListItem root = ListItem.getListItem(collection, null, registry, false, expandedCollections,
                        items_to_be_moved, items_to_be_copied, 0, null, false, null);
                root.setName(displayName);
                other_sites.add(root);
            } catch (IdUnusedException e) {
                // TODO Auto-generated catch block
                logger.warn("IdUnusedException " + e);
            } catch (TypeException e) {
                // TODO Auto-generated catch block
                logger.warn("TypeException " + e);
            } catch (PermissionException e) {
                // TODO Auto-generated catch block
                logger.warn("PermissionException " + e);
            }
        }
    }

    return other_sites;
}

From source file:com.android.mms.transaction.MessagingNotification.java

/**
 * updateNotification is *the* main function for building the actual notification handed to
 * the NotificationManager/*from  w w w  . j  a  va2 s . co m*/
 * @param context
 * @param newThreadId the new thread id
 * @param uniqueThreadCount
 * @param notificationSet the set of notifications to display
 */
private static void updateNotification(Context context, long newThreadId, int uniqueThreadCount,
        SortedSet<NotificationInfo> notificationSet) {
    boolean isNew = newThreadId != THREAD_NONE;
    CMConversationSettings conversationSettings = CMConversationSettings.getOrNew(context, newThreadId);

    // If the user has turned off notifications in settings, don't do any notifying.
    if ((isNew && !conversationSettings.getNotificationEnabled())
            || !MessagingPreferenceActivity.getNotificationEnabled(context)) {
        if (DEBUG) {
            Log.d(TAG, "updateNotification: notifications turned off in prefs, bailing");
        }
        return;
    }

    // Figure out what we've got -- whether all sms's, mms's, or a mixture of both.
    final int messageCount = notificationSet.size();
    NotificationInfo mostRecentNotification = notificationSet.first();

    final NotificationCompat.Builder noti = new NotificationCompat.Builder(context)
            .setWhen(mostRecentNotification.mTimeMillis);

    if (isNew) {
        noti.setTicker(mostRecentNotification.mTicker);
    }

    // If we have more than one unique thread, change the title (which would
    // normally be the contact who sent the message) to a generic one that
    // makes sense for multiple senders, and change the Intent to take the
    // user to the conversation list instead of the specific thread.

    // Cases:
    //   1) single message from single thread - intent goes to ComposeMessageActivity
    //   2) multiple messages from single thread - intent goes to ComposeMessageActivity
    //   3) messages from multiple threads - intent goes to ConversationList

    final Resources res = context.getResources();
    String title = null;
    Bitmap avatar = null;
    PendingIntent pendingIntent = null;
    boolean isMultiNewMessages = MessageUtils.isMailboxMode() ? messageCount > 1 : uniqueThreadCount > 1;
    if (isMultiNewMessages) { // messages from multiple threads
        Intent mainActivityIntent = getMultiThreadsViewIntent(context);
        pendingIntent = PendingIntent.getActivity(context, 0, mainActivityIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        title = context.getString(R.string.message_count_notification, messageCount);
    } else { // same thread, single or multiple messages
        title = mostRecentNotification.mTitle;
        avatar = mostRecentNotification.mSender.getAvatar(context);
        noti.setSubText(mostRecentNotification.mSimName); // no-op in single SIM case
        if (avatar != null) {
            // Show the sender's avatar as the big icon. Contact bitmaps are 96x96 so we
            // have to scale 'em up to 128x128 to fill the whole notification large icon.
            final int idealIconHeight = res
                    .getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
            final int idealIconWidth = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
            noti.setLargeIcon(BitmapUtil.getRoundedBitmap(avatar, idealIconWidth, idealIconHeight));
        }

        pendingIntent = PendingIntent.getActivity(context, 0, mostRecentNotification.mClickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }
    // Always have to set the small icon or the notification is ignored
    noti.setSmallIcon(R.drawable.stat_notify_sms);

    NotificationManagerCompat nm = NotificationManagerCompat.from(context);

    // Update the notification.
    noti.setContentTitle(title).setContentIntent(pendingIntent)
            .setColor(context.getResources().getColor(R.color.mms_theme_color))
            .setCategory(Notification.CATEGORY_MESSAGE).setPriority(Notification.PRIORITY_DEFAULT); // TODO: set based on contact coming
                                                                                                                                                                                                                                  // from a favorite.

    // Tag notification with all senders.
    for (NotificationInfo info : notificationSet) {
        Uri peopleReferenceUri = info.mSender.getPeopleReferenceUri();
        if (peopleReferenceUri != null) {
            noti.addPerson(peopleReferenceUri.toString());
        }
    }

    int defaults = 0;

    if (isNew) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        if (conversationSettings.getVibrateEnabled()) {
            String pattern = conversationSettings.getVibratePattern();

            if (!TextUtils.isEmpty(pattern)) {
                noti.setVibrate(parseVibratePattern(pattern));
            } else {
                defaults |= Notification.DEFAULT_VIBRATE;
            }
        }

        String ringtoneStr = conversationSettings.getNotificationTone();
        noti.setSound(TextUtils.isEmpty(ringtoneStr) ? null : Uri.parse(ringtoneStr));
        Log.d(TAG, "updateNotification: new message, adding sound to the notification");
    }

    defaults |= Notification.DEFAULT_LIGHTS;

    noti.setDefaults(defaults);

    // set up delete intent
    noti.setDeleteIntent(PendingIntent.getBroadcast(context, 0, sNotificationOnDeleteIntent, 0));

    // See if QuickMessage pop-up support is enabled in preferences
    boolean qmPopupEnabled = MessagingPreferenceActivity.getQuickMessageEnabled(context);

    // Set up the QuickMessage intent
    Intent qmIntent = null;
    if (mostRecentNotification.mIsSms) {
        // QuickMessage support is only for SMS
        qmIntent = new Intent();
        qmIntent.setClass(context, QuickMessagePopup.class);
        qmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        qmIntent.putExtra(QuickMessagePopup.SMS_FROM_NAME_EXTRA, mostRecentNotification.mSender.getName());
        qmIntent.putExtra(QuickMessagePopup.SMS_FROM_NUMBER_EXTRA, mostRecentNotification.mSender.getNumber());
        qmIntent.putExtra(QuickMessagePopup.SMS_NOTIFICATION_OBJECT_EXTRA, mostRecentNotification);
    }

    // Start getting the notification ready
    final Notification notification;

    //Create a WearableExtender to add actions too
    WearableExtender wearableExtender = new WearableExtender();

    if (messageCount == 1 || uniqueThreadCount == 1) {
        // Add the Quick Reply action only if the pop-up won't be shown already
        if (!qmPopupEnabled && qmIntent != null) {

            // This is a QR, we should show the keyboard when the user taps to reply
            qmIntent.putExtra(QuickMessagePopup.QR_SHOW_KEYBOARD_EXTRA, true);

            // Create the pending intent and add it to the notification
            CharSequence qmText = context.getText(R.string.menu_reply);
            PendingIntent qmPendingIntent = PendingIntent.getActivity(context, 0, qmIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            noti.addAction(R.drawable.ic_reply, qmText, qmPendingIntent);

            //Wearable
            noti.extend(wearableExtender.addAction(
                    new NotificationCompat.Action.Builder(R.drawable.ic_reply, qmText, qmPendingIntent)
                            .build()));
        }

        // Add the 'Mark as read' action
        CharSequence markReadText = context.getText(R.string.qm_mark_read);
        Intent mrIntent = new Intent();
        mrIntent.setClass(context, QmMarkRead.class);
        mrIntent.putExtra(QmMarkRead.SMS_THREAD_ID, mostRecentNotification.mThreadId);
        PendingIntent mrPendingIntent = PendingIntent.getBroadcast(context, 0, mrIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        noti.addAction(R.drawable.ic_mark_read, markReadText, mrPendingIntent);

        // Add the Call action
        CharSequence callText = context.getText(R.string.menu_call);
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + mostRecentNotification.mSender.getNumber()));
        PendingIntent callPendingIntent = PendingIntent.getActivity(context, 0, callIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        noti.addAction(R.drawable.ic_menu_call, callText, callPendingIntent);

        //Wearable
        noti.extend(wearableExtender.addAction(
                new NotificationCompat.Action.Builder(R.drawable.ic_menu_call, callText, callPendingIntent)
                        .build()));

        //Set up remote input
        String replyLabel = context.getString(R.string.qm_wear_voice_reply);
        RemoteInput remoteInput = new RemoteInput.Builder(QuickMessageWear.EXTRA_VOICE_REPLY)
                .setLabel(replyLabel).build();
        //Set up pending intent for voice reply
        Intent voiceReplyIntent = new Intent(context, QuickMessageWear.class);
        voiceReplyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        voiceReplyIntent.putExtra(QuickMessageWear.SMS_CONATCT, mostRecentNotification.mSender.getName());
        voiceReplyIntent.putExtra(QuickMessageWear.SMS_SENDER, mostRecentNotification.mSender.getNumber());
        voiceReplyIntent.putExtra(QuickMessageWear.SMS_THEAD_ID, mostRecentNotification.mThreadId);
        PendingIntent voiceReplyPendingIntent = PendingIntent.getActivity(context, 0, voiceReplyIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        //Wearable voice reply action
        NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_reply,
                context.getString(R.string.qm_wear_reply_by_voice), voiceReplyPendingIntent)
                        .addRemoteInput(remoteInput).build();
        noti.extend(wearableExtender.addAction(action));
    }

    if (messageCount == 1) {
        // We've got a single message

        // This sets the text for the collapsed form:
        noti.setContentText(mostRecentNotification.formatBigMessage(context));

        if (mostRecentNotification.mAttachmentBitmap != null) {
            // The message has a picture, show that

            NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle(noti)
                    .bigPicture(mostRecentNotification.mAttachmentBitmap)
                    .setSummaryText(mostRecentNotification.formatPictureMessage(context));

            notification = noti.setStyle(bigPictureStyle).build();
        } else {
            // Show a single notification -- big style with the text of the whole message
            NotificationCompat.BigTextStyle bigTextStyle1 = new NotificationCompat.BigTextStyle(noti)
                    .bigText(mostRecentNotification.formatBigMessage(context));

            notification = noti.setStyle(bigTextStyle1).build();
        }
        if (DEBUG) {
            Log.d(TAG, "updateNotification: single message notification");
        }
    } else {
        // We've got multiple messages
        if (!isMultiNewMessages) {
            // We've got multiple messages for the same thread.
            // Starting with the oldest new message, display the full text of each message.
            // Begin a line for each subsequent message.
            SpannableStringBuilder buf = new SpannableStringBuilder();
            NotificationInfo infos[] = notificationSet.toArray(new NotificationInfo[messageCount]);
            int len = infos.length;
            for (int i = len - 1; i >= 0; i--) {
                NotificationInfo info = infos[i];

                buf.append(info.formatBigMessage(context));

                if (i != 0) {
                    buf.append('\n');
                }
            }

            noti.setContentText(context.getString(R.string.message_count_notification, messageCount));

            // Show a single notification -- big style with the text of all the messages
            NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
            bigTextStyle.bigText(buf)
                    // Forcibly show the last line, with the app's smallIcon in it, if we
                    // kicked the smallIcon out with an avatar bitmap
                    .setSummaryText((avatar == null) ? null : " ");
            notification = noti.setStyle(bigTextStyle).build();
            if (DEBUG) {
                Log.d(TAG, "updateNotification: multi messages for single thread");
            }
        } else {
            // Build a set of the most recent notification per threadId.
            HashSet<Long> uniqueThreads = new HashSet<Long>(messageCount);
            ArrayList<NotificationInfo> mostRecentNotifPerThread = new ArrayList<NotificationInfo>();
            Iterator<NotificationInfo> notifications = notificationSet.iterator();
            while (notifications.hasNext()) {
                NotificationInfo notificationInfo = notifications.next();
                if (!uniqueThreads.contains(notificationInfo.mThreadId)) {
                    uniqueThreads.add(notificationInfo.mThreadId);
                    mostRecentNotifPerThread.add(notificationInfo);
                }
            }
            // When collapsed, show all the senders like this:
            //     Fred Flinstone, Barry Manilow, Pete...
            noti.setContentText(formatSenders(context, mostRecentNotifPerThread));
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(noti);

            // We have to set the summary text to non-empty so the content text doesn't show
            // up when expanded.
            inboxStyle.setSummaryText(" ");

            // At this point we've got multiple messages in multiple threads. We only
            // want to show the most recent message per thread, which are in
            // mostRecentNotifPerThread.
            int uniqueThreadMessageCount = mostRecentNotifPerThread.size();
            int maxMessages = Math.min(MAX_MESSAGES_TO_SHOW, uniqueThreadMessageCount);

            for (int i = 0; i < maxMessages; i++) {
                NotificationInfo info = mostRecentNotifPerThread.get(i);
                inboxStyle.addLine(info.formatInboxMessage(context));
            }
            notification = inboxStyle.build();

            uniqueThreads.clear();
            mostRecentNotifPerThread.clear();

            if (DEBUG) {
                Log.d(TAG, "updateNotification: multi messages," + " showing inboxStyle notification");
            }
        }
    }

    notifyUserIfFullScreen(context, title);
    nm.notify(NOTIFICATION_ID, notification);

    // Trigger the QuickMessage pop-up activity if enabled
    // But don't show the QuickMessage if the user is in a call or the phone is ringing
    if (qmPopupEnabled && qmIntent != null) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE && !ConversationList.mIsRunning
                && !ComposeMessageActivity.mIsRunning) {
            context.startActivity(qmIntent);
        }
    }
}