01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.http.webclient.repository.modify.remove;
07:
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10: import javax.servlet.http.HttpSession;
11:
12: import org.slf4j.Logger;
13: import org.slf4j.LoggerFactory;
14: import org.springframework.validation.BindException;
15: import org.springframework.web.bind.ServletRequestDataBinder;
16: import org.springframework.web.servlet.ModelAndView;
17: import org.springframework.web.servlet.mvc.SimpleFormController;
18:
19: import org.openrdf.http.webclient.SessionKeys;
20: import org.openrdf.http.webclient.properties.ResourcePropertyEditor;
21: import org.openrdf.http.webclient.properties.UriPropertyEditor;
22: import org.openrdf.http.webclient.properties.ValuePropertyEditor;
23: import org.openrdf.model.Resource;
24: import org.openrdf.model.URI;
25: import org.openrdf.model.Value;
26: import org.openrdf.repository.RepositoryConnection;
27: import org.openrdf.repository.RepositoryException;
28: import org.openrdf.repository.http.HTTPRepository;
29:
30: /**
31: * @author Herko ter Horst
32: */
33: public class RemoveStatementsController extends SimpleFormController {
34:
35: final Logger logger = LoggerFactory.getLogger(this .getClass());
36:
37: @Override
38: protected void initBinder(HttpServletRequest request,
39: ServletRequestDataBinder binder) {
40: HttpSession session = request.getSession();
41: HTTPRepository repo = (HTTPRepository) session
42: .getAttribute(SessionKeys.REPOSITORY_KEY);
43:
44: binder.registerCustomEditor(Resource.class,
45: new ResourcePropertyEditor(repo.getValueFactory()));
46: binder.registerCustomEditor(URI.class, new UriPropertyEditor(
47: repo.getValueFactory()));
48: binder.registerCustomEditor(Value.class,
49: new ValuePropertyEditor(repo.getValueFactory()));
50: }
51:
52: @Override
53: protected ModelAndView onSubmit(HttpServletRequest request,
54: HttpServletResponse response, Object command,
55: BindException errors) {
56: logger.info("Removing statements...");
57: String actionResult = "repository.modify.remove.statements.success";
58:
59: RemovalSpecification toRemove = (RemovalSpecification) command;
60:
61: HTTPRepository repo = (HTTPRepository) request.getSession()
62: .getAttribute(SessionKeys.REPOSITORY_KEY);
63: RepositoryConnection conn = null;
64: try {
65: conn = repo.getConnection();
66: conn.remove(toRemove.getSubject(), toRemove.getPredicate(),
67: toRemove.getObject(), toRemove.getContexts());
68: conn.commit();
69: logger.info("Remove committed.");
70: } catch (RepositoryException e) {
71: logger.warn("Unable to clear repository", e);
72: actionResult = "repository.modify.remove.statements.failure";
73: } finally {
74: if (conn != null) {
75: try {
76: conn.close();
77: } catch (RepositoryException e) {
78: e.printStackTrace();
79: }
80: }
81: }
82:
83: return new ModelAndView(getSuccessView(), "actionResult",
84: actionResult);
85: }
86: }
|