Skip to content

Commit acaa588

Browse files
committed
added some handy Tools methods for handling VCards and also impl support
for adding own VCard to imported archives [see #110]
1 parent 2be0297 commit acaa588

4 files changed

Lines changed: 99 additions & 13 deletions

File tree

src/main/java/de/unirostock/sems/cbarchive/web/Tools.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.security.MessageDigest;
2727
import java.security.NoSuchAlgorithmException;
2828
import java.text.SimpleDateFormat;
29+
import java.util.Collection;
2930
import java.util.Date;
3031

3132
import javax.servlet.http.Cookie;
@@ -39,6 +40,11 @@
3940
import com.fasterxml.jackson.core.JsonProcessingException;
4041

4142
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;
4248
import de.unirostock.sems.cbarchive.web.dataholder.UserData;
4349
import de.unirostock.sems.cbarchive.web.exception.CombineArchiveWebCriticalException;
4450
import de.unirostock.sems.cbarchive.web.exception.CombineArchiveWebException;
@@ -277,4 +283,86 @@ public static boolean isFilenameBlacklisted( String filename ) {
277283

278284
return false;
279285
}
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+
280368
}

src/main/java/de/unirostock/sems/cbarchive/web/UserManager.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import de.unirostock.sems.cbarchive.ArchiveEntry;
4242
import de.unirostock.sems.cbarchive.CombineArchive;
4343
import de.unirostock.sems.cbarchive.CombineArchiveException;
44-
import de.unirostock.sems.cbarchive.meta.MetaDataObject;
4544
import de.unirostock.sems.cbarchive.meta.OmexMetaDataObject;
4645
import de.unirostock.sems.cbarchive.meta.omex.OmexDescription;
4746
import de.unirostock.sems.cbarchive.meta.omex.VCard;
@@ -253,6 +252,12 @@ private String createArchive( String name, File existingArchive, VCard creator )
253252
// an archive already exists
254253
// check if combineArchive is valid
255254
CombineArchive combineArchive = new CombineArchive( existingArchive );
255+
256+
// adds the creator and the current date, if provided and archive is valid
257+
if( combineArchive != null && creator != null && !creator.isEmpty() ) {
258+
// add modified date and own VCard to all omex descriptions for the root element
259+
Tools.addOmexMetaData(combineArchive, creator, true);
260+
}
256261
combineArchive.close();
257262

258263
// copy files
@@ -407,10 +412,8 @@ else if( strategy == ReplaceStrategy.RENAME ) {
407412
}
408413

409414
// add modified date to all omex descriptions for the root element
410-
for( MetaDataObject metaObject : combineArchive.getDescriptions() ) {
411-
if( metaObject instanceof OmexMetaDataObject )
412-
((OmexMetaDataObject) metaObject).getOmexDescription().getModified().add( new Date() );
413-
}
415+
Tools.addOmexMetaData(combineArchive, null, false);
416+
414417
}
415418

416419
// format changed

src/main/java/de/unirostock/sems/cbarchive/web/dataholder/UserData.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.fasterxml.jackson.databind.ObjectMapper;
3333

3434
import de.unirostock.sems.cbarchive.meta.omex.VCard;
35+
import de.unirostock.sems.cbarchive.web.Tools;
3536

3637
@XmlAccessorType(XmlAccessType.FIELD)
3738
public class UserData {
@@ -102,14 +103,8 @@ public VCard getVCard() {
102103
*/
103104
@JsonIgnore
104105
public boolean isContained( List<VCard> list ) {
105-
106-
for( VCard user : list ) {
107-
if( user.getFamilyName().equals(familyName) && user.getGivenName().equals(givenName)
108-
&& user.getEmail().equals(email) && user.getOrganization().equals(organization) )
109-
return true;
110-
}
111106

112-
return false;
107+
return Tools.containsVCard(list, getVCard());
113108
}
114109

115110
@JsonIgnore

src/main/java/de/unirostock/sems/cbarchive/web/rest/RestApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,6 @@ public Response updateArchiveEntry( @PathParam("archive_id") String archiveId, @
825825
}
826826

827827
try {
828-
// TODO parse option filed
829828
user.updateArchiveEntry(archiveId, newEntry);
830829

831830
Archive archive = user.getArchive(archiveId);
@@ -844,6 +843,7 @@ public Response updateArchiveEntry( @PathParam("archive_id") String archiveId, @
844843
}
845844
}
846845

846+
@SuppressWarnings("resource")
847847
@POST
848848
@Path( "/archives/{archive_id}/entries" )
849849
@Produces( MediaType.APPLICATION_JSON )

0 commit comments

Comments
 (0)