Ignore:
Timestamp:
08/11/14 16:07:55 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentUtils.java

    r4098 r5549  
    11package clarin.cmdi.componentregistry.impl;
    22
     3import clarin.cmdi.componentregistry.ComponentRegistryException;
    34import java.text.ParseException;
    45import java.util.ArrayList;
     
    1112import org.springframework.beans.BeanUtils;
    1213
    13 import clarin.cmdi.componentregistry.DatesHelper;
    1414import clarin.cmdi.componentregistry.model.BaseDescription;
    1515import clarin.cmdi.componentregistry.model.ComponentDescription;
     
    1818/**
    1919 * Utilities for working with {@link BaseDescription}s
    20  * 
     20 *
    2121 * @author george.georgovassilis@mpi.nl
    22  * 
     22 *
    2323 */
    2424public class ComponentUtils {
    2525
    2626    public static boolean isProfileId(String componentId) {
    27         return ("" + componentId).startsWith(ProfileDescription.PROFILE_PREFIX);
     27        return ("" + componentId).startsWith(ProfileDescription.PROFILE_PREFIX);
    2828    }
    2929
    3030    public static boolean isComponentId(String componentId) {
    31         return ("" + componentId)
    32                 .startsWith(ComponentDescription.COMPONENT_PREFIX);
     31        return ("" + componentId)
     32                .startsWith(ComponentDescription.COMPONENT_PREFIX);
    3333    }
    3434
    3535    public static void copyPropertiesFrom(BaseDescription from, BaseDescription to) {
    36         BeanUtils.copyProperties(from, to);
     36        BeanUtils.copyProperties(from, to);
    3737    }
    3838
    3939    public static Date getDate(String registrationDate) throws ParseException {
    40         return DateUtils.parseDate(registrationDate,
    41                 new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
    42                         .getPattern() });
     40        return DateUtils.parseDate(registrationDate,
     41                new String[]{DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
     42                    .getPattern()});
    4343    }
    4444
    4545    public static String createPublicHref(String href) {
    46         String result = href;
    47         if (href != null) {
    48             int index = href.indexOf("?");
    49             if (index != -1) { // strip off query params the rest should be the
    50                                // public href.
    51                 result = href.substring(0, index);
    52             }
    53         }
    54         return result;
     46        String result = href;
     47        if (href != null) {
     48            int index = href.indexOf("?");
     49            if (index != -1) { // strip off query params the rest should be the
     50                // public href.
     51                result = href.substring(0, index);
     52            }
     53        }
     54        return result;
    5555    }
    56 
    5756    /**
    58      * Compares two descriptions by the their value as returned by
    59      * {@link BaseDescription#getName()
     57     * Compares two descriptions by the their value as returned by      {@link BaseDescription#getName()
    6058     * }
    6159     */
    6260    public static final Comparator<? super BaseDescription> COMPARE_ON_NAME = new Comparator<BaseDescription>() {
    63         @Override
    64         public int compare(BaseDescription o1, BaseDescription o2) {
    65             int result = 0;
    66             if (o1.getName() != null && o2.getName() != null) {
    67                 result = o1.getName().compareToIgnoreCase(o2.getName());
    68             }
    69             if (o1.getId() != null && result == 0) {
    70                 result = o1.getId().compareTo(o2.getId());
    71             }
    72             return result;
    73         }
     61        @Override
     62        public int compare(BaseDescription o1, BaseDescription o2) {
     63            int result = 0;
     64            if (o1.getName() != null && o2.getName() != null) {
     65                result = o1.getName().compareToIgnoreCase(o2.getName());
     66            }
     67            if (o1.getId() != null && result == 0) {
     68                result = o1.getId().compareTo(o2.getId());
     69            }
     70            return result;
     71        }
    7472    };
    7573    /**
     
    7876     */
    7977    public static final Comparator<? super BaseDescription> COMPARE_ON_DATE = new Comparator<BaseDescription>() {
    80         /**
    81         * @returns 1 if o11 is older than o2, returns -1 if o1 is younger than
    82          *          o2
    83         */
    84         @Override
    85         public int compare(BaseDescription o1, BaseDescription o2) {
    86             return o1.getRegistrationDate().compareTo(o2.getRegistrationDate());
    87         }
     78        /**
     79        * @returns 1 if o11 is older than o2, returns -1 if o1 is younger than
     80         * o2
     81        */
     82        @Override
     83        public int compare(BaseDescription o1, BaseDescription o2) {
     84            return o1.getRegistrationDate().compareTo(o2.getRegistrationDate());
     85        }
    8886    };
    8987
    90     public static ProfileDescription toProfile(BaseDescription baseDescription) {
    91         if (baseDescription == null)
    92             return null;
    93         ProfileDescription copy = new ProfileDescription();
    94         BeanUtils.copyProperties(baseDescription, copy);
    95         return copy;
     88    public static ProfileDescription toProfile(BaseDescription baseDescription) throws ComponentRegistryException {
     89        if (baseDescription == null) {
     90            return null;
     91        }
     92        if (baseDescription.getId().startsWith(ProfileDescription.PROFILE_PREFIX)) {
     93            ProfileDescription copy = new ProfileDescription();
     94            BeanUtils.copyProperties(baseDescription, copy);
     95            return copy;
     96        } else {
     97            throw new ComponentRegistryException("The item is not a profile.");
     98        }
    9699    }
    97100
    98     public static ComponentDescription toComponent(BaseDescription baseDescription) {
    99         if (baseDescription == null)
    100             return null;
    101         ComponentDescription copy = new ComponentDescription();
    102         BeanUtils.copyProperties(baseDescription, copy);
    103         return copy;
     101    public static ComponentDescription toComponent(BaseDescription baseDescription) throws ComponentRegistryException {
     102        if (baseDescription == null) {
     103            return null;
     104        }
     105        if (baseDescription.getId().startsWith(ComponentDescription.COMPONENT_PREFIX)) {
     106            ComponentDescription copy = new ComponentDescription();
     107            BeanUtils.copyProperties(baseDescription, copy);
     108            return copy;
     109        } else {
     110            throw new ComponentRegistryException("The item is not a component");
     111        }
    104112    }
    105113
    106114    public static List<ProfileDescription> toProfiles(
    107             List<BaseDescription> baseDescription) {
    108         if (baseDescription == null)
    109             return null;
    110         List<ProfileDescription> list = new ArrayList<ProfileDescription>();
    111         for (BaseDescription c : baseDescription)
    112             list.add(toProfile(c));
    113         return list;
     115            List<BaseDescription> baseDescription) {
     116        if (baseDescription == null) {
     117            return null;
     118        }
     119        List<ProfileDescription> list = new ArrayList<ProfileDescription>();
     120        for (BaseDescription c : baseDescription) {
     121            try {
     122                list.add(toProfile(c));
     123            } catch (ComponentRegistryException e) {
     124            }
     125        }
     126        return list;
    114127    }
    115128
    116129    public static List<ComponentDescription> toComponents(
    117             List<BaseDescription> baseDescription) {
    118         if (baseDescription == null)
    119             return null;
    120         List<ComponentDescription> list = new ArrayList<ComponentDescription>();
    121         for (BaseDescription c : baseDescription)
    122             list.add(toComponent(c));
    123         return list;
     130            List<BaseDescription> baseDescription) {
     131        if (baseDescription == null) {
     132            return null;
     133        }
     134        List<ComponentDescription> list = new ArrayList<ComponentDescription>();
     135        for (BaseDescription c : baseDescription) {
     136            try {
     137                list.add(toComponent(c));
     138            } catch (ComponentRegistryException e) {
     139            }
     140        }
     141        return list;
    124142    }
    125 
    126143}
Note: See TracChangeset for help on using the changeset viewer.