Skip to content

Commit

Permalink
Graph remove implemented and tested
Browse files Browse the repository at this point in the history
Fix search Bug
  • Loading branch information
tanakrit127 committed Oct 2, 2016
1 parent 1647334 commit 9c0db46
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/com/sixppl/cmd/AdminRemovePubCommand.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package com.sixppl.cmd;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sixppl.dao.AdminPubDAO;
import com.sixppl.dao.EntityDAO;
import com.sixppl.dao.GraphDAO;
import com.sixppl.main.Application;

public class AdminRemovePubCommand implements Command{
private AdminPubDAO adminPubDao;
private GraphDAO graphDao;
private EntityDAO entityDao;

public AdminRemovePubCommand() {
adminPubDao = Application.getSharedInstance().getDAOFactory().getAdminPubDAO();
graphDao = Application.getSharedInstance().getDAOFactory().getGraphDAO();
entityDao = Application.getSharedInstance().getDAOFactory().getEntityDAO();
}

@Override
Expand All @@ -26,8 +34,22 @@ public void execute(HttpServletRequest request, HttpServletResponse response) th
request.setAttribute("error", true);
return;
}

//Check graph
ArrayList<String> duplicatedNodes = graphDao.findDuplicatedNodeTo(pubID);
for(int i = 0; i < duplicatedNodes.size(); i+=2){
try {
entityDao.updatePubIDByEntityID(Integer.valueOf(duplicatedNodes.get(i+1)), duplicatedNodes.get(i));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Remove from Listing
success = adminPubDao.removePub(pubID);
request.setAttribute("error", !success);
}

}
6 changes: 5 additions & 1 deletion src/com/sixppl/cmd/SearchCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ else if(type.contains("venue")){
if(!request.getParameter("year-to").toLowerCase().contains("end of humanity")){
pubKey.toYear = Integer.valueOf(request.getParameter("year-to"));
}
pubKey.venue = request.getParameter("venue").trim();
try{
pubKey.venue = request.getParameter("venue").trim();
}catch(NullPointerException e){

}

ArrayList<ListingDTO> results = listingDao.Search(pubKey, request.getSession().getId());
setResultsAttribute(request,results);
Expand Down
1 change: 1 addition & 0 deletions src/com/sixppl/dao/EntityDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ public interface EntityDAO {
ArrayList<EntityDTO> getRandomNodes(int nodeAmount) throws SQLException;
long getMaxNodeID(String Type) throws SQLException;
long getMaxEdgeID() throws SQLException;
void updatePubIDByEntityID(int pubID, String entityID) throws SQLException;
}
1 change: 1 addition & 0 deletions src/com/sixppl/dao/GraphDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface GraphDAO {
void deleteGraph(long PubID) throws SQLException;
ArrayList<GraphOutputDTO> findGraphOutput(String node) throws SQLException;
ArrayList<GraphOutputDTO> findAllGraphOutput() throws SQLException;
public ArrayList<String> findDuplicatedNodeTo(int pubID);
}
17 changes: 17 additions & 0 deletions src/com/sixppl/dao/support/EntityDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ public void updateEntity(EntityDTO entity, long PubID) throws SQLException {
System.out.println(e.getMessage());
}
}

@Override
public void updatePubIDByEntityID(int pubID, String entityID) throws SQLException {
String sql = "UPDATE Entity SET PubID = ? WHERE EntityID=?";
Connection connection = null;
try {
connection = Application.getSharedInstance().getDAOSupport().getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setLong(1, pubID);
ps.setString(2, entityID);
ps.executeUpdate();
ps.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}

@Override
public void deleteEntity(long PubID) throws SQLException {
Expand Down
40 changes: 40 additions & 0 deletions src/com/sixppl/dao/support/GraphDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,44 @@ public ArrayList<GraphOutputDTO> findAllGraphOutput() throws SQLException {
}
return result;
}
public ArrayList<String> findDuplicatedNodeTo(int pubID){
ArrayList<String> deletedNodes = new ArrayList<String>();
ArrayList<String> duplicatedNode = new ArrayList<String>();
String sql = "SELECT NodeTo FROM Graph WHERE PubID = ?";
Connection connection = null;
try{
connection = Application.getSharedInstance().getDAOSupport().getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
ps.setLong(1, pubID);
ResultSet rs = ps.executeQuery();
while(rs.next()){
String deletedNode = rs.getString("NodeTo");
deletedNodes.add(deletedNode);
}
rs.close();
ps.close();

//Check for duplicated
for(String deletedNode : deletedNodes){
sql = "SELECT PubID, NodeTo FROM Graph WHERE PubID != ? AND NodeTo = ?";
ps = connection.prepareStatement(sql);
ps.setLong(1, pubID);
ps.setString(2, deletedNode);
rs = ps.executeQuery();
while(rs.next()){
if(rs.getString("NodeTo").contains(deletedNode)){
duplicatedNode.add(deletedNode);
long changedPubID = rs.getLong("PubID");
duplicatedNode.add(String.valueOf(changedPubID));
}
}
rs.close();
ps.close();
}
}catch(SQLException e){
System.out.println(e.getMessage());
}

return duplicatedNode;
}
}

0 comments on commit 9c0db46

Please sign in to comment.