|
26 | 26 | import java.security.MessageDigest; |
27 | 27 | import java.security.NoSuchAlgorithmException; |
28 | 28 | import java.text.SimpleDateFormat; |
| 29 | +import java.util.Collection; |
29 | 30 | import java.util.Date; |
30 | 31 |
|
31 | 32 | import javax.servlet.http.Cookie; |
|
39 | 40 | import com.fasterxml.jackson.core.JsonProcessingException; |
40 | 41 |
|
41 | 42 | import de.binfalse.bflog.LOGGER; |
| 43 | +import de.unirostock.sems.cbarchive.meta.MetaDataHolder; |
| 44 | +import de.unirostock.sems.cbarchive.meta.MetaDataObject; |
| 45 | +import de.unirostock.sems.cbarchive.meta.OmexMetaDataObject; |
| 46 | +import de.unirostock.sems.cbarchive.meta.omex.OmexDescription; |
| 47 | +import de.unirostock.sems.cbarchive.meta.omex.VCard; |
42 | 48 | import de.unirostock.sems.cbarchive.web.dataholder.UserData; |
43 | 49 | import de.unirostock.sems.cbarchive.web.exception.CombineArchiveWebCriticalException; |
44 | 50 | import de.unirostock.sems.cbarchive.web.exception.CombineArchiveWebException; |
@@ -277,4 +283,86 @@ public static boolean isFilenameBlacklisted( String filename ) { |
277 | 283 |
|
278 | 284 | return false; |
279 | 285 | } |
| 286 | + |
| 287 | + /** |
| 288 | + * Adds current date as modification and adds the creator if not done yet, to every Omex description |
| 289 | + * Also creates new Omex description, if create is set to true and only if necessary |
| 290 | + * |
| 291 | + * @param entity |
| 292 | + * @param creator |
| 293 | + * @param create |
| 294 | + */ |
| 295 | + public static void addOmexMetaData(MetaDataHolder entity, VCard creator, boolean create) { |
| 296 | + |
| 297 | + int added = 0; |
| 298 | + // save some checks |
| 299 | + if( creator != null && creator.isEmpty() ) |
| 300 | + creator = null; |
| 301 | + |
| 302 | + // add modified date and own VCard to all omex descriptions for the root element |
| 303 | + for( MetaDataObject metaObject : entity.getDescriptions() ) { |
| 304 | + if( metaObject instanceof OmexMetaDataObject ) { |
| 305 | + OmexDescription meta = ((OmexMetaDataObject) metaObject).getOmexDescription(); |
| 306 | + |
| 307 | + meta.getModified().add( new Date() ); |
| 308 | + if( creator != null && !containsVCard(meta.getCreators(), creator) ) |
| 309 | + // creator is set and not in Omex right now |
| 310 | + meta.getCreators().add(creator); |
| 311 | + added++; |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + if( added == 0 && create == true ) { |
| 316 | + // meta was added to non entry -> create |
| 317 | + OmexDescription meta = new OmexDescription(); |
| 318 | + meta.getModified().add( meta.getCreated() ); |
| 319 | + if( creator != null ) |
| 320 | + meta.getCreators().add( creator ); |
| 321 | + |
| 322 | + // attach to entity |
| 323 | + entity.addDescription( new OmexMetaDataObject(meta) ); |
| 324 | + } |
| 325 | + |
| 326 | + } |
| 327 | + |
| 328 | + /** |
| 329 | + * Checks if the given VCard exists already in the Collection |
| 330 | + * |
| 331 | + * @param collection |
| 332 | + * @param vcard |
| 333 | + * @return |
| 334 | + */ |
| 335 | + public static boolean containsVCard( Collection<VCard> collection, VCard vcard ) { |
| 336 | + |
| 337 | + if( collection == null ) |
| 338 | + return vcard == null; |
| 339 | + else if( vcard == null ) |
| 340 | + return false; |
| 341 | + |
| 342 | + for( VCard current : collection ) |
| 343 | + if( areVCardEqual(current, vcard) ) |
| 344 | + return true; |
| 345 | + |
| 346 | + return false; |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * Compares 2 VCards and returns true if both are identical in means of String.equal() or if both are null |
| 351 | + * |
| 352 | + * @param vcard1 |
| 353 | + * @param vcard2 |
| 354 | + * @return |
| 355 | + */ |
| 356 | + public static boolean areVCardEqual( VCard vcard1, VCard vcard2 ) { |
| 357 | + |
| 358 | + if( vcard1 == vcard2 || (vcard1 == null && vcard2 == null) ) |
| 359 | + return true; |
| 360 | + else if( vcard1 == null || vcard2 == null ) |
| 361 | + return false; |
| 362 | + return (vcard1.getGivenName() == null ? vcard2.getGivenName() == null : vcard1.getGivenName().equals( vcard2.getGivenName() )) && |
| 363 | + (vcard1.getFamilyName() == null ? vcard2.getFamilyName() == null : vcard1.getFamilyName().equals( vcard2.getFamilyName() )) && |
| 364 | + (vcard1.getEmail() == null ? vcard2.getEmail() == null : vcard1.getEmail().equals( vcard2.getEmail() )) && |
| 365 | + (vcard1.getOrganization() == null ? vcard2.getOrganization() == null : vcard1.getOrganization().equals( vcard2.getOrganization() )); |
| 366 | + } |
| 367 | + |
280 | 368 | } |
0 commit comments