Search in sources :

Example 16 with SyncerExecutor

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

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)

Example 17 with SyncerExecutor

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

the class NewCachePersistingAssessmentManager method saveNodeCoachComment.

/**
 * @see org.olat.course.assessment.AssessmentManager#saveNodeCoachComment(org.olat.course.nodes.CourseNode,
 *      org.olat.core.id.Identity, java.lang.String)
 */
public void saveNodeCoachComment(final CourseNode courseNode, final Identity assessedIdentity, final String comment) {
    ICourse course = CourseFactory.loadCourse(ores);
    final CoursePropertyManager cpm = course.getCourseEnvironment().getCoursePropertyManager();
    CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(createOLATResourceableForLocking(assessedIdentity), new SyncerExecutor() {

        public void execute() {
            Property commentProperty = cpm.findCourseNodeProperty(courseNode, assessedIdentity, null, COACH_COMMENT);
            if (commentProperty == null) {
                commentProperty = cpm.createCourseNodePropertyInstance(courseNode, assessedIdentity, null, COACH_COMMENT, null, null, null, comment);
                cpm.saveProperty(commentProperty);
            } else {
                commentProperty.setTextValue(comment);
                cpm.updateProperty(commentProperty);
            }
            // add to cache
            putPropertyIntoCache(assessedIdentity, commentProperty);
        }
    });
    // olat::: no node log here? (because what we did above is a node log with custom text AND by a coach)?
    // notify about changes
    AssessmentChangedEvent ace = new AssessmentChangedEvent(AssessmentChangedEvent.TYPE_COACH_COMMENT_CHANGED, assessedIdentity);
    CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(ace, course);
    // user activity logging
    ThreadLocalUserActivityLogger.log(AssessmentLoggingAction.ASSESSMENT_COACHCOMMENT_UPDATED, getClass(), LoggingResourceable.wrap(assessedIdentity), LoggingResourceable.wrapNonOlatResource(StringResourceableType.qtiCoachComment, "", StringHelper.stripLineBreaks(comment)));
}
Also used : AssessmentChangedEvent(org.olat.course.assessment.AssessmentChangedEvent) ICourse(org.olat.course.ICourse) SyncerExecutor(org.olat.core.util.coordinate.SyncerExecutor) Property(org.olat.properties.Property) CoursePropertyManager(org.olat.course.properties.CoursePropertyManager)

Example 18 with SyncerExecutor

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

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 19 with SyncerExecutor

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

the class UserCourseInformationsManagerImpl method updateUserCourseInformations.

/**
 * Update (or create if not exists) the course informations for a user
 * @param userCourseEnv
 * @return
 */
@Override
public void updateUserCourseInformations(final OLATResource courseResource, final Identity identity) {
    int updatedRows = lowLevelUpdate(courseResource, identity);
    // to make it quick
    dbInstance.commit();
    if (updatedRows == 0) {
        OLATResourceable lockRes = OresHelper.createOLATResourceableInstance("CourseLaunchDate::Identity", identity.getKey());
        CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(lockRes, new SyncerExecutor() {

            @Override
            public void execute() {
                try {
                    int retryUpdatedRows = lowLevelUpdate(courseResource, identity);
                    if (retryUpdatedRows == 0) {
                        UserCourseInfosImpl infos = new UserCourseInfosImpl();
                        infos.setIdentity(identity);
                        infos.setCreationDate(new Date());
                        infos.setInitialLaunch(new Date());
                        infos.setLastModified(new Date());
                        infos.setRecentLaunch(new Date());
                        infos.setVisit(1);
                        infos.setResource(courseResource);
                        dbInstance.getCurrentEntityManager().persist(infos);
                    }
                } catch (Exception e) {
                    log.error("Cannot update course informations for: " + identity + " from " + identity, e);
                }
            }
        });
    }
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) SyncerExecutor(org.olat.core.util.coordinate.SyncerExecutor) UserCourseInfosImpl(org.olat.course.assessment.model.UserCourseInfosImpl) Date(java.util.Date)

Example 20 with SyncerExecutor

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

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)

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