use of org.opennms.web.svclayer.api.RequisitionAccessService in project opennms by OpenNMS.
the class NodeLabelChangeServlet method doPost.
/**
* {@inheritDoc}
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nodeIdString = request.getParameter("node");
String labelType = request.getParameter("labeltype");
String userLabel = request.getParameter("userlabel");
if (nodeIdString == null) {
throw new MissingParameterException("node", new String[] { "node", "labeltype", "userlabel" });
}
if (labelType == null) {
throw new MissingParameterException("labeltype", new String[] { "node", "labeltype", "userlabel" });
}
if (userLabel == null) {
throw new MissingParameterException("userlabel", new String[] { "node", "labeltype", "userlabel" });
}
try {
final int nodeId = WebSecurityUtils.safeParseInt(nodeIdString);
final OnmsNode node = NetworkElementFactory.getInstance(getServletContext()).getNode(nodeId);
NodeLabel nodeLabel = BeanUtils.getBean("daoContext", "nodeLabel", NodeLabel.class);
NodeLabel oldLabel = new NodeLabelDaoImpl(node.getLabel(), node.getLabelSource());
NodeLabel newLabel = null;
if (labelType.equals("auto")) {
newLabel = nodeLabel.computeLabel(nodeId);
} else if (labelType.equals("user")) {
newLabel = new NodeLabelDaoImpl(userLabel, NodeLabelSource.USER);
} else {
throw new ServletException("Unexpected labeltype value: " + labelType);
}
final String newNodeLabel = newLabel.getLabel();
boolean managedByProvisiond = node.getForeignSource() != null && node.getForeignId() != null;
if (managedByProvisiond) {
WebApplicationContext beanFactory = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
final TransactionTemplate transactionTemplate = beanFactory.getBean(TransactionTemplate.class);
final RequisitionAccessService requisitionService = beanFactory.getBean(RequisitionAccessService.class);
transactionTemplate.execute(new TransactionCallback<RequisitionNode>() {
@Override
public RequisitionNode doInTransaction(TransactionStatus status) {
MultivaluedMap<String, String> params = new MultivaluedHashMap<String, String>();
params.putSingle("node-label", newNodeLabel);
requisitionService.updateNode(node.getForeignSource(), node.getForeignId(), params);
return requisitionService.getNode(node.getForeignSource(), node.getForeignId());
}
});
}
this.sendLabelChangeEvent(nodeId, oldLabel, newLabel);
if (managedByProvisiond) {
response.sendRedirect(Util.calculateUrlBase(request, "admin/nodelabelProvisioned.jsp?node=" + nodeIdString + "&foreignSource=" + node.getForeignSource()));
} else {
final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
final TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class);
final NodeLabel newLabelFinal = newLabel;
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
nodeLabel.assignLabel(nodeId, newLabelFinal);
} catch (SQLException e) {
LOG.error("Failed to change label on node with id: {} to: {}", nodeId, newLabelFinal, e);
throw Throwables.propagate(e);
}
}
});
response.sendRedirect(Util.calculateUrlBase(request, "element/node.jsp?node=" + nodeIdString));
}
} catch (SQLException e) {
throw new ServletException("Database exception", e);
} catch (Throwable e) {
throw new ServletException("Exception sending node label change event", e);
}
}
Aggregations