implementation of support for multi attachments is completed

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-networking-library@122250 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2016-01-15 16:07:44 +00:00
parent 630a26d821
commit 4607c7f815
5 changed files with 198 additions and 52 deletions

View File

@ -1,6 +1,7 @@
<ReleaseNotes>
<Changeset component="org.gcube.portal.social-networking-library.1-9-0"
date="2015-12-09">
date="2016-01-15">
<Change>Add support for multi attachments to post, Feature #1982</Change>
<Change>Add support for user statistics fast retrieval, Feature #1663</Change>
<Change>Updated the way we instanciate keyspace, now it is more efficient, Feature #1493</Change>
<Change>Updated methods for editing comments, Bug #246</Change>

View File

@ -407,6 +407,25 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
* {@inheritDoc}
*/
@Override
public boolean saveAppFeed(Feed feed, List<Attachment> attachments) {
if (attachments != null && !attachments.isEmpty())
feed.setMultiFileUpload(true);
boolean saveFeedResult = saveAppFeed(feed);
if (saveFeedResult) {
String feedKey = feed.getKey();
for (Attachment attachment : attachments) {
boolean attachSaveResult = saveAttachmentEntry(feedKey, attachment);
if (!attachSaveResult)
_log.warn("Some of the attachments failed to me saved: " + attachment.getName());
}
return true;
}
else return false;
}
/**
* {@inheritDoc}
*/
@Override
public boolean saveFeedToVRETimeline(String feedKey, String vreid) throws FeedIDNotFoundException {
String feedId = feedKey;
Feed toCheck = null;
@ -1859,6 +1878,60 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
* {@inheritDoc}
*/
@Override
public List<Attachment> getAttachmentsByFeedId(String feedId) throws FeedIDNotFoundException {
Feed toCheck = null;
try {
toCheck = readFeed(feedId);
if (toCheck == null)
throw new FeedIDNotFoundException("Could not find Feed with id " + feedId);
} catch (Exception e) {
e.printStackTrace();
return null;
}
List<Attachment> toReturn = new ArrayList<Attachment>();
PreparedIndexExpression<String, String> clause = cf_Attachments.newIndexClause().whereColumn("feedId").equals().value(feedId);
OperationResult<Rows<String, String>> result;
try {
result = conn.getKeyspace().prepareQuery(cf_Attachments)
.searchWithIndex()
.setStartKey("")
.addPreparedExpressions(Arrays.asList(clause))
.execute();
// Iterate rows and their columns
for (Row<String, String> row : result.getResult()) {
Attachment toAdd = new Attachment();
toAdd.setId(row.getKey());
for (Column<String> col : row.getColumns()) {
if (col.getName().compareTo("feedId") == 0)
_log.trace("Reading attachment if feed=" + col.getStringValue());
else if (col.getName().compareTo("uri") == 0)
toAdd.setUri(col.getStringValue());
else if (col.getName().compareTo("name") == 0)
toAdd.setName(col.getStringValue());
else if (col.getName().compareTo("description") == 0)
toAdd.setDescription(col.getStringValue());
else if (col.getName().compareTo("ThumbnailURL") == 0)
toAdd.setThumbnailURL(col.getStringValue());
else if (col.getName().compareTo("mimeType") == 0)
toAdd.setMimeType(col.getStringValue());
else {
_log.error("getAttachmentsByFeedId(): Could not assign variable to this Attachment for column name: " + col.getName());
}
}
toReturn.add(toAdd);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return toReturn;
}
/**
* {@inheritDoc}
*/
@Override
public void closeConnection() {
}
/*
@ -1875,13 +1948,13 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
// Inserting data
MutationBatch m = conn.getKeyspace().prepareMutationBatch();
//an entry in the Attachment CF
String attachmentID = UUID.randomUUID().toString();
m.withRow(cf_Attachments, attachmentID)
m.withRow(cf_Attachments, toSave.getId())
.putColumn("feedId", feedId, null)
.putColumn("uri", toSave.getUri(), null)
.putColumn("name", toSave.getName(), null)
.putColumn("description",toSave.getDescription(), null)
.putColumn("thumbnailURL",toSave.getThumbnailURL(), null);
.putColumn("thumbnailURL",toSave.getThumbnailURL(), null)
.putColumn("mimeType",toSave.getMimeType(), null);
try {
m.execute();
} catch (ConnectionException e) {
@ -2230,4 +2303,5 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
}

View File

@ -22,8 +22,12 @@ import org.junit.Test;
import com.google.gwt.thirdparty.guava.common.collect.ImmutableMap;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.ddl.ColumnDefinition;
import com.netflix.astyanax.ddl.ColumnFamilyDefinition;
import com.netflix.astyanax.model.ColumnFamily;
import com.netflix.astyanax.serializers.StringSerializer;
import com.netflix.astyanax.thrift.ddl.ThriftColumnDefinitionImpl;
import com.netflix.astyanax.thrift.ddl.ThriftColumnFamilyDefinitionImpl;
public class DatabookCassandraTest {
private static DBCassandraAstyanaxImpl store;
@ -66,24 +70,42 @@ public class DatabookCassandraTest {
// long end = System.currentTimeMillis();
// System.err.println("retrieved " + feeds.size() + " and " + numComment + " in " + (end - init) + "ms");
// }
@Test
public void testAttachments() {
Attachment a1 = new Attachment("www1", "gattino1", "description1", "http://cdn.tuttozampe.com/wp-content/uploads/2010/09/ipoglicemia-gatto.jpg");
Attachment a2 = new Attachment("www2", "name2", "description2", "http://www.gcomegatto.it/wp-content/uploads/2015/01/09gatto.jpg");
Attachment a3 = new Attachment("www3", "name3", "description3", "http://cdn.tuttozampe.com/wp-content/uploads/2010/09/ipoglicemia-gatto.jpg");
List<Attachment> toPass = new ArrayList<Attachment>();
toPass.add(a1);
toPass.add(a2);
@Test
public void testAttachments() {
Attachment a1 = new Attachment(UUID.randomUUID().toString(), "www1", "gattino1", "description1", "http://cdn.tuttozampe.com/wp-content/uploads/2010/09/ipoglicemia-gatto.jpg", "image/jpg");
Attachment a2 = new Attachment(UUID.randomUUID().toString(), "www2", "name2", "description2", "http://www.gcomegatto.it/wp-content/uploads/2015/01/09gatto.jpg","image/jpg");
Attachment a3 = new Attachment(UUID.randomUUID().toString(), "www3", "name3", "description3", "http://cdn.tuttozampe.com/wp-content/uploads/2010/09/ipoglicemia-gatto.jpg","image/jpg");
List<Attachment> toPass = new ArrayList<Attachment>();
toPass.add(a1);
toPass.add(a2);
toPass.add(a3);
Feed feed = new Feed(UUID.randomUUID().toString(), FeedType.TWEET, "massimiliano.assante", new Date(), "/gcube/devsec/devVRE",
"http://www.gcomegatto.it/wp-content/uploads/2015/01/09gatto.jpg", "http://www.gcomegatto.it/wp-content/uploads/2015/01/09gatto.jpg", "This feed has attachments (gattini) ", PrivacyLevel.SINGLE_VRE,
"Massimiliano Assante", "massimiliano.assante@isti.cnr.it", "http://cdn.tuttozampe.com/wp-content/uploads/2010/09/ipoglicemia-gatto.jpg", "Gattino", "linkDesc", "http://www.gcomegatto.it/wp-content/uploads/2015/01/09gatto.jpg", false);
feed.setMultiFileUpload(true);
assertTrue(store.saveUserFeed(feed, toPass));
}
String feedId = UUID.randomUUID().toString();
Feed feed = new Feed(feedId, FeedType.TWEET, "massimiliano.assante", new Date(), "/gcube/devsec/devVRE",
"http://www.dailybest.it/wp-content/uploads/2015/10/gattini-nele-ciotole-e1344352237289.jpg",
"http://www.dailybest.it/wp-content/uploads/2015/10/gattini-nele-ciotole-e1344352237289.jpg",
"This post has attachments (gattini) ", PrivacyLevel.SINGLE_VRE,
"Massimiliano Assante",
"massimiliano.assante@isti.cnr.it",
"http://www.dailybest.it/wp-content/uploads/2015/10/gattini-nele-ciotole-e1344352237289.jpg",
"Gattino",
"linkDesc",
"image/jpeg", false);
feed.setMultiFileUpload(true);
assertTrue(store.saveUserFeed(feed, toPass));
System.out.println("Wrote post? ");
System.out.println("Feed has the following attachments: ");
try {
for (Attachment at : store.getAttachmentsByFeedId(feedId)) {
System.out.println(at);
}
} catch (FeedIDNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// @Test
// public void testHashTag() {
@ -120,26 +142,32 @@ public class DatabookCassandraTest {
//
// }
//
// /**
// * use exclusively to add a new (Static) CF to a keyspace
// */
// @Test
// public void addAttachmentStaticColumnFamilies() {
// ColumnFamily<String, String> CF_INVITES = ColumnFamily.newColumnFamily(DBCassandraAstyanaxImpl.ATTACHMENTS, StringSerializer.get(), StringSerializer.get());
//
// try {
// new CassandraClusterConnection(false).getKeyspace().createColumnFamily(CF_INVITES, ImmutableMap.<String, Object>builder()
// .put("default_validation_class", "UTF8Type")
// .put("key_validation_class", "UTF8Type")
// .put("comparator_type", "UTF8Type")
// .build());
//
// } catch (ConnectionException e) {
// e.printStackTrace();
// }
// System.out.println("addStaticColumnFamily END");
// }
// /**
// * use exclusively to add a new (Static) CF to a keyspace with a secondary index
// */
// @Test
// public void addAttachmentStaticColumnFamilies() {
// ColumnFamily<String, String> CF_ATTACHMENTS = ColumnFamily.newColumnFamily(DBCassandraAstyanaxImpl.ATTACHMENTS, StringSerializer.get(), StringSerializer.get());
//
// try {
// String colNameToIndex = "feedId";
// new CassandraClusterConnection(false).getKeyspace().createColumnFamily(CF_ATTACHMENTS, ImmutableMap.<String, Object>builder()
// .put("column_metadata", ImmutableMap.<String, Object>builder()
// .put(colNameToIndex, ImmutableMap.<String, Object>builder()
// .put("validation_class", "UTF8Type")
// .put("index_name", "FeedIndex_"+UUID.randomUUID().toString().substring(0,5))
// .put("index_type", "KEYS")
// .build())
// .build())
// .build());
//
//
// } catch (ConnectionException e) {
// e.printStackTrace();
// }
// System.out.println("addStaticColumnFamily END");
// }
// /**

View File

@ -65,7 +65,10 @@ public interface DatabookStore {
*/
boolean saveUserFeed(Feed feed);
/**
* save a Feed instance in the store having more than one attachment
* Save a Feed instance in the store having more than one attachment
* Use this if you need to attach more than one file to the post
*
* @param attachments, a list of attachments starting from the second
* @return true if everything went fine
*/
boolean saveUserFeed(Feed feed, List<Attachment> attachments);
@ -76,7 +79,7 @@ public interface DatabookStore {
*/
boolean deleteFeed(String feedid) throws FeedIDNotFoundException, PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException;
/**
* save a Feed in the VRES TimeLine in the store
* save a post in the VRES TimeLine in the store
* @param feedKey feedKey
* @param vreid vre identifier
* @return
@ -84,10 +87,18 @@ public interface DatabookStore {
*/
boolean saveFeedToVRETimeline(String feedKey, String vreid) throws FeedIDNotFoundException;
/**
* save a Feed instance in the store
* save a Post instance in the store
* @return true if everything went fine
*/
boolean saveAppFeed(Feed feed);
boolean saveAppFeed(Feed feed);
/**
* Save a feed instance in the store
* Use this if your app needs to attach more than one file to the post
*
* @param attachments, a list of attachments starting from the second
* @return true if everything went fine
*/
boolean saveAppFeed(Feed feed, List<Attachment> attachments);
/**
* read a feed from a given id
* @throws PrivacyLevelTypeNotFoundException
@ -404,6 +415,12 @@ public interface DatabookStore {
* @throws InviteStatusNotFoundException
*/
List<Invite> getInvitedEmailsByVRE(String vreid, InviteStatus... status) throws InviteIDNotFoundException, InviteStatusNotFoundException;
/**
*
* @param feedId
* @return the list of attachments of the feed feedId, starting from the second one (first attachment is included in Feed instance already)
*/
List<Attachment> getAttachmentsByFeedId(String feedId) throws FeedIDNotFoundException;
/**
* close the connection to the underlying database
*/

View File

@ -4,28 +4,43 @@ import java.io.Serializable;
@SuppressWarnings("serial")
public class Attachment implements Serializable {
private String id;
private String uri;
private String name;
private String description;
private String thumbnailURL;
private String mimeType;
public Attachment() {
super();
}
/**
* @param id the id in the cassandra CF
* @param uri where you can download the file from
* @param name the name of the attached file
* @param description the description of the attached file
* @param thumbnailURL the URL of the image representing the attached file
*/
public Attachment(String uri, String name,
String description, String thumbnailURL) {
public Attachment(String id, String uri, String name, String description,
String thumbnailURL, String mimeType) {
super();
this.id = id;
this.uri = uri;
this.name = name;
this.description = description;
this.thumbnailURL = thumbnailURL;
}
this.mimeType = mimeType;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUri() {
return uri;
}
@ -64,11 +79,22 @@ public class Attachment implements Serializable {
this.thumbnailURL = thumbnailURL;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
@Override
public String toString() {
return "Attachment [uri=" + uri + ", name=" + name + ", description="
+ description + ", thumbnailURL=" + thumbnailURL + "]";
+ description + ", thumbnailURL=" + thumbnailURL
+ ", mimeType=" + mimeType + "]";
}
}