Example usage for org.hibernate Session update

List of usage examples for org.hibernate Session update

Introduction

In this page you can find the example usage for org.hibernate Session update.

Prototype

void update(Object object);

Source Link

Document

Update the persistent instance with the identifier of the given detached instance.

Usage

From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java

License:Open Source License

/**
 * Checks if the state of a given virtual machine, is actually the last valid state in the Data
 * Base If it is the same, the state of the virtual machine will be updated to
 * State.IN_PROGRESS, and a boolean will be returned to true, to indicate that the virtual
 * machine can be manipulated Otherwise, the current state will be returned, and the boolean
 * will be set to false, indicating that the virtual machine can not be manipulated
 * /*from  ww  w  .  j a v  a 2 s  .  c om*/
 * @param virtualMachine The virtual machine that will be checked
 * @return A DataResult object, containing a boolean that indicates if the virtual machine can
 *         be manipulated and, in any case, it will contain the last valid state of the virtual
 *         machine
 * @throws Exception An Exception is thrown if there was a problem connecting to the Data base
 */
private DataResult<State> checkVirtualMachineState(final VirtualMachine virtualMachine) throws Exception {
    Session session = null;
    Transaction transaction = null;

    DataResult<State> currentStateAndAllow = new DataResult<State>();

    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        // Getting the last state of the virtual machine
        VirtualmachineHB virtualMachineHB = (VirtualmachineHB) session.get(VirtualmachineHB.class,
                virtualMachine.getId());

        if (virtualMachine.getState().toEnum() == virtualMachineHB.getState()
                && virtualMachineHB.getState() != StateEnum.LOCKED) {
            // The given virtual machine is up to date, and is not in
            // progress.
            // We set it now to IN_PROGRESS, and return that it is allowed
            // to manipulate it
            virtualMachineHB.setState(StateEnum.LOCKED);

            session.update(virtualMachineHB);

            // Generating the result
            currentStateAndAllow.setSuccess(true);
            currentStateAndAllow.setData(new State(StateEnum.LOCKED));
        } else {
            // The given virtual machine is not up to date, or the virtual
            // machine
            // is already in the state State.IN_PROGRESS. Manipulating it is
            // not allowed

            // Generating the result
            currentStateAndAllow.setSuccess(false);
            currentStateAndAllow.setData(new State(virtualMachineHB.getState()));
        }

        transaction.commit();
    }

    catch (Exception e) {
        if (transaction != null && transaction.isActive()) {
            transaction.rollback();
        }
        throw e;
    }

    return currentStateAndAllow;
}

From source file:com.abiquo.abiserver.commands.InfrastructureCommand.java

License:Mozilla Public License

/**
 * Edits dataCenter's information in the data base
 * /*w  w  w  .  jav a2 s  .  c  o m*/
 * @param dataCenter
 * @return
 */
protected BasicResult editDataCenter(UserSession userSession, DataCenter dataCenter) {
    BasicResult basicResult;
    basicResult = new BasicResult();
    basicResult.setSuccess(true);

    Session session = null;
    Transaction transaction = null;
    try {
        UserHB userHB = SessionUtil.findUserHBByName(userSession.getUser());

        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        DatacenterHB datacenterPojo = (DatacenterHB) session.get(DatacenterHB.class, dataCenter.getId());
        datacenterPojo.setName(dataCenter.getName());
        datacenterPojo.setSituation(dataCenter.getSituation());
        datacenterPojo.setLastModificationDate(new Date());

        datacenterPojo.setUserHBByIdUserLastModification(userHB);

        session.update(datacenterPojo);

        transaction.commit();
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(InfrastructureCommand.resourceManager, basicResult, "editDataCenter", e);
    }
    return basicResult;
}

From source file:com.abiquo.abiserver.commands.InfrastructureCommand.java

License:Mozilla Public License

/**
 * Edits rack's information in the data base
 * /*w  w  w . j av a2 s .c o m*/
 * @param rack
 * @return
 */
protected BasicResult editRack(UserSession userSession, Rack rack) {

    BasicResult basicResult;
    basicResult = new BasicResult();
    basicResult.setSuccess(true);

    Session session = null;
    Transaction transaction = null;

    try {
        UserHB userHB = SessionUtil.findUserHBByName(userSession.getUser());

        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        RackHB rackPojo = (RackHB) session
                .get(com.abiquo.abiserver.business.hibernate.pojohb.infrastructure.RackHB.class, rack.getId());
        rackPojo.setName(rack.getName());
        rackPojo.setShortDescription(rack.getShortDescription());
        rackPojo.setLargeDescription(rack.getLargeDescription());
        rackPojo.setLastModificationDate(new Date());

        rackPojo.setUserHBByIdUserLastModification(userHB);

        session.update(rackPojo);

        transaction.commit();
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(InfrastructureCommand.resourceManager, basicResult, "editRack", e);

    }
    return basicResult;
}

From source file:com.abiquo.abiserver.commands.InfrastructureCommand.java

License:Mozilla Public License

/**
 * Edits physical machine's information in the data base TODO: Possibly we need to connect
 * AbiCloud WS too, for example, when we change information related to Network Module
 * /*w w w  .j a  va2 s. co m*/
 * @param physicalMachine
 * @return
 */
protected BasicResult editPhysicalMachine(UserSession userSession, PhysicalMachine physicalMachine) {

    BasicResult basicResult;
    basicResult = new BasicResult();
    basicResult.setSuccess(true);

    Session session = null;
    Transaction transaction = null;
    try {
        UserHB userHB = SessionUtil.findUserHBByName(userSession.getUser());

        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        // The new rack for the Physical Machine
        PhysicalmachineHB physicalMachineHb = (PhysicalmachineHB) session.get(PhysicalmachineHB.class,
                physicalMachine.getId());
        RackHB rackHB = (RackHB) session.get(RackHB.class, ((Rack) physicalMachine.getAssignedTo()).getId());
        physicalMachineHb.setRack(rackHB);

        // The new SO for the Physical Machine
        SoHB so = new SoHB();
        so.setDescription(physicalMachine.getHostSO().getDescription());
        so.setIdSo(physicalMachine.getHostSO().getId());
        physicalMachineHb.setSo(so);

        // Updating the other attributes
        physicalMachineHb.setName(physicalMachine.getName());
        physicalMachineHb.setDescription(physicalMachine.getDescription());
        physicalMachineHb.setCpu(physicalMachine.getCpu());
        physicalMachineHb.setRam(physicalMachine.getRam());
        physicalMachineHb.setHd(physicalMachine.getHd());

        // User and Date modification
        physicalMachineHb.setUserHBByIdUserLastModification(userHB);
        physicalMachineHb.setLastModificationDate(new Date());

        session.update(physicalMachineHb);

        // Updating the Network Modules
        // First, we have to delete the old network modules
        for (NetworkmoduleHB networkmoduleHB : physicalMachineHb.getNetworkmodules()) {
            session.delete(networkmoduleHB);
        }

        // Now, we add the new network modules and its DNS
        NetworkmoduleHB networkmoduleHB;
        int idNetworkModule = 1; // We need to generate one of the fields of the NetworkModule
                                 // composite Primary Key
        for (NetworkModule networkModule : physicalMachine.getNetworkModuleList()) {
            networkmoduleHB = (NetworkmoduleHB) networkModule.toPojoHB();

            networkmoduleHB.getId().setIdNetworkModule(idNetworkModule);
            networkmoduleHB.getId().setIdPhysicalMachine(physicalMachineHb.getIdPhysicalMachine());
            networkmoduleHB.setUserHBByIdUserCreation(userHB);
            networkmoduleHB.setCreationDate(new Date());
            session.save(networkmoduleHB);

            // Saving the DNS of this NetworkModule
            DnsHB dnsHB;
            int idDNS = 1; // We need to generate one of the fields of the DNS composite Primary
                           // Key
            for (DNS dns : networkModule.getDns()) {
                dnsHB = (DnsHB) dns.toPojoHB();

                dnsHB.getId().setIdDns(idDNS);
                dnsHB.getId().setIdNetworkModule(networkmoduleHB.getId().getIdNetworkModule());
                dnsHB.getId().setIdPhysicalMachine(physicalMachineHb.getIdPhysicalMachine());
                dnsHB.setUserHBByIdUserCreation(userHB);
                dnsHB.setCreationDate(new Date());
                session.save(dnsHB);
                idDNS++;
            }

            idNetworkModule++;
        }

        transaction.commit();

    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(resourceManager, basicResult, "editPhysicalMachine", e);

    }

    return basicResult;
}

From source file:com.abiquo.abiserver.commands.InfrastructureCommand.java

License:Mozilla Public License

/**
 * Edits an existing in Hypervisor in DataBase, with the information contained in parameter
 * hypervisor/*from  w  ww.java 2  s  .  c  o m*/
 * 
 * @param userSession UserSession object with the user's session who called this method
 * @param hypervisor The hypervisor that will be edited, with the new information
 * @return A BasicResult object with the result of the edition
 */
protected BasicResult editHypervisor(UserSession userSession, HyperVisor hypervisor) {

    BasicResult basicResult;
    basicResult = new BasicResult();

    Session session = null;
    Transaction transaction = null;

    try {
        UserHB userHB = SessionUtil.findUserHBByName(userSession.getUser());

        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        HypervisorHB hypervisorHB = (HypervisorHB) session.get(HypervisorHB.class, hypervisor.getId());
        PhysicalmachineHB physicalMachineHB = (PhysicalmachineHB) session.get(PhysicalmachineHB.class,
                hypervisor.getAssignedTo().getId());

        // Updating the Hypervisor
        hypervisorHB.setShortDescription(hypervisor.getShortDescription());
        hypervisorHB.setType((HypervisorTypeHB) hypervisor.getType().toPojoHB());
        hypervisorHB.setIp(hypervisor.getIp());
        hypervisorHB.setPort(hypervisor.getPort());
        hypervisorHB.setPhysicalMachine(physicalMachineHB);
        hypervisorHB.setUserHBByIdUserLastModification(userHB);
        hypervisorHB.setLastModificationDate(new Date());

        session.update(hypervisorHB);

        transaction.commit();
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(resourceManager, basicResult, "editHypervisor", e);
    }

    basicResult.setMessage(InfrastructureCommand.resourceManager.getMessage("editHypervisor.success"));
    basicResult.setSuccess(true);

    return basicResult;
}

From source file:com.abiquo.abiserver.commands.InfrastructureCommand.java

License:Mozilla Public License

/**
 * Edits virtual machine's information in the data base
 * /*  www.ja v a 2s.co  m*/
 * @param virtualMachine
 * @return
 */
protected BasicResult editVirtualMachine(UserSession userSession, VirtualMachine virtualMachine) {
    BasicResult basicResult = new BasicResult();
    InfrastructureWS infrWS = null;
    Session session = null;
    Transaction transaction = null;
    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        VirtualmachineHB virtualMachineHB = (VirtualmachineHB) session.get(VirtualmachineHB.class,
                virtualMachine.getId());

        // The new Hypervisor for this VirtualMachine
        HypervisorHB newHyperVisorHB = (HypervisorHB) session.get(HypervisorHB.class,
                virtualMachine.getAssignedTo().getId());
        virtualMachineHB.setHypervisor(newHyperVisorHB);

        // TODO Anunciar al WS del cambio de Hypervisor de la Maquina Virtual
        // TODO Tener en cuenta cuando la Maquina Virtual esta encendida, etc.
        infrWS = new InfrastructureWS();
        basicResult = infrWS.editVirtualMachine(virtualMachine);

        if (basicResult.getSuccess()) {
            // Updating the other attributes
            virtualMachineHB.setName(virtualMachine.getName());
            virtualMachineHB.setDescription(virtualMachine.getDescription());
            virtualMachineHB.setCpu(virtualMachine.getCpu());
            virtualMachineHB.setRam(virtualMachine.getRam());
            virtualMachineHB.setHd(virtualMachine.getHd());
            virtualMachineHB.setHighDisponibility(virtualMachine.getHighDisponibility() ? 1 : 0);
            session.update(virtualMachineHB);
            transaction.commit();
        }

    } catch (Exception e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(resourceManager, basicResult, "editVirtualMachine", e);
    }

    basicResult.setSuccess(true);
    basicResult.setMessage(InfrastructureCommand.resourceManager.getMessage("editVirtualMachine.success"));

    return basicResult;
}

From source file:com.abiquo.abiserver.commands.InfrastructureCommand.java

License:Mozilla Public License

/**
 * Private helper to update the state in the Database
 * /* ww w.  ja  va2s . com*/
 * @param virtualMachine
 * @param newState the new state to update
 * @return a basic Result with the operation result
 */
private BasicResult updateStateInDB(VirtualMachine virtualMachine, State newState) {
    BasicResult basicResult;
    basicResult = new BasicResult();
    basicResult.setSuccess(true);

    Session session = null;
    Transaction transaction = null;
    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        VirtualmachineHB virtualMachinePojo = (VirtualmachineHB) session.get(VirtualmachineHB.class,
                virtualMachine.getId());
        StateHB newStatePojo = new StateHB();
        newStatePojo.setIdState(newState.getId());
        virtualMachinePojo.setState(newStatePojo);
        session.update(virtualMachinePojo);

        transaction.commit();
    } catch (HibernateException e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(resourceManager, basicResult, "updateStateInDB", e);
    }
    return basicResult;
}

From source file:com.abiquo.abiserver.commands.InfrastructureCommand.java

License:Mozilla Public License

/**
 * Checks if the state of a given virtual machine, is actually the last valid state in the Data
 * Base If it is the same, the state of the virtual machine will be updated to
 * State.IN_PROGRESS, and a boolean will be returned to true, to indicate that the virtual
 * machine can be manipulated Otherwise, the current state will be returned, and the boolean
 * will be set to false, indicating that the virtual machine can not be manipulated
 * /*from w w  w .  j av  a2s.  co  m*/
 * @param virtualMachine The virtual machine that will be checked
 * @return A DataResult object, containing a boolean that indicates if the virtual machine can
 *         be manipulated and, in any case, it will contain the last valid state of the virtual
 *         machine
 * @throws Exception An Exception is thrown if there was a problem connecting to the Data base
 */
private DataResult<State> checkVirtualMachineState(VirtualMachine virtualMachine) throws Exception {
    Session session = null;
    Transaction transaction = null;

    DataResult<State> currentStateAndAllow = new DataResult<State>();

    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        // Getting the last state of the virtual machine
        VirtualmachineHB virtualMachineHB = (VirtualmachineHB) session.get(VirtualmachineHB.class,
                virtualMachine.getId());

        if (virtualMachine.getState().getId() == virtualMachineHB.getState().getIdState()
                && virtualMachineHB.getState().getIdState() != State.IN_PROGRESS) {
            // The given virtual machine is up to date, and is not in progress.
            // We set it now to IN_PROGRESS, and return that it is allowed to manipulate it
            StateHB newStateHB = new StateHB();
            newStateHB.setIdState(State.IN_PROGRESS);
            virtualMachineHB.setState(newStateHB);

            session.update(virtualMachineHB);

            // Generating the result
            currentStateAndAllow.setSuccess(true);
            currentStateAndAllow.setData((State) newStateHB.toPojo());
        } else {
            // The given virtual machine is not up to date, or the virtual machine
            // is already in the state State.IN_PROGRESS. Manipulating it is not allowed

            // Generating the result
            currentStateAndAllow.setSuccess(false);
            currentStateAndAllow.setData((State) virtualMachineHB.getState().toPojo());
        }

        transaction.commit();
    }

    catch (Exception e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();
        throw e;
    }

    return currentStateAndAllow;
}

From source file:com.abiquo.abiserver.commands.UserCommand.java

License:Mozilla Public License

/**
 * Modifies a User that already exists in the Data Base
 * // w ww.j  a  va 2  s  . co  m
 * @param userSession
 * @param users A list of users to be modified
 * @return A BasicResult object, informing if the user edition was successful
 */
protected BasicResult editUser(UserSession userSession, ArrayList<User> users) {
    BasicResult basicResult = new BasicResult();
    Session session = null;
    Transaction transaction = null;

    try {
        // Getting the user who has called this method
        UserHB userInfoHB = SessionUtil.findUserHBByName(userSession.getUser());

        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();
        EnterpriseHB userEnterpriseHB;

        for (User user : users) {
            // Getting from the Data Base the user that is being edited
            UserHB userHBToEdit = (UserHB) session.get(UserHB.class, user.getId());

            // Editing the user
            userHBToEdit.setRoleHB((RoleHB) user.getRole().toPojoHB());
            userHBToEdit.setUser(user.getUser());
            userHBToEdit.setName(user.getName());
            userHBToEdit.setSurname(user.getSurname());
            userHBToEdit.setDescription(user.getDescription());
            userHBToEdit.setEmail(user.getEmail());
            userHBToEdit.setLocale(user.getLocale());
            userHBToEdit.setPassword(user.getPass());
            userHBToEdit.setActive(user.getActive() ? 1 : 0);
            userHBToEdit.setDeleted(user.getDeleted() ? 1 : 0);

            // Enterprise update
            if (user.getEnterprise() != null) {
                userEnterpriseHB = (EnterpriseHB) session.get(EnterpriseHB.class, user.getEnterprise().getId());
                userHBToEdit.setEnterpriseHB(userEnterpriseHB);
            } else
                userHBToEdit.setEnterpriseHB(null);

            // Adding the last edit info
            userHBToEdit.setUserHBByIdUserLastModification(userInfoHB.getIdUser());
            userHBToEdit.setLastModificationDate(new Date());

            session.update(userHBToEdit);
        }
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        // Check if the error was produces because an invalid user name
        if (e instanceof ConstraintViolationException)
            basicResult.setResultCode(BasicResult.USER_INVALID);

        this.errorManager.reportError(resourceManager, basicResult, "editUser", e);

        return basicResult;
    }

    basicResult.setSuccess(true);
    basicResult.setMessage(UserCommand.resourceManager.getMessage("editUser.success"));

    return basicResult;
}

From source file:com.abiquo.abiserver.commands.UserCommand.java

License:Mozilla Public License

/**
 * Marks an user in Data Base as deleted. This services DOES NOT delete the user from the Data
 * Base//w  w w  .  j a  va 2 s  .  c om
 * 
 * @param userSession
 * @param user The user to be deleted
 * @return A BasicResult object, informing if the user deletion was successful
 */
protected BasicResult deleteUser(UserSession userSession, User user) {

    BasicResult basicResult = new BasicResult();
    Session session = null;
    Transaction transaction = null;

    try {
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();

        // Getting the user who has called this method
        UserHB userInfoHB = (UserHB) session.createCriteria(UserHB.class)
                .add(Restrictions.eq("user", userSession.getUser())).uniqueResult();

        // Getting from the Data Base the user that is being deleted
        UserHB userHBToDelete = (UserHB) session.get(UserHB.class, user.getId());

        // Marking the user as deleted.
        userHBToDelete.setDeleted(1);

        // Updating other fields
        userHBToDelete.setLastModificationDate(new Date());
        userHBToDelete.setUserHBByIdUserLastModification(userInfoHB.getIdUser());

        // Updating user in Data Base
        session.update(userHBToDelete);
        transaction.commit();
    } catch (Exception e) {
        if (transaction != null && transaction.isActive())
            transaction.rollback();

        this.errorManager.reportError(resourceManager, basicResult, "deleteUser", e);

        return basicResult;
    }

    basicResult.setSuccess(true);
    basicResult.setMessage(UserCommand.resourceManager.getMessage("deleteUser.success"));

    return basicResult;
}