Search in sources :

Example 41 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRTransactionableCallableTest method run.

@Test
public void run() {
    // with session
    MCRSession session = MCRSessionMgr.getCurrentSession();
    String sessionID = session.getID();
    TestCallable testCallable = new TestCallable();
    MCRTransactionableCallable<Boolean> transactionableRunnable = new MCRTransactionableCallable<>(testCallable, session);
    try {
        assertTrue(transactionableRunnable.call());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    assertEquals(sessionID, testCallable.getSessionContextID());
    session.close();
    // without session
    transactionableRunnable = new MCRTransactionableCallable<>(testCallable);
    try {
        assertTrue(transactionableRunnable.call());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    assertNotEquals(sessionID, testCallable.getSessionContextID());
    assertFalse(MCRSessionMgr.hasCurrentSession());
}
Also used : MCRSession(org.mycore.common.MCRSession) Test(org.junit.Test)

Example 42 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRTransactionableRunnableTest method run.

@Test
public void run() {
    // with session
    MCRSession session = MCRSessionMgr.getCurrentSession();
    String sessionID = session.getID();
    TestRunnable testRunnable = new TestRunnable();
    MCRTransactionableRunnable transactionableRunnable = new MCRTransactionableRunnable(testRunnable, session);
    transactionableRunnable.run();
    assertTrue(testRunnable.isExecuted());
    assertEquals(sessionID, testRunnable.getSessionContextID());
    session.close();
    // without session
    transactionableRunnable = new MCRTransactionableRunnable(testRunnable);
    transactionableRunnable.run();
    assertTrue(testRunnable.isExecuted());
    assertNotEquals(sessionID, testRunnable.getSessionContextID());
    assertFalse(MCRSessionMgr.hasCurrentSession());
}
Also used : MCRSession(org.mycore.common.MCRSession) Test(org.junit.Test)

Example 43 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRMetaHistoryItem method now.

static MCRMetaHistoryItem now(MCRObjectID id, MCRMetadataHistoryEventType type) {
    MCRMetaHistoryItem historyItem = new MCRMetaHistoryItem();
    historyItem.setId(id);
    historyItem.setTime(Instant.now());
    historyItem.setEventType(type);
    if (MCRSessionMgr.hasCurrentSession()) {
        MCRSession currentSession = MCRSessionMgr.getCurrentSession();
        historyItem.setUserID(currentSession.getUserInformation().getUserID());
        historyItem.setUserIP(currentSession.getCurrentIP());
    }
    return historyItem;
}
Also used : MCRSession(org.mycore.common.MCRSession)

Example 44 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRMetadataHistoryCommands method buildHistory.

@MCRCommand(syntax = "build metadata history of base {0}", help = "build metadata history of all objects with base id {0}")
public static List<String> buildHistory(String baseId) {
    MCRMetadataStore store = MCRXMLMetadataManager.instance().getStore(baseId);
    if (store instanceof MCRVersioningMetadataStore) {
        LogManager.getLogger().info("Verify SVN history of {}", baseId);
        ((MCRVersioningMetadataStore) store).verify();
    }
    ExecutorService executorService = Executors.newWorkStealingPool();
    MCRSession currentSession = MCRSessionMgr.getCurrentSession();
    int maxId = store.getHighestStoredID();
    AtomicInteger completed = new AtomicInteger(maxId);
    IntStream.rangeClosed(1, maxId).parallel().mapToObj(i -> MCRObjectID.formatID(baseId, i)).map(MCRObjectID::getInstance).map(id -> new MCRTransactionableCallable<>(Executors.callable(() -> {
        EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
        getHistoryItems(id).sequential().forEach(em::persist);
        completed.decrementAndGet();
    }), currentSession)).forEach(executorService::submit);
    executorService.shutdown();
    boolean waitToFinish = true;
    while (!executorService.isTerminated() && waitToFinish) {
        LogManager.getLogger().info("Waiting for history of {} objects/derivates.", completed.get());
        try {
            executorService.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            waitToFinish = false;
        }
    }
    return Collections.emptyList();
}
Also used : IntStream(java.util.stream.IntStream) MCRTransactionableCallable(org.mycore.util.concurrent.MCRTransactionableCallable) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) CriteriaDelete(javax.persistence.criteria.CriteriaDelete) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRCreatorCache(org.mycore.datamodel.common.MCRCreatorCache) ExecutorService(java.util.concurrent.ExecutorService) Root(javax.persistence.criteria.Root) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) List(java.util.List) Stream(java.util.stream.Stream) MCRSystemUserInformation(org.mycore.common.MCRSystemUserInformation) MCRVersioningMetadataStore(org.mycore.datamodel.ifs2.MCRVersioningMetadataStore) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRSession(org.mycore.common.MCRSession) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRSessionMgr(org.mycore.common.MCRSessionMgr) MCRObject(org.mycore.datamodel.metadata.MCRObject) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MCRMetadataStore(org.mycore.datamodel.ifs2.MCRMetadataStore) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRTransactionableCallable(org.mycore.util.concurrent.MCRTransactionableCallable) MCRMetadataStore(org.mycore.datamodel.ifs2.MCRMetadataStore) EntityManager(javax.persistence.EntityManager) MCRSession(org.mycore.common.MCRSession) MCRVersioningMetadataStore(org.mycore.datamodel.ifs2.MCRVersioningMetadataStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 45 with MCRSession

use of org.mycore.common.MCRSession in project mycore by MyCoRe-Org.

the class MCRCommandLineInterface method whoami.

/**
 * Prints out the current user.
 */
public static void whoami() {
    MCRSession session = MCRSessionMgr.getCurrentSession();
    String userName = session.getUserInformation().getUserID();
    output("You are user " + userName);
}
Also used : MCRSession(org.mycore.common.MCRSession)

Aggregations

MCRSession (org.mycore.common.MCRSession)63 IOException (java.io.IOException)15 MCRUserInformation (org.mycore.common.MCRUserInformation)10 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)10 MCRPath (org.mycore.datamodel.niofs.MCRPath)6 SignedJWT (com.nimbusds.jwt.SignedJWT)5 Date (java.util.Date)5 EntityManager (javax.persistence.EntityManager)5 Path (java.nio.file.Path)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)4 SAXException (org.xml.sax.SAXException)4 UnknownHostException (java.net.UnknownHostException)3 Document (org.jdom2.Document)3 MCRAccessException (org.mycore.access.MCRAccessException)3 MCRException (org.mycore.common.MCRException)3 MCRPersistenceException (org.mycore.common.MCRPersistenceException)3 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRRestAPIError (org.mycore.restapi.v1.errors.MCRRestAPIError)3