/** * Returns the managed user context. * * @return User context */ @SuppressWarnings("unchecked") public static ISharedContext<IUser> getUserContext() { return (ISharedContext<IUser>) ContextManager.getInstance().getSharedContext(UserContext.class.getName()); }
/** * Updates managed contexts based on the marshaledContext. * * @param marshaledContext The marshaled context to process. * @param commit If true the pending contexts are committed. * @return Reason if context change rejected. */ /*package*/String setMarshaledContext(ContextItems marshaledContext, boolean commit) { StringBuilder reason = new StringBuilder(); for (IManagedContext managedContext : managedContexts) { try { if (managedContext.setContextItems(marshaledContext)) { localChangeBegin(managedContext); appendResponse(reason, localChangeEnd(managedContext, true, true)); } } catch (Exception e) { log.error("Error processing marshaled context change.", e); appendResponse(reason, e.toString()); } } if (commit) { commitContexts(reason.length() == 0, false); } return reason.toString(); }
/** * Initialize context objects to their default state. The default state should match the CCOW * context if one exists, or an initial state as determined by the context object. * * @return {@link #init(IManagedContext)} */ public boolean init() { return init(null); }
/** * @see org.carewebframework.api.context.IContextManager#reset */ @Override public boolean reset(boolean silent) { pendingStack.clear(); commitStack.clear(); boolean result = true; for (IManagedContext managedContext : managedContexts) { result &= resetItem(managedContext, silent); if (!silent && !result) { break; } } boolean commit = silent || result; commitContexts(commit, commit); return result; }
/** * Resets the managed context. * * @param item Managed context to reset. * @param silent Silent flag. * @param callback Callback for polling. */ private void resetItem(IManagedContext<?> item, boolean silent, ISurveyCallback callback) { try { localChangeBegin(item); item.reset(); localChangeEnd(item, silent, true, callback); } catch (ContextException e) { execCallback(callback, e); } }
/** * Resets the managed context. * * @param item Managed context to reset. * @param silent Silent flag. * @return True if change was accepted. */ private boolean resetItem(IManagedContext item, boolean silent) { try { localChangeBegin(item); item.reset(); return StringUtils.isEmpty(localChangeEnd(item, silent, true)); } catch (ContextException e) { return true; } }
private void setMarshaledContext(ContextItems marshaledContext, Iterator<IManagedContext<?>> iter, ISurveyResponse response, ISurveyCallback callback) { if (iter.hasNext()) { IManagedContext<?> managedContext = iter.next(); try { if (managedContext.setContextItems(marshaledContext)) { localChangeBegin(managedContext); localChangeEnd(managedContext, true, true, aresponse -> { response.merge(aresponse); setMarshaledContext(marshaledContext, iter, response, callback); }); } } catch (Exception e) { log.error("Error processing marshaled context change.", e); response.reject(e.toString()); setMarshaledContext(marshaledContext, iter, response, callback); } } else { execCallback(callback, response); } }
/** * Initializes the managed context. * * @param item Managed context to initialize. * @param callback Callback to report subscriber responses. */ private void initItem(IManagedContext<?> item, ISurveyCallback callback) { try { localChangeBegin(item); if (hasSubject(item.getContextName())) { item.setContextItems(contextItems); } else { item.init(); } localChangeEnd(item, callback); } catch (ContextException e) { log.error("Error initializing context.", e); execCallback(callback, new SurveyResponse(e.toString())); } }
/** * Joins the CCOW common context, if available. * * @return True if the operation was successful. */ public boolean ccowJoin() { if (ccowIsActive()) { return true; } if (ccowContextManager == null && ccowEnabled) { ccowContextManager = new CCOWContextManager(); ccowContextManager.subscribe(this); ccowContextManager.run("CareWebFramework#", "", true, "*"); } if (ccowContextManager != null) { if (!ccowContextManager.isActive()) { ccowContextManager.resume(); } if (!init()) { ccowContextManager.suspend(); } } updateCCOWStatus(); return ccowIsActive(); }
/** * Initializes the managed context. * * @param item Managed context to initialize. * @return True if change was accepted. */ private boolean initItem(IManagedContext item) { try { localChangeBegin(item); if (hasSubject(item.getContextName())) { item.setContextItems(contextItems); } else { item.init(); } return StringUtils.isEmpty(localChangeEnd(item)); } catch (ContextException e) { log.error("Error initializing context.", e); return false; } }
@Test @Ignore public void marshalling() throws Exception { changePatientContext(); ContextMarshaller marshaller = contextManager.getContextMarshaller("keystore-test"); String ctx = marshaller.marshal(contextManager.getMarshaledContext()); String sig = marshaller.sign(ctx); PatientContext.changePatient(null); assertNull(PatientContext.getActivePatient()); marshaller.unmarshal(ctx, sig); Patient patient = PatientContext.getActivePatient(); assertTrue("Doe, Jane".equalsIgnoreCase(new HumanNameParser().toString(patient.getName().get(0)))); }
/** * Initializes one or all managed contexts to their default state. * * @param item Managed context to initialize or, if null, initializes all managed contexts. * @return True if the operation was successful. */ public boolean init(IManagedContext item) { contextItems.clear(); boolean result = true; if (ccowIsActive()) { contextItems.addItems(ccowContextManager.getCCOWContext()); } if (item != null) { result = initItem(item); } else { for (IManagedContext managedContext : managedContexts) { result &= initItem(managedContext); } } return result; }
/** * @see org.carewebframework.api.context.IContextManager#reset */ @Override public void reset(boolean silent, ISurveyCallback callback) { pendingStack.clear(); commitStack.clear(); SurveyResponse response = new SurveyResponse(); Iterator<IManagedContext<?>> iter = managedContexts.iterator(); reset(silent, iter, response, __ -> { boolean commit = silent || !response.rejected(); commitContexts(commit, commit); execCallback(callback, response); }); }
/** * Updates managed contexts based on the marshaledContext. * * @param marshaledContext The marshaled context to process. * @param commit If true the pending contexts are committed. * @param callback Callback to report subscriber responses. */ /*package*/void setMarshaledContext(ContextItems marshaledContext, boolean commit, ISurveyCallback callback) { ISurveyResponse response = new SurveyResponse(); Iterator<IManagedContext<?>> iter = managedContexts.iterator(); setMarshaledContext(marshaledContext, iter, response, __ -> { if (commit) { commitContexts(!response.rejected(), false); } }); }
/** * Callback to handle canceled context change request from the CCOW context manager. */ @Override public void ccowCanceled(CCOWContextManager sender) { commitContexts(false, false); }
commitContexts(accept, accept); execCallback(callback, response); });
/** * Notifies subscribers of a change in the CCOW status via a generic event. */ private void updateCCOWStatus() { if (ccowEnabled && eventManager != null) { eventManager.fireLocalEvent("CCOW", Integer.toString(getCCOWStatus().ordinal())); } }
private void initItem(Iterator<IManagedContext<?>> iter, SurveyResponse response, ISurveyCallback callback) { if (iter.hasNext()) { IManagedContext<?> managedContext = iter.next(); initItem(managedContext, aresponse -> { response.merge(aresponse); initItem(iter, response, callback); }); } else if (callback != null) { callback.response(response); } }
/** * Joins the CCOW common context, if available. */ public void ccowJoin() { if (ccowIsActive()) { return; } if (ccowContextManager == null && ccowEnabled) { ccowContextManager = new CCOWContextManager(); ccowContextManager.subscribe(this); ccowContextManager.run("CareWebFramework#", "", true, "*"); } if (ccowContextManager != null) { if (!ccowContextManager.isActive()) { ccowContextManager.resume(); } init(response -> { if (response.rejected()) { ccowContextManager.suspend(); } updateCCOWStatus(); }); } }
@Test @Ignore public void marshalling() throws Exception { changePatientContext(); ContextMarshaller marshaller = contextManager.getContextMarshaller("keystore-test"); String ctx = marshaller.marshal(contextManager.getMarshaledContext()); String sig = marshaller.sign(ctx); PatientContext.changePatient((Patient) null); assertNull(PatientContext.getActivePatient()); marshaller.unmarshal(ctx, sig); Patient patient = PatientContext.getActivePatient(); assertTrue("Doe, Jane".equalsIgnoreCase(new HumanNameParser().toString(patient.getName().get(0)))); }