Search in sources :

Example 1 with StringHolder

use of org.eclipse.scout.rt.platform.holders.StringHolder in project scout.rt by eclipse.

the class AbstractSqlService method execCustomBindFunction.

/**
 * Custom functions that can be used in sql statements as binds or sql style independent functions
 * <p>
 * Default functions are<br>
 * ::level(permissionClass) --> int //to resolve a permissin level by executing a permission<br>
 * ::level(permissionLevel) --> int //to resolve a permissin level by its id<br>
 * ::code(codeClass or codeTypeClass) --> the ID of the code or code type<br>
 * ::text(textId) --> the text in the user sessions language
 * <p>
 * Examples:<br>
 * ::level(UpdatePersonPermission)<br>
 * ::level(UpdatePersonPermission.LEVEL_OWN)<br>
 * <br>
 * ::code(CompanyAddressCodeType.MainAddressCode)<br>
 * ::code(MainAddressCode)<br>
 * <br>
 * ::text(SalutationMr)
 * <p>
 *
 * @return a plain object value or in case of a null value preferrably a {@link IHolder} of the correct value type
 */
@ConfigOperation
@Order(40)
protected Object execCustomBindFunction(String functionName, String[] args, Object[] bindBases) {
    if ("level".equals(functionName)) {
        if (args.length != 1) {
            throw new IllegalArgumentException("expected 1 argument for function '" + functionName + "'");
        }
        String permissionClassName = args[0];
        String levelField = null;
        // eventually a level id?
        int levelDot = permissionClassName.indexOf(".LEVEL_");
        if (levelDot >= 0) {
            levelField = permissionClassName.substring(levelDot + 1);
            permissionClassName = permissionClassName.substring(0, levelDot);
        }
        Class permissionClass = loadBundleClassLenient(m_permissionNameToDescriptor, permissionClassName);
        IAccessControlService accessControlService = BEANS.get(IAccessControlService.class);
        Object ret = tryGetPermissionLevel(permissionClass, levelField, accessControlService);
        return ret != null ? ret : new LongHolder();
    } else if ("code".equals(functionName)) {
        if (args.length != 1) {
            throw new IllegalArgumentException("expected 1 argument for function '" + functionName + "'");
        }
        String codeClassName = args[0];
        Class codeClass = loadBundleClassLenient(m_codeNameToDescriptor, codeClassName);
        if (codeClass == null) {
            throw new ProcessingException("Cannot find class for code '{}", new Object[] { args[0] });
        }
        try {
            Object ret = codeClass.getField("ID").get(null);
            return ret != null ? ret : new LongHolder();
        } catch (Exception t) {
            throw new ProcessingException("ID of code '{}'", new Object[] { args[0], t });
        }
    } else if ("text".equals(functionName)) {
        if (args.length < 1) {
            throw new IllegalArgumentException("expected at least 1 argument for function '" + functionName + "'");
        }
        String ret = TEXTS.get(args[0]);
        return ret != null ? ret : new StringHolder();
    } else {
        throw new IllegalArgumentException("undefined function '" + functionName + "'");
    }
}
Also used : LongHolder(org.eclipse.scout.rt.platform.holders.LongHolder) StringHolder(org.eclipse.scout.rt.platform.holders.StringHolder) IAccessControlService(org.eclipse.scout.rt.shared.services.common.security.IAccessControlService) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) SQLException(java.sql.SQLException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) Order(org.eclipse.scout.rt.platform.Order) ConfigOperation(org.eclipse.scout.rt.platform.annotations.ConfigOperation)

Example 2 with StringHolder

use of org.eclipse.scout.rt.platform.holders.StringHolder in project scout.rt by eclipse.

the class ThreadLocalProcessorTest method test.

@Test
public void test() throws Exception {
    final StringHolder actualValue = new StringHolder();
    THREAD_LOCAL.set("ORIG");
    CallableChain<Void> callableChain = new CallableChain<>();
    callableChain.add(new ThreadLocalProcessor<>(THREAD_LOCAL, "ABC"));
    callableChain.call(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            actualValue.setValue(THREAD_LOCAL.get());
            return null;
        }
    });
    assertEquals("ABC", actualValue.getValue());
    assertEquals("ORIG", THREAD_LOCAL.get());
}
Also used : StringHolder(org.eclipse.scout.rt.platform.holders.StringHolder) CallableChain(org.eclipse.scout.rt.platform.chain.callable.CallableChain) Test(org.junit.Test)

Example 3 with StringHolder

use of org.eclipse.scout.rt.platform.holders.StringHolder in project scout.rt by eclipse.

the class JmsMomImplementorTest method testMomEnvironmentWithCustomDefaultMarshaller.

@Test
public void testMomEnvironmentWithCustomDefaultMarshaller() throws InterruptedException {
    installTestMom(JmsTestMomWithTextMarshaller.class);
    final Capturer<String> capturer1 = new Capturer<>();
    final Capturer<Object> capturer2 = new Capturer<>();
    IDestination<String> queueString = MOM.newDestination("test/mom/testPublishStringData", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    IDestination<Object> queueObject = MOM.newDestination("test/mom/testPublishObjectData", DestinationType.QUEUE, ResolveMethod.DEFINE, null);
    m_disposables.add(MOM.registerMarshaller(JmsTestMom.class, queueObject, BEANS.get(ObjectMarshaller.class)));
    MOM.publish(JmsTestMom.class, queueString, "Hello MOM!");
    MOM.publish(JmsTestMom.class, queueObject, new StringHolder("Hello MOM! (holder)"));
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queueString, new IMessageListener<String>() {

        @Override
        public void onMessage(IMessage<String> message) {
            capturer1.set(message.getTransferObject());
        }
    }));
    m_disposables.add(MOM.subscribe(JmsTestMom.class, queueObject, new IMessageListener<Object>() {

        @Override
        public void onMessage(IMessage<Object> message) {
            capturer2.set(message.getTransferObject());
        }
    }));
    // Verify
    String received1 = capturer1.get();
    Object received2 = capturer2.get();
    assertEquals("Hello MOM!", received1);
    assertEquals("Hello MOM! (holder)", Objects.toString(received2));
}
Also used : StringHolder(org.eclipse.scout.rt.platform.holders.StringHolder) IMessage(org.eclipse.scout.rt.mom.api.IMessage) IMessageListener(org.eclipse.scout.rt.mom.api.IMessageListener) Test(org.junit.Test)

Aggregations

StringHolder (org.eclipse.scout.rt.platform.holders.StringHolder)3 Test (org.junit.Test)2 SQLException (java.sql.SQLException)1 IMessage (org.eclipse.scout.rt.mom.api.IMessage)1 IMessageListener (org.eclipse.scout.rt.mom.api.IMessageListener)1 Order (org.eclipse.scout.rt.platform.Order)1 ConfigOperation (org.eclipse.scout.rt.platform.annotations.ConfigOperation)1 CallableChain (org.eclipse.scout.rt.platform.chain.callable.CallableChain)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)1 LongHolder (org.eclipse.scout.rt.platform.holders.LongHolder)1 IAccessControlService (org.eclipse.scout.rt.shared.services.common.security.IAccessControlService)1