diff --git a/src/main/java/org/wikitolearn/dao/GenericDAO.java b/src/main/java/org/wikitolearn/dao/GenericDAO.java index f24f054..cd4a946 100644 --- a/src/main/java/org/wikitolearn/dao/GenericDAO.java +++ b/src/main/java/org/wikitolearn/dao/GenericDAO.java @@ -1,33 +1,33 @@ /** * */ package org.wikitolearn.dao; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.wikitolearn.utils.DbConnection; /** * @author aletundo * */ public abstract class GenericDAO { protected final Logger LOG = LoggerFactory.getLogger(getClass()); @Autowired protected DbConnection connection; - @Value("#{'${mediawiki.langs}'.split(',')}") + @Value("#{'${mediawiki.langs}'.trim().split(',')}") protected List langs; /** * This method is used to create the class on the DB. * It creates an unique index on the id to avoid duplicated. * @return void */ public abstract void createDatabaseClass(); } \ No newline at end of file diff --git a/src/main/java/org/wikitolearn/dao/RevisionDAO.java b/src/main/java/org/wikitolearn/dao/RevisionDAO.java index cc52f5c..c899c1b 100644 --- a/src/main/java/org/wikitolearn/dao/RevisionDAO.java +++ b/src/main/java/org/wikitolearn/dao/RevisionDAO.java @@ -1,161 +1,161 @@ package org.wikitolearn.dao; import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OType; import com.orientechnologies.orient.core.sql.OCommandSQL; import com.orientechnologies.orient.core.storage.ORecordDuplicatedException; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; import com.tinkerpop.blueprints.impls.orient.OrientVertex; import com.tinkerpop.blueprints.impls.orient.OrientVertexType; import org.springframework.stereotype.Repository; import org.wikitolearn.models.Revision; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; /** * * @author aletundo, valsdav * */ @Repository public class RevisionDAO extends GenericDAO { /** * This method is used to create the Revision class on the database. Moreover it creates * a unique index on the revid property to avoid duplication. */ @Override public void createDatabaseClass() { LOG.info("Creating database Revision class..."); OrientGraphNoTx graph = connection.getGraphNT(); try { // Vertex type for the revision OrientVertexType vertex = graph.createVertexType("Revision", 1); vertex.createProperty("revid", OType.INTEGER).setMandatory(true); vertex.createProperty("lang", OType.STRING).setMandatory(true); vertex.createIndex("revid", OClass.INDEX_TYPE.UNIQUE, "revid", "lang"); // Add a cluster for each language for (String lang : langs) { graph.command(new OCommandSQL("ALTER CLASS Revision ADDCLUSTER Revisions_" + lang)).execute(); } // Edge type for the created edge from User to Revision graph.createEdgeType("Author"); // Edge type to connect revision to parent revision graph.createEdgeType("ParentRevision"); // Edge type to connect last revision to page vertex graph.createEdgeType("LastRevision"); // Edge type to connect the first revision of a page graph.createEdgeType("FirstRevision"); } catch (Exception e) { LOG.error("Something went wrong during class creation. {}.", e.getMessage()); } finally { graph.shutdown(); } } /** * This method will insert the revisions of one page, creating the link * ParentRevision between them and the link FirstRevision and LastRevision * with the Page vertex. Moreover it connects the Users to the revisions * they have created. This method must be used only for the first INIT * import, NOT for incremental insertion. * * @param pageId * @param revs * @return */ public Boolean insertRevisions(int pageId, List revs, String lang) { OrientGraphNoTx graph = connection.getGraphNT(); LOG.info("Starting to insert revisions..."); HashMap revsNodes = new HashMap(); Vertex firstRev = null; Vertex lastRev = null; try { for (Revision rev : revs) { Map props = new HashMap<>(); props.put("revid", rev.getRevid()); props.put("lang", lang); props.put("length", rev.getLength()); props.put("changeCoefficient", rev.getChangeCoefficient()); props.put("currentMeanVote", rev.getCurrentMeanVote()); props.put("currentVotesReliability", rev.getCurrentVotesReliability()); props.put("currentNormalizedVotesReliability", rev.getCurrentNormalisesVotesReliability()); props.put("totalMeanVote", rev.getTotalMeanVote()); props.put("totalVotesReliability", rev.getTotalVotesReliability()); props.put("totalNormalizedVotesReliability", rev.getTotalNormalisesVotesReliability()); props.put("validated", rev.isValidated()); Vertex revNode = graph.addVertex("class:Revision,cluster:Revisions_" + lang, props); // LOG.info("Revision inserted {}.", revNode.toString()); revsNodes.put(Integer.toString(rev.getRevid()), revNode); if (rev.getParentid() == 0) { firstRev = revNode; } if (lastRev == null || rev.getRevid() > (int) lastRev.getProperty("revid")) { lastRev = revNode; } // Connecting the creator of the revisions Vertex userCreator = null; try { userCreator = graph.getVertices("User.userid", rev.getUserid()).iterator().next(); } catch (NoSuchElementException e) { // if the user is not found we link it to the Anonymous // user. userCreator = graph.getVertices("User.userid", "0").iterator().next(); } graph.addEdge("class:Author", userCreator, revNode, "Author"); } // Now we have to create the the links between revisions for (Revision r : revs) { if (r.getParentid() != 0) { graph.addEdge("class:ParentRevision", revsNodes.get(Integer.toString(r.getRevid())), revsNodes.get(Integer.toString(r.getParentid())), "ParentRevision"); } } // Now let's create the LastRevision and FirstRevision edges Vertex page = graph.getVertices("Page.pageid", pageId).iterator().next(); graph.addEdge("class:LastRevision", page, lastRev, "LastRevision"); graph.addEdge("class:FirstRevision", page, firstRev, "FirstRevision"); LOG.info("Revisions of page {} insertion committed", pageId); return true; } catch (ORecordDuplicatedException or) { - LOG.error("Some of the pages are duplicates. {}", or.getMessage()); + LOG.error("Some of the revisions are duplicates. {}", or.getMessage()); } catch (Exception e) { - LOG.error("Something went wrong during user insertion. {}", e.getMessage()); + LOG.error("Something went wrong during revisions insertion. {}", e.getMessage()); } finally { graph.shutdown(); } return false; } /** * This methods returns an Iterable over all the Revisions belonging to a * certain cluster, so coming from the same language domain. * * @param lang * String The language of the cluster * @return result Iterable with all the revisions of the * cluster */ public Iterable getRevisionsIteratorFromCluster(OrientGraph graph, String lang) { Iterable result = null; try { result = (Iterable) graph.command(new OCommandSQL("SELECT * FROM cluster:Revisions_" + lang)) .execute(); } catch (Exception e) { LOG.error("Something went wrong during quering for revisions. {}", e.getMessage()); } return result; } }