Changeset 5549 for ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentUtils.java
- Timestamp:
- 08/11/14 16:07:55 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentUtils.java
r4098 r5549 1 1 package clarin.cmdi.componentregistry.impl; 2 2 3 import clarin.cmdi.componentregistry.ComponentRegistryException; 3 4 import java.text.ParseException; 4 5 import java.util.ArrayList; … … 11 12 import org.springframework.beans.BeanUtils; 12 13 13 import clarin.cmdi.componentregistry.DatesHelper;14 14 import clarin.cmdi.componentregistry.model.BaseDescription; 15 15 import clarin.cmdi.componentregistry.model.ComponentDescription; … … 18 18 /** 19 19 * Utilities for working with {@link BaseDescription}s 20 * 20 * 21 21 * @author george.georgovassilis@mpi.nl 22 * 22 * 23 23 */ 24 24 public class ComponentUtils { 25 25 26 26 public static boolean isProfileId(String componentId) { 27 27 return ("" + componentId).startsWith(ProfileDescription.PROFILE_PREFIX); 28 28 } 29 29 30 30 public static boolean isComponentId(String componentId) { 31 32 31 return ("" + componentId) 32 .startsWith(ComponentDescription.COMPONENT_PREFIX); 33 33 } 34 34 35 35 public static void copyPropertiesFrom(BaseDescription from, BaseDescription to) { 36 36 BeanUtils.copyProperties(from, to); 37 37 } 38 38 39 39 public static Date getDate(String registrationDate) throws ParseException { 40 41 new String[] {DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT42 .getPattern()});40 return DateUtils.parseDate(registrationDate, 41 new String[]{DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT 42 .getPattern()}); 43 43 } 44 44 45 45 public static String createPublicHref(String href) { 46 47 48 49 50 51 52 53 54 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; 55 55 } 56 57 56 /** 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() 60 58 * } 61 59 */ 62 60 public static final Comparator<? super BaseDescription> COMPARE_ON_NAME = new Comparator<BaseDescription>() { 63 64 65 66 67 68 69 70 71 72 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 } 74 72 }; 75 73 /** … … 78 76 */ 79 77 public static final Comparator<? super BaseDescription> COMPARE_ON_DATE = new Comparator<BaseDescription>() { 80 81 82 *o283 84 85 86 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 } 88 86 }; 89 87 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 } 96 99 } 97 100 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 } 104 112 } 105 113 106 114 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; 114 127 } 115 128 116 129 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; 124 142 } 125 126 143 }
Note: See TracChangeset
for help on using the changeset viewer.