Example usage for org.hibernate Session save

List of usage examples for org.hibernate Session save

Introduction

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

Prototype

Serializable save(Object object);

Source Link

Document

Persist the given transient instance, first assigning a generated identifier.

Usage

From source file:au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.tests.RegisterLocalRigTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.rigprovider.impl.RegisterLocalRig#registerRig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
 *///from w w  w. j  a v a2  s  .  co  m
@Test
public void testAddRigToSchedServerActiveRig() {
    Session ses = DataAccessActivator.getNewSession();

    String name = "register1";
    String type = "registertest";
    String caps = "a,b,c,d,e,f";
    String contactUrl = "http://lbremote1.eng.uts.edu.au:7070/services/RigClientService";

    RigType existType = new RigType(type, 180, false);
    RigCapabilities existCaps = new RigCapabilities(caps);
    ses.beginTransaction();
    ses.save(existType);
    ses.save(existCaps);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existType);
    ses.evict(existCaps);

    Rig existRig = new Rig(existType, existCaps, name, contactUrl, new Date(), false, "Broken.", false, true,
            true);
    ses.beginTransaction();
    ses.save(existRig);
    ses.getTransaction().commit();
    ses.flush();
    ses.evict(existRig);

    assertFalse(this.register.registerRig(name, type, caps, contactUrl));

    RigCapabilities capsRec = new RigCapabilitiesDao(ses).findCapabilites(caps);
    RigType typeRec = new RigTypeDao(ses).findByName(type);
    Rig rigRec = new RigDao(ses).findByName(name);

    /* Cleanup. */
    ses.beginTransaction();
    ses.createQuery("DELETE FROM RigLog WHERE rig='" + rigRec.getId() + "'").executeUpdate();
    ses.getTransaction().commit();

    ses.beginTransaction();
    ses.delete(rigRec);
    ses.delete(capsRec);
    ses.delete(typeRec);
    ses.getTransaction().commit();

    assertEquals("Exists", this.register.getFailedReason());
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public void createOrUpdateArkModuleFunction(ArkModule arkModule, Collection<ArkFunction> selectedArkFunctions) {
    Session session = getSession();

    // Remove previous list of ArkFunctions
    Collection<ArkModuleFunction> arkModuleFunctions = getArkModuleFunctionByArkModule(arkModule);
    for (ArkModuleFunction arkModuleFunctionToRemove : arkModuleFunctions) {
        session.delete(arkModuleFunctionToRemove);
    }//from   w  w w . ja v a  2 s  . c  om

    // Insert the ArkFunctions for the ArkModule
    Long functionSequence = new Long(1);
    for (Iterator<ArkFunction> iterator = selectedArkFunctions.iterator(); iterator.hasNext();) {
        ArkModuleFunction arkModuleFunction = new ArkModuleFunction();
        ArkFunction arkFunction = iterator.next();
        arkModuleFunction.setArkModule(arkModule);
        arkModuleFunction.setArkFunction(arkFunction);
        arkModuleFunction.setFunctionSequence(functionSequence++);
        session.save(arkModuleFunction);
    }

    // Flush must be the last thing to call. If there is any other code/logic to be added make sure session.flush() is invoked after that.
    session.flush();
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public void createArkModuleRole(ArkModule arkModule, Collection<ArkRole> selectedArkRoles) {
    Session session = getSession();

    // Remove previous list of ArkRoles
    List<ArkModuleRole> arkModuleRoles = getArkModuleRoleByArkModule(arkModule);
    for (ArkModuleRole arkModuleRoleToRemove : arkModuleRoles) {
        session.delete(arkModuleRoleToRemove);
    }/* w  w w  .  ja v a2  s. c om*/

    ArkPermission arkPermission = getArkPermissionByName("READ");

    // Insert the ArkRoles for the ArkModule
    for (Iterator<ArkRole> iterator = selectedArkRoles.iterator(); iterator.hasNext();) {
        ArkModuleRole arkModuleRole = new ArkModuleRole();
        ArkRole arkRole = iterator.next();
        arkModuleRole.setArkModule(arkModule);
        arkModuleRole.setArkRole(arkRole);

        session.save(arkModuleRole);

        // Add a default READ permission to NEW module/roles
        List<ArkFunction> arkFunctions = getArkFunctionListByArkModule(arkModule);
        for (Iterator iterator2 = arkFunctions.iterator(); iterator2.hasNext();) {
            ArkFunction arkFunction = (ArkFunction) iterator2.next();
            ArkRolePolicyTemplate arkRolePolicyTemplate = new ArkRolePolicyTemplate();
            arkRolePolicyTemplate.setArkRole(arkRole);
            arkRolePolicyTemplate.setArkModule(arkModule);
            arkRolePolicyTemplate.setArkFunction(arkFunction);
            arkRolePolicyTemplate.setArkPermission(arkPermission);

            session.save(arkRolePolicyTemplate);
        }
    }

    // Flush must be the last thing to call. If there is any other code/logic to be added make sure session.flush() is invoked after that.
    session.flush();
}

From source file:au.org.theark.admin.model.dao.AdminDao.java

License:Open Source License

public void updateArkModuleRole(ArkModule arkModule, Collection<ArkRole> selectedArkRoles) {
    Session session = getSession();

    // Remove previous list of ArkRoles
    List<ArkModuleRole> arkModuleRoles = getArkModuleRoleByArkModule(arkModule);
    for (ArkModuleRole arkModuleRoleToRemove : arkModuleRoles) {
        session.delete(arkModuleRoleToRemove);
    }/* w ww .  ja v a  2 s .  c o m*/

    // Insert the ArkRoles for the ArkModule
    for (Iterator<ArkRole> iterator = selectedArkRoles.iterator(); iterator.hasNext();) {
        ArkModuleRole arkModuleRole = new ArkModuleRole();
        ArkRole arkRole = iterator.next();
        arkModuleRole.setArkModule(arkModule);
        arkModuleRole.setArkRole(arkRole);
        session.save(arkModuleRole);
    }

    // Flush must be the last thing to call. If there is any other code/logic to be added make sure session.flush() is invoked after that.
    session.flush();
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

/**
 * Create a new Ark User in the system and associates the arkuser with the study and links the user to one or more Modules and Roles that was
 * configured for the Study./*  w  w  w . jav  a 2s  . co  m*/
 */
public void createArkUser(ArkUserVO arkUserVO) {
    Session session = getSession();
    session.save(arkUserVO.getArkUserEntity());
    List<ArkUserRole> arkUserRoleList = arkUserVO.getArkUserRoleList();
    for (ArkUserRole arkUserRole : arkUserRoleList) {
        if (arkUserRole.getArkRole() != null) {
            arkUserRole.setArkUser(arkUserVO.getArkUserEntity());
            session.save(arkUserRole);
        }
    }

    for (UserConfig config : arkUserVO.getArkUserConfigs()) {
        session.saveOrUpdate(config);
    }
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

/**
 * Will update the ArkUser or adds the user into ArkUser table. It determines the List of ArkUserRoles for insertion and removal and processes
 * these ArkUserRole lists.//w w  w. j a va  2  s .c om
 * 
 * @param arkUserVO
 * @throws EntityNotFoundException
 * @throws ArkSystemException
 */
public void updateArkUser(ArkUserVO arkUserVO) throws EntityNotFoundException, ArkSystemException {
    try {
        if (arkUserVO.getArkUserEntity() != null && arkUserVO.getArkUserEntity().getId() != null) {
            // Never update/delete Super User records
            Session session = getSession();
            if (!isUserAdminHelper(arkUserVO.getArkUserEntity().getLdapUserName(),
                    au.org.theark.core.security.RoleConstants.ARK_ROLE_SUPER_ADMINISTATOR)) {
                // User is present in the ArkUserTable can go for update of the entity and related objects (ArkUserRoles)
                session.update(arkUserVO.getArkUserEntity());

                // Insert new ArkUserRole
                for (ArkUserRole arkUserRoleToAdd : getArkUserRolesToAdd(arkUserVO)) {
                    if (arkUserRoleToAdd.getArkRole() != null) {
                        arkUserRoleToAdd.setArkUser(arkUserVO.getArkUserEntity());
                        session.save(arkUserRoleToAdd);
                    }
                }

                for (ArkUserRole arkUserRoleToRemove : getArkUserRolesToRemove(arkUserVO)) {
                    session.delete(arkUserRoleToRemove);
                }
            }
            for (UserConfig config : arkUserVO.getArkUserConfigs()) {
                session.update(config);
            }
        } else {
            createArkUser(arkUserVO);
        }
    } catch (Exception exception) {
        StringBuffer sb = new StringBuffer();
        sb.append("There was an exception while updating the ArkUser in the backend.");
        sb.append(exception.getMessage());
        log.error(sb.toString());
        throw new ArkSystemException();
    }
}

From source file:au.org.theark.core.dao.ArkAuthorisationDao.java

License:Open Source License

/**
 * This method will update the ark user role for a study for a existing user.
 * @param arkUserVO/*from   www .  j  a  v  a  2s  .c om*/
 */
public void updateArkUserRoleListForExsistingUser(ArkUserVO arkUserVO) {
    Session session = getSession();
    List<ArkUserRole> arkUserRoleList = arkUserVO.getArkUserRoleList();
    for (ArkUserRole arkUserRole : arkUserRoleList) {
        if (arkUserRole.getArkRole() != null) {
            arkUserRole.setArkUser(arkUserVO.getArkUserEntity());
            session.save(arkUserRole);
        }
    }
    for (UserConfig config : arkUserVO.getArkUserConfigs()) {
        session.update(config);
    }
}

From source file:au.org.theark.lims.model.dao.InventoryDao.java

License:Open Source License

public void updateInvSite(LimsVO modelObject) {

    InvSite invSite = modelObject.getInvSite();
    Session session = getSession();
    session.update(invSite);//from   w  ww  .ja  v  a  2  s  .  c  o m
    session.flush();
    session.refresh(invSite);
    List<StudyInvSite> existingInvSites = invSite.getStudyInvSites();

    //for (StudyInvSite sis : modelObject.getInvSite().getStudyInvSites()) {
    //      session.delete(sis);
    //      session.flush();
    //}

    List<Long> selectedAndExistingStudies = new ArrayList<Long>();
    List<Study> selectedStudies = modelObject.getSelectedStudies();

    for (Study selectedStudy : selectedStudies) {
        boolean studyAlreadyLinked = false;
        log.info("selected =" + selectedStudy.getId());

        for (StudyInvSite sis : existingInvSites) {
            Study existingStudy = sis.getStudy();
            log.info("  existing=" + selectedStudy.getId());
            if (existingStudy.getId().equals(selectedStudy.getId())) {
                log.info("found a match for " + selectedStudy.getId());
                studyAlreadyLinked = true;
                selectedAndExistingStudies.add(selectedStudy.getId());
                break; // leave it along
            }
        }

        if (!studyAlreadyLinked) {
            log.info("about to create" + selectedStudy.getId());
            StudyInvSite studyInvSite = new StudyInvSite();
            studyInvSite.setStudy(selectedStudy);
            studyInvSite.setInvSite(invSite);
            session.save(studyInvSite);
        }

    }

    for (StudyInvSite sis : existingInvSites) {
        log.info("about to investigate for deletion existing study " + sis.getStudy().getId());
        boolean deletePreviouslyExistingSiteAsItWasNotSelected = true;
        for (Long selectedId : selectedAndExistingStudies) {
            log.info("compare it to selected " + selectedId);
            if (selectedId.equals(sis.getStudy().getId())) {
                log.info("recommending you don't delete");
                deletePreviouslyExistingSiteAsItWasNotSelected = false;
            } else {
                log.info("match not found.");
            }
        }
        if (deletePreviouslyExistingSiteAsItWasNotSelected) {
            log.info("deleting " + sis.getStudy().getId());
            session.delete(sis);
        }
    }
    session.flush();
    session.refresh(invSite);

    //List<StudyInvSite> existingInvSites = invSite.getStudyInvSites();

    //for (StudyInvSite sis : modelObject.getInvSite().getStudyInvSites()) {
    //   session.delete(sis);
    //   session.flush();
    //}

    /*
            
    List<Study> selectedAndExistingStudies = new ArrayList<Study>();
    List<Study> selectedStudies = modelObject.getSelectedStudies();
            
    for (Study selectedStudy : selectedStudies) {
       boolean studyAlreadyLinked = false;
       for(StudyInvSite sis: existingInvSites){
    Study existingStudy = sis.getStudy();
    if(existingStudy.equals(selectedStudy)){
       studyAlreadyLinked = true;
       selectedAndExistingStudies.add(selectedStudy);
       break; // leave it along
    }
       }
               
       if(!studyAlreadyLinked){
    StudyInvSite studyInvSite = new StudyInvSite();
    studyInvSite.setStudy(selectedStudy);
    studyInvSite.setInvSite(modelObject.getInvSite());
    session.save(studyInvSite);
       }
               
    }
            
    for(StudyInvSite sis: existingInvSites){
       if(!selectedAndExistingStudies.contains(sis.getStudy())){
    session.delete(sis);
       }
    }
    */
}

From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java

License:Open Source License

public void createUpload(Upload phenoUpload) {
    Session session = getSession();

    //currentUser = SecurityUtils.getSubject();
    Date dateNow = new Date(System.currentTimeMillis());
    //phenoUpload.setInsertTime(dateNow);
    //phenoUpload.setUserId(currentUser.getPrincipal().toString());

    if (phenoUpload.getStartTime() == null)
        phenoUpload.setStartTime(dateNow);

    session.save(phenoUpload);
}

From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java

License:Open Source License

/**
 * Create  a CustomFieldGroup and then link the selected custom fields into the Group via
 * the CustomFieldDisplay. For each Custom Field create a new CustomFieldDisplay
 * @param customFieldGroupVO/* w  w w.  j ava2 s  . co  m*/
 */
public void createCustomFieldGroup(CustomFieldGroupVO customFieldGroupVO) throws ArkSystemException {

    CustomFieldGroup customFieldGroup = customFieldGroupVO.getCustomFieldGroup();
    Session session = getSession();
    if (customFieldGroup.getPublished() == null) {
        customFieldGroup.setPublished(new Boolean("false"));
    }
    session.save(customFieldGroup);
    ArrayList<CustomField> customFieldList = customFieldGroupVO.getSelectedCustomFields();

    int fieldposition = 0;
    for (CustomField customField : customFieldList) {
        ++fieldposition;
        CustomFieldDisplay customFieldDisplay = new CustomFieldDisplay();
        customFieldDisplay.setCustomFieldGroup(customFieldGroup);
        customFieldDisplay.setCustomField(customField);
        customFieldDisplay.setSequence(new Long(fieldposition));
        session.save(customFieldDisplay);
        log.debug("Saved CustomFieldDisplay for Custom Field Group");
    }
}