Skip to content

Commit

Permalink
Fix add listing bug of empty editor and duplicate EntityID
Browse files Browse the repository at this point in the history
  • Loading branch information
MonaiThang committed Oct 1, 2016
1 parent 16c3035 commit 473f382
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 22 deletions.
76 changes: 54 additions & 22 deletions src/com/sixppl/cmd/SellCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class SellCommand implements Command {
private boolean error;
private String error_msg;
private ListingDAO listingDao;
private EntityDAO entityDao;
private GraphDAO graphDao;
long countPublication;
long countAuthor;
long countVenue;
Expand All @@ -42,11 +44,41 @@ public SellCommand(){
error = false;
error_msg = null;
listingDao = Application.getSharedInstance().getDAOFactory().getListingDAO();
countPublication = 0;
countAuthor = 0;
countEdge = 0;
entityDao = Application.getSharedInstance().getDAOFactory().getEntityDAO();
graphDao = Application.getSharedInstance().getDAOFactory().getGraphDAO();
countEntity = 0;
insertedEntity = new ArrayList<EntityDTO>();
// countPublication = 0;
// countAuthor = 0;
// countEdge = 0;

// Get all existing nodes from entity table
try {
insertedEntity = entityDao.findAllNodes();
} catch (SQLException e2) {
e2.printStackTrace();
}

// Get max ID of each node class
try {
countPublication = entityDao.getMaxNodeID("Publication");
} catch (SQLException e) {
e.printStackTrace();
}
try {
countAuthor = entityDao.getMaxNodeID("Author");
} catch (SQLException e) {
e.printStackTrace();
}
try {
countVenue = entityDao.getMaxNodeID("Venue");
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
countEdge = entityDao.getMaxEdgeID();
} catch (SQLException e) {
e.printStackTrace();
}
}

@Override
Expand All @@ -59,17 +91,26 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
return;
}
ListingDTO pubSell = new ListingDTO();
PublicationDTO pubGraph = new PublicationDTO();
ArrayList<String> pubGraphAuthor = new ArrayList<String>();
ArrayList<String> pubGraphEditor = new ArrayList<String>();
pubSell.title = request.getParameter("title").trim();
if(request.getParameter("authors") != null){
String[] authors = request.getParameter("authors").split(",");
for(String author: authors){
pubSell.authors.add(author.trim());
if (!author.trim().equals("")) {
pubGraphAuthor.add(author.trim());
}
}
}
if(request.getParameter("editors") != null){
String[] editors = request.getParameter("editors").split(",");
for(String editor: editors){
pubSell.editors.add(editor.trim());
if (!editor.trim().equals("")) {
pubGraphEditor.add(editor.trim());
}
}
}
SessionDAO sessionDao = new SessionDAOImpl();
Expand Down Expand Up @@ -112,10 +153,13 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
}

// Add to Graph
PublicationDTO pubGraph = new PublicationDTO();
pubGraph.setTitle(pubSell.title);
pubGraph.setAuthor(pubSell.authors);
pubGraph.setEditor(pubSell.editors);
if (!pubGraphAuthor.isEmpty()){
pubGraph.setAuthor(pubGraphAuthor);
}
if (!pubGraphEditor.isEmpty()){
pubGraph.setEditor(pubGraphEditor);
}
pubGraph.setJournal(pubSell.venue);

addToGraph(pubGraph);
Expand Down Expand Up @@ -149,9 +193,7 @@ public boolean parameterChecker(HttpServletRequest request){
}

public void addToGraph(PublicationDTO p){
EntityDAO entityDao = Application.getSharedInstance().getDAOFactory().getEntityDAO();
GraphDAO graphDao = Application.getSharedInstance().getDAOFactory().getGraphDAO();
ListingDAO listingDAO = Application.getSharedInstance().getDAOFactory().getListingDAO();
ListingDAO listingDao = Application.getSharedInstance().getDAOFactory().getListingDAO();
EntityDTO ep = new EntityDTO();
EntityDTO ea = new EntityDTO();
EntityDTO ed = new EntityDTO();
Expand All @@ -160,17 +202,16 @@ public void addToGraph(PublicationDTO p){
GraphDTO pa = new GraphDTO();
GraphDTO pd = new GraphDTO();
GraphDTO pv = new GraphDTO();
long PubID = listingDAO.getMaxPubID();
long PubID = listingDao.getMaxPubID();
// Extract publication
ep = new EntityDTO(countEntity+1, "P"+ Long.toString(countPublication+1), "Node", "Publication", p.getTitle());
ep = new EntityDTO(countEntity+1, "P"+ Long.toString(PubID), "Node", "Publication", p.getTitle());
if (EntityDTO.containsEntity(insertedEntity, ep)) {
ep = EntityDTO.findEntity(insertedEntity, ep);
}
else {
try {
entityDao.insertEntity(ep,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Insert Title");
Expand All @@ -190,7 +231,6 @@ public void addToGraph(PublicationDTO p){
try {
entityDao.insertEntity(ea,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Insert Author");
Expand All @@ -203,7 +243,6 @@ public void addToGraph(PublicationDTO p){
try {
entityDao.insertEntity(ee,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Link Title to author");
Expand All @@ -212,7 +251,6 @@ public void addToGraph(PublicationDTO p){
try {
graphDao.insertGraph(pa,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
countEdge++;
Expand All @@ -231,7 +269,6 @@ public void addToGraph(PublicationDTO p){
try {
entityDao.insertEntity(ed,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
insertedEntity.add(ed);
Expand All @@ -244,15 +281,13 @@ public void addToGraph(PublicationDTO p){
try {
entityDao.insertEntity(ee,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
insertedEntity.add(ee);
pd = new GraphDTO(0, ep.getEntityID(), ee.getEntityID(), ed.getEntityID());
try {
graphDao.insertGraph(pd,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
countEdge++;
Expand All @@ -269,7 +304,6 @@ public void addToGraph(PublicationDTO p){
try {
entityDao.insertEntity(ev,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Insert venue");
Expand All @@ -282,15 +316,13 @@ public void addToGraph(PublicationDTO p){
try {
entityDao.insertEntity(ee,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
insertedEntity.add(ee);
pv = new GraphDTO(0, ep.getEntityID(), ee.getEntityID(), ev.getEntityID());
try {
graphDao.insertGraph(pv,PubID);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
countEdge++;
Expand Down
2 changes: 2 additions & 0 deletions src/com/sixppl/dao/EntityDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface EntityDAO {
ArrayList<String> findLinkedEntity(String node) throws SQLException;
ArrayList<EntityDTO> findAllNodes() throws SQLException;
ArrayList<EntityDTO> getRandomNodes(int nodeAmount) throws SQLException;
long getMaxNodeID(String Type) throws SQLException;
long getMaxEdgeID() throws SQLException;
}
53 changes: 53 additions & 0 deletions src/com/sixppl/dao/support/EntityDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,57 @@ public ArrayList<EntityDTO> getRandomNodes(int nodeAmount) throws SQLException {
}
return result;
}

@Override
public long getMaxNodeID(String Type) throws SQLException {
long result = 0;
String MaxEntityID = "";
String sql = "SELECT EntityID FROM Entity WHERE Type=? ORDER BY EntityID DESC LIMIT 0,1";
Connection connection = null;
try {
connection = Application.getSharedInstance().getDAOSupport().getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, Type);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
MaxEntityID = rs.getString("EntityID");
}
rs.close();
ps.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
if(MaxEntityID != null && !MaxEntityID.equals("")) {
result = Long.parseLong(MaxEntityID.substring(1));
}
return result;
}

@Override
public long getMaxEdgeID() throws SQLException {
long result = 0;
String MaxEntityID = "";
String sql = "SELECT EntityID FROM Entity WHERE Class='Edge' ORDER BY EntityID DESC LIMIT 0,1";
Connection connection = null;
try {
connection = Application.getSharedInstance().getDAOSupport().getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
MaxEntityID = rs.getString("EntityID");
}
rs.close();
ps.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
if(MaxEntityID != null && !MaxEntityID.equals("")) {
result = Long.parseLong(MaxEntityID.substring(1));
}
return result;
}


}

0 comments on commit 473f382

Please sign in to comment.