Search in sources :

Example 1 with SyncerExecutor

use of org.olat.core.util.coordinate.SyncerExecutor in project OpenOLAT by OpenOLAT.

the class BinderUserInformationsDAO method updateBinderUserInformations.

/**
 * Update (or create if not exists) the course informations for a user. To creation
 * of the user infos is made with doInSync.
 *
 * @param binder The binder
 * @param identity The identity
 */
public void updateBinderUserInformations(final Binder binder, final Identity identity) {
    int updatedRows = lowLevelUpdate(binder, identity);
    // to make it quick
    dbInstance.commit();
    if (updatedRows == 0) {
        OLATResourceable lockRes = OresHelper.createOLATResourceableInstance("BinderLaunchDate::Identity", identity.getKey());
        CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(lockRes, new SyncerExecutor() {

            @Override
            public void execute() {
                try {
                    int retryUpdatedRows = lowLevelUpdate(binder, identity);
                    if (retryUpdatedRows == 0) {
                        createAndPersistUserInfos(binder, identity);
                    }
                } catch (Exception e) {
                    log.error("Cannot update binder informations for: " + identity + " from " + identity, e);
                }
            }
        });
    }
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) SyncerExecutor(org.olat.core.util.coordinate.SyncerExecutor)

Example 2 with SyncerExecutor

use of org.olat.core.util.coordinate.SyncerExecutor in project OpenOLAT by OpenOLAT.

the class ProjectBrokerManagerImpl method setProjectState.

@Override
public void setProjectState(final Project project, final String state) {
    final Long projectBrokerId = project.getProjectBroker().getKey();
    OLATResourceable projectBrokerOres = OresHelper.createOLATResourceableInstance(this.getClass(), projectBrokerId);
    CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(projectBrokerOres, new SyncerExecutor() {

        @Override
        public void execute() {
            // For cluster-safe : reload project object here another node might have changed this in the meantime
            Project reloadedProject = (Project) dbInstance.loadObject(project, true);
            reloadedProject.setState(state);
            updateProjectAndInvalidateCache(reloadedProject);
        }
    });
}
Also used : Project(org.olat.course.nodes.projectbroker.datamodel.Project) OLATResourceable(org.olat.core.id.OLATResourceable) SyncerExecutor(org.olat.core.util.coordinate.SyncerExecutor)

Example 3 with SyncerExecutor

use of org.olat.core.util.coordinate.SyncerExecutor in project OpenOLAT by OpenOLAT.

the class BGAreaManagerImpl method deleteBGArea.

/**
 * @see org.olat.group.area.BGAreaManager#deleteBGArea(org.olat.group.area.BGArea)
 */
@Override
public void deleteBGArea(final BGArea area) {
    final OLATResource resource = area.getResource();
    CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(resource, new SyncerExecutor() {

        @Override
        public void execute() {
            BGArea reloadArea = loadArea(area.getKey());
            if (reloadArea != null) {
                // 1) delete all area - group relations
                deleteBGtoAreaRelations(reloadArea);
                // 2) delete area - assessment mode relations
                deleteAssessmentModeToAreaRelations(reloadArea);
                // 3) delete area itself
                dbInstance.deleteObject(reloadArea);
                logAudit("Deleted Business Group Area", reloadArea.toString());
            } else {
                logAudit("Business Group Area was already deleted", area.toString());
            }
        }
    });
}
Also used : OLATResource(org.olat.resource.OLATResource) SyncerExecutor(org.olat.core.util.coordinate.SyncerExecutor)

Example 4 with SyncerExecutor

use of org.olat.core.util.coordinate.SyncerExecutor in project OpenOLAT by OpenOLAT.

the class CoordinatorTest method testDoInSyncWithSyncerExecutor.

/**
 * Test with 2 threads T1 & T2.
 * T1		      T2
 * doInSync T1-1  sleep 5sec
 * sleep 10sec    ...
 * ...            ...
 * ...            doInSync T2-1
 * ...            sleep 10sec
 * ...            ...
 * doInSync T1-2  ...
 * finished       ...
 *                doInSync T2-2
 *                finished
 */
@Test
public void testDoInSyncWithSyncerExecutor() {
    final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
    final CountDownLatch finishCount = new CountDownLatch(2);
    final OLATResourceable ores = OresHelper.createOLATResourceableInstance("testDoInSync", new Long("123"));
    // thread 1
    new Thread(new Runnable() {

        public void run() {
            try {
                // do something in sync
                CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(ores, new SyncerExecutor() {

                    public void execute() {
                        log.info("Thread-1: execute doInSync 1");
                    }
                });
                // end syncerCallback
                // sleep
                sleep(1000);
                // do again do something in sync
                CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(ores, new SyncerExecutor() {

                    public void execute() {
                        log.info("Thread-1: execute doInSync 2");
                    }
                });
                // end syncerCallback
                log.info("Thread-1: finished");
                statusList.add(Boolean.TRUE);
            } catch (Exception e) {
                exceptionHolder.add(e);
            } finally {
                try {
                    DBFactory.getInstance().closeSession();
                } catch (Exception e) {
                // ignore
                }
                finishCount.countDown();
            }
        }
    }).start();
    // thread 2
    new Thread(new Runnable() {

        public void run() {
            try {
                // sleep
                sleep(500);
                // do something in sync
                CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(ores, new SyncerExecutor() {

                    public void execute() {
                        log.info("Thread-2: execute doInSync 1");
                    }
                });
                // end syncerCallback
                // sleep
                sleep(1000);
                // do again do something in sync
                CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(ores, new SyncerExecutor() {

                    public void execute() {
                        log.info("Thread-2: execute doInSync 2");
                    }
                });
                // end syncerCallback
                log.info("Thread-2: finished");
                statusList.add(Boolean.TRUE);
            } catch (Exception e) {
                exceptionHolder.add(e);
            } finally {
                try {
                    DBFactory.getInstance().closeSession();
                } catch (Exception e) {
                // ignore
                }
                finishCount.countDown();
            }
        }
    }).start();
    // sleep until t1 and t2 should have terminated/excepted
    try {
        finishCount.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Assert.fail("Threads did not finish in 10sec");
    }
    // if not -> they are in deadlock and the db did not detect it
    for (Exception exception : exceptionHolder) {
        log.error("exception: ", exception);
    }
    Assert.assertEquals("It throws an exception in test", 0, exceptionHolder.size());
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) SyncerExecutor(org.olat.core.util.coordinate.SyncerExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) AssertException(org.olat.core.logging.AssertException) Test(org.junit.Test)

Example 5 with SyncerExecutor

use of org.olat.core.util.coordinate.SyncerExecutor in project OpenOLAT by OpenOLAT.

the class ProfileFormController method formOK.

@Override
protected void formOK(final UserRequest ureq) {
    User user = identityToModify.getUser();
    // update each user field
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
        FormItem formItem = formItems.get(userPropertyHandler.getName());
        if (formItem.isEnabled()) {
            userPropertyHandler.updateUserFromFormItem(user, formItem);
        }
    }
    if (portraitDeleted) {
        File img = dps.getLargestPortrait(identityToModify.getName());
        if (img != null) {
            dps.deletePortrait(identityToModify);
            notifyPortraitChanged();
        }
    }
    File uploadedImage = portraitUpload.getUploadFile();
    String uploadedFilename = portraitUpload.getUploadFileName();
    if (uploadedImage != null) {
        dps.setPortrait(uploadedImage, uploadedFilename, identityToModify.getName());
        notifyPortraitChanged();
    }
    if (logoDeleted) {
        File img = dps.getLargestLogo(identityToModify.getName());
        if (img != null) {
            dps.deleteLogo(identityToModify);
            notifyPortraitChanged();
        }
    }
    if (logoUpload != null) {
        File uploadedLogo = logoUpload.getUploadFile();
        String uploadedLogoname = logoUpload.getUploadFileName();
        if (uploadedLogo != null) {
            dps.setLogo(uploadedLogo, uploadedLogoname, identityToModify.getName());
            notifyPortraitChanged();
        }
    }
    // Store the "about me" text.
    HomePageConfig conf = hpcm.loadConfigFor(identityToModify.getName());
    conf.setTextAboutMe(textAboutMe.getValue());
    hpcm.saveConfigTo(identityToModify.getName(), conf);
    // fire the appropriate event
    fireEvent(ureq, Event.DONE_EVENT);
    // update the user profile data
    CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(OresHelper.createOLATResourceableInstance(Identity.class, identityToModify.getKey()), new SyncerExecutor() {

        @Override
        public void execute() {
            UserManager um = UserManager.getInstance();
            identityToModify = (Identity) DBFactory.getInstance().loadObject(identityToModify);
            currentEmail = identityToModify.getUser().getProperty("email", null);
            identityToModify = updateIdentityFromFormData(identityToModify);
            changedEmail = identityToModify.getUser().getProperty("email", null);
            emailChanged = false;
            if ((currentEmail == null && StringHelper.containsNonWhitespace(changedEmail)) || (currentEmail != null && !currentEmail.equals(changedEmail))) {
                if (isAllowedToChangeEmailWithoutVerification(ureq) || !StringHelper.containsNonWhitespace(changedEmail)) {
                    String key = identityToModify.getUser().getProperty("emchangeKey", null);
                    TemporaryKey tempKey = rm.loadTemporaryKeyByRegistrationKey(key);
                    if (tempKey != null) {
                        rm.deleteTemporaryKey(tempKey);
                    }
                    securityManager.deleteInvalidAuthenticationsByEmail(currentEmail);
                } else {
                    emailChanged = true;
                    // change email address to old address until it is verified
                    identityToModify.getUser().setProperty("email", currentEmail);
                }
            }
            if (!um.updateUserFromIdentity(identityToModify)) {
                getWindowControl().setInfo(translate("profile.unsuccessful"));
                // reload user data from db
                identityToModify = BaseSecurityManager.getInstance().loadIdentityByKey(identityToModify.getKey());
            }
            OLATResourceable modRes = OresHelper.createOLATResourceableInstance(Identity.class, identityToModify.getKey());
            CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(new MultiUserEvent("changed"), modRes);
            if (!emailChanged) {
                fireEvent(ureq, Event.FAILED_EVENT);
            }
        }
    });
    if (emailChanged) {
        removeAsListenerAndDispose(dialogCtr);
        String dialogText = "";
        if (identityToModify.equals(ureq.getIdentity())) {
            dialogText = translate("email.change.dialog.text");
        } else {
            dialogText = translate("email.change.dialog.text.usermanager");
        }
        dialogCtr = DialogBoxUIFactory.createYesNoDialog(ureq, getWindowControl(), translate("email.change.dialog.title"), dialogText);
        listenTo(dialogCtr);
        dialogCtr.activate();
    }
}
Also used : User(org.olat.core.id.User) OLATResourceable(org.olat.core.id.OLATResourceable) FormItem(org.olat.core.gui.components.form.flexible.FormItem) TemporaryKey(org.olat.registration.TemporaryKey) SyncerExecutor(org.olat.core.util.coordinate.SyncerExecutor) Identity(org.olat.core.id.Identity) File(java.io.File) MultiUserEvent(org.olat.core.util.event.MultiUserEvent) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Aggregations

SyncerExecutor (org.olat.core.util.coordinate.SyncerExecutor)48 OLATResourceable (org.olat.core.id.OLATResourceable)30 ICourse (org.olat.course.ICourse)12 Property (org.olat.properties.Property)12 Project (org.olat.course.nodes.projectbroker.datamodel.Project)8 CoursePropertyManager (org.olat.course.properties.CoursePropertyManager)8 Identity (org.olat.core.id.Identity)6 MultiUserEvent (org.olat.core.util.event.MultiUserEvent)6 AssessmentChangedEvent (org.olat.course.assessment.AssessmentChangedEvent)6 Date (java.util.Date)4 Iterator (java.util.Iterator)4 Test (org.junit.Test)4 UserNodeAuditManager (org.olat.course.auditing.UserNodeAuditManager)4 ProjectBroker (org.olat.course.nodes.projectbroker.datamodel.ProjectBroker)4 PropertyManager (org.olat.properties.PropertyManager)4 OLATResource (org.olat.resource.OLATResource)4 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 CountDownLatch (java.util.concurrent.CountDownLatch)2