Skip to content

Commit

Permalink
partial fix for deleting datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
checker5965 committed Mar 2, 2019
1 parent 8ec852b commit 2ffb375
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 6 deletions.
9 changes: 9 additions & 0 deletions WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
<url-pattern>/custom-dataset</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>delete-dataset</servlet-name>
<servlet-class>in.edu.ashoka.surf.delete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete-dataset</servlet-name>
<url-pattern>/delete-dataset</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>overwrite</servlet-name>
<servlet-class>in.edu.ashoka.surf.overwrite</servlet-class>
Expand Down
19 changes: 15 additions & 4 deletions WebContent/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,30 @@
</div>
<br/>
<div class="submit-button">
<button type="submit" class="btn btn-default">Submit</button>
<button type="submit" class="btn btn-default">Run Surf</button>
</div>
<br/>
</form>
<hr/>
<p class="text-center" style="margin: auto">OR</p>
<form action="custom-dataset" method="get">
<div class="form-group">
</div>
<div class="submit-button">
<button type="submit" class="btn btn-default">Upload a new Dataset</button>
</div>
</form>
<hr>
<form action="delete-dataset" method="post">
<div class="form-group">
<label for="delDataset">Select Dataset to Delete</label>
<select id="delDataset" class="form-control selectpicker" name="delDataset"> <!-- called state for historical reasons, TOFIX -->
<% for (String key: Config.keyToDescription.keySet()) { %>
<option value="<%=key%>"><%=Config.keyToDescription.get(key)%></option>
<% } %>
</select>
</div>
<br/>
<div class="submit-button">
<button type="submit" class="btn btn-default">Delete Selected Dataset</button>
</div>
</form>
</div>
</body>
Expand Down
2 changes: 2 additions & 0 deletions src/in/edu/ashoka/surf/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ private static Properties readProperties() {
try {
InputStream is = new FileInputStream(PROPS_FILE);
props.load(is);
is.close();
} catch (Exception e) {
log.warn("Error reading Surf properties file " + PROPS_FILE + " " + e);
}
Expand Down Expand Up @@ -309,6 +310,7 @@ public static void addDatasetToConfig(String path, String desc, String name, Str
try {
InputStream is = new FileInputStream(PROPS_FILE);
props.load(is);
is.close();
} catch (Exception e) {
log.warn("Error reading Surf properties file " + PROPS_FILE + " " + e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/in/edu/ashoka/surf/customServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
}
}

parse.close();
Config.addDatasetToConfig(Config.SURF_HOME + File.separator + filename, description, filename, firstLine);
request.getRequestDispatcher("index.jsp").include(request, response);
}
Expand Down
82 changes: 82 additions & 0 deletions src/in/edu/ashoka/surf/delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package in.edu.ashoka.surf;

import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.*;
import java.io.InputStream;
import java.io.FileWriter;
import java.io.FileInputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@MultipartConfig
public class delete extends HttpServlet {
public static Log log = LogFactory.getLog(in.edu.ashoka.surf.delete.class);

public delete() {
super();
// TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("index.jsp").include(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
String toDel = request.getParameter("delDataset");
Properties props = new Properties();
String propsFile = System.getProperty("user.home") + File.separator + "surf.properties";
File f = new File(propsFile);
if (f.exists() && f.canRead()) {
try {
InputStream is = new FileInputStream(propsFile);
props.load(is);
is.close();
} catch (Exception e) {
log.warn("Error reading Surf properties file " + propsFile + " " + e);
}
String delPath = "";
boolean flag = false;
for (String key: props.stringPropertyNames()) {
if(key.substring(0, key.lastIndexOf("_")).equals(toDel))
{
if(key.endsWith("_Path"))
{
delPath = props.getProperty(key);
}
flag = true;
props.remove(key);
}
}
if(flag)
{
try
{
PrintWriter out = new PrintWriter(new FileWriter(propsFile), true);
Files.lines(f.toPath()).filter(line -> !line.contains(toDel)).forEach(out::println);
out.flush();
out.close();
} catch (Exception e) {
log.warn("Error deleting from props file\nException: "+e);
}
}
File oldfile = new File(delPath);
log.warn("Deleted? "+ oldfile.delete());
}
Config.createDatasets();
request.getRequestDispatcher("index.jsp").include(request, response);
}

public void destroy() {
}
}
1 change: 0 additions & 1 deletion src/in/edu/ashoka/surf/overwrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.*;
import javax.servlet.http.Part;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down

0 comments on commit 2ffb375

Please sign in to comment.