Changeset 2221


Ignore:
Timestamp:
09/13/12 15:41:58 (12 years ago)
Author:
olhsha
Message:

comparator for abstract description is implemented. Visually it works ok (on Rss of components and Profiles). ToDOs: refactoring -- single out the common part for rss-ing of profiles and components. Write testing for rss-sing of profiles and components.

Location:
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/RssCreator.java

    r2218 r2221  
    5555     * @param descrs
    5656     */
     57    /* public void setDescriptions(List<AbstractDescription> descrs){
     58       
     59        this.descrs = new ArrayList<AbstractDescription>();
     60       
     61        for (AbstractDescription currentdesc : descrs)
     62        {this.descrs.add(currentdesc);};
     63    }/**
     64     *
     65     * @param descrs
     66     */
    5767    public void setComponentDescriptions(List<ComponentDescription> descrs){
    5868       
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/AbstractDescription.java

    r1696 r2221  
    11package clarin.cmdi.componentregistry.model;
    22
     3import java.text.DateFormat;
    34import java.text.ParseException;
    45import java.util.Comparator;
     
    166167        return result;
    167168    }
     169   
    168170    public static final Comparator<? super AbstractDescription> COMPARE_ON_NAME = new Comparator<AbstractDescription>() {
    169171
     
    179181        }
    180182    };
     183   
     184    public static final Comparator<? super AbstractDescription> COMPARE_ON_DATE = new Comparator<AbstractDescription>() {
     185       
     186        //compare two components by the date of registration
     187        // the dates, which are strings accrording to  ISO 8601, compared lexicographically
     188       
     189        public int compare(AbstractDescription o1, AbstractDescription o2) {
     190           
     191            int result = 0;
     192             
     193            DateFormat df = DateFormat.getDateTimeInstance();
     194           
     195            try{
     196            Date d1 = df.parse(o1.getRegistrationDate());
     197            Date d2 = df.parse(o2.getRegistrationDate());
     198           
     199            result = d1.compareTo(d2);
     200           
     201            } catch (ParseException pe)  {return 0;};
     202           
     203           
     204           
     205            return result;
     206        }
     207    };
     208   
    181209}
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/ComponentDescription.java

    r1371 r2221  
    1010
    1111@XmlRootElement(name = "componentDescription")
    12 public class ComponentDescription extends AbstractDescription implements Serializable{
     12public class ComponentDescription extends AbstractDescription implements Serializable {
    1313
    1414    private static final long serialVersionUID = 1L;
    15    
    1615    public static final Comparator<? super ComponentDescription> COMPARE_ON_GROUP_AND_NAME = new Comparator<ComponentDescription>() {
    1716        public int compare(ComponentDescription o1, ComponentDescription o2) {
    1817            int result = 0;
    19             if (o1.getGroupName() != null && o2.getGroupName() != null)
     18            if (o1.getGroupName() != null && o2.getGroupName() != null) {
    2019                result = o1.getGroupName().compareToIgnoreCase(o2.getGroupName());
     20            }
    2121            if (result == 0) {
    2222                if (o1.getName() != null && o2.getName() != null) {
     
    2929        }
    3030    };
     31   
    3132
    3233    public static ComponentDescription createNewDescription() {
     
    3738        return desc;
    3839    }
    39 
    4040}
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r2218 r2221  
    2929import java.util.ArrayList;
    3030import java.util.Arrays;
    31 import java.util.Comparator;
     31import java.util.Collections;
    3232import java.util.List;
    3333import javax.servlet.http.HttpServletRequest;
     
    965965    }
    966966   
     967    /*
     968     * generating rss: commom part for profile and component descriptions
     969     *
     970     */
     971   
     972   /* private Rss getRss(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit,
     973            List<AbstractDescription> descs) throws ComponentRegistryException {
     974       
     975       
     976       
     977        RssCreator rssCreator = new RssCreator();
     978        int limitInt = Integer.parseInt(limit);
     979       
     980        if (descs.size()<limitInt) {limitInt = descs.size();};
     981       
     982        List<AbstractDescription> sublist = descs.subList(0, limitInt);
     983       
     984        Collections.sort(sublist, AbstractDescription.COMPARE_ON_DATE);
     985       
     986        rssCreator.setComponentDescriptions(sublist);
     987       
     988        if (userspace)   {rssCreator.setTitle("Workspace components");}
     989        else {rssCreator.setTitle("Public components");}
     990       
     991         
     992        Rss rss =rssCreator.makeRssChannel();
     993       
     994        LOG.info("Releasing " + limitInt + "most recent registered components into the world sorted by their registration date-and-time");
     995        return rss;
     996    }*/
    967997    ////////////////////////////////////////////////
    968998    @GET
    969999    @Path("/components/rss")
    9701000    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    971     public Rss getRss(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException {
     1001    public Rss getRssComponent(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException {
    9721002       
    973         long start = System.currentTimeMillis();
     1003       
    9741004        List<ComponentDescription> components = getRegistry(getStatus(userspace)).getComponentDescriptions();
    9751005       
     
    9821012       
    9831013        List<ComponentDescription> sublist = components.subList(0, limitInt);
    984         //Collections.sort(sublist, );
     1014       
     1015        Collections.sort(sublist, ComponentDescription.COMPARE_ON_DATE);
    9851016       
    9861017        rssCreator.setComponentDescriptions(sublist);
     
    9921023        Rss rss =rssCreator.makeRssChannel();
    9931024       
    994         LOG.info("Releasing " + limitInt + "most recent registered components into the world sorted by creations day");
     1025        LOG.info("Releasing " + limitInt + "most recent registered components into the world sorted by their registration date-and-time");
    9951026        return rss;
    9961027    }
    9971028   
     1029     ////////////////////////////////////////////////
     1030    @GET
     1031    @Path("/profiles/rss")
     1032    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
     1033    public Rss getRssProfile(@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace, @QueryParam(NUMBER_OF_RSSITEMS) @DefaultValue("20") String limit) throws ComponentRegistryException {
     1034       
     1035        long start = System.currentTimeMillis();
     1036        List<ProfileDescription> profiles = getRegistry(getStatus(userspace)).getProfileDescriptions();
     1037       
     1038       
     1039         
     1040        RssCreator rssCreator = new RssCreator();
     1041        int limitInt = Integer.parseInt(limit);
     1042       
     1043        if (profiles.size()<limitInt) {limitInt = profiles.size();};
     1044       
     1045        List<ProfileDescription> sublist = profiles.subList(0, limitInt);
    9981046       
    999        
    1000        /* public static final Comparator<? super ComponentDescription> COMPARE_ON_GROUP_AND_NAME = new Comparator<ComponentDescription>() {
    1001         public int compare(ComponentDescription o1, ComponentDescription o2) {
    1002             int result = 0;
    1003             if (o1.getGroupName() != null && o2.getGroupName() != null)
    1004                 result = o1.getGroupName().compareToIgnoreCase(o2.getGroupName());
    1005             if (result == 0) {
    1006                 if (o1.getName() != null && o2.getName() != null) {
    1007                     result = o1.getName().compareToIgnoreCase(o2.getName());
    1008                 } else {
    1009                     result = o1.getId().compareTo(o2.getId());
    1010                 }
    1011             }
    1012             return result;
    1013         }
    1014     };*/
     1047        Collections.sort(sublist, ProfileDescription.COMPARE_ON_DATE);
     1048       
     1049        rssCreator.setProfileDescriptions(sublist);
     1050       
     1051        if (userspace)   {rssCreator.setTitle("Workspace profiles");}
     1052        else {rssCreator.setTitle("Public profiles");}
     1053       
     1054         
     1055        Rss rss =rssCreator.makeRssChannel();
     1056       
     1057        LOG.info("Releasing " + limitInt + "most recent registered profiles into the world sorted by their registration date-and-time");
     1058        return rss;
     1059    }   
     1060   
    10151061}
Note: See TracChangeset for help on using the changeset viewer.