Search in sources :

Example 1 with NodeLabelDaoImpl

use of org.opennms.netmgt.dao.hibernate.NodeLabelDaoImpl 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);
    }
}
Also used : RequisitionNode(org.opennms.netmgt.provision.persist.requisition.RequisitionNode) OnmsNode(org.opennms.netmgt.model.OnmsNode) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ServletException(javax.servlet.ServletException) NodeLabel(org.opennms.netmgt.dao.api.NodeLabel) RequisitionAccessService(org.opennms.web.svclayer.api.RequisitionAccessService) NodeLabelDaoImpl(org.opennms.netmgt.dao.hibernate.NodeLabelDaoImpl) MissingParameterException(org.opennms.web.servlet.MissingParameterException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

SQLException (java.sql.SQLException)1 ServletException (javax.servlet.ServletException)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 NodeLabel (org.opennms.netmgt.dao.api.NodeLabel)1 NodeLabelDaoImpl (org.opennms.netmgt.dao.hibernate.NodeLabelDaoImpl)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 RequisitionNode (org.opennms.netmgt.provision.persist.requisition.RequisitionNode)1 MissingParameterException (org.opennms.web.servlet.MissingParameterException)1 RequisitionAccessService (org.opennms.web.svclayer.api.RequisitionAccessService)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1