Programování

Pozadí ubytovníčku: validace

0

Co mám: mnoho nápadů
Výstup: nejjednodušší možné řešení, kterým jsem schopen vyřešit zákazníkův problém
Co budu dělat: mluvit s potenciálními zákazníky, pivotovat, hledat nejjednodušší možnou funkcionalitu
Co pro to použiju: lean canvas (případně jiný canvas či způsob validace, pokud je vhodnější)

Minule jsem psal o něčem, co by se dalo nazvat blitím nápadů. Podstatnější než to je však validace těchto nápadů, v tom pomůže svatá trojice. Tou se rozumí trojice uživatel, uživatelův problém a moje řešení tohoto problému. Nejlepší situace je ta, když sám vidím v něčem problém a chci jej řešit (neboli sám sobě jsem zákazníkem). Potom můžu přistoupit k onomu blití nápadů z minula a mám nápadů na řešení přehršel, protže vím, o čem mluvím. Ovšem i přesto můžu být sám sobě zákazníkem jediným a to je špatně, proto přichází na řadu výše zmíněná validace.

Validace problému/řešení

Validace problému/řešení (zdroj: http://market-by-numbers.com)

Proč jsem zvýraznil spojení uživatelův problém? Protože musím vycházet z problému, který uživatele opravdu trápí, ne z problému, o kterém si myslím já, že uživatele pálí. Proto je třeba se ptát, ne někde v ústraní dumat nad možnými problémy. Je potřeba sehnat potencionální zákazníky, lidi, kteří do dané problematiky vidí a věčné skeptiky (mě :-) ) a předložit jim různé scénáře s problémovými situacemi a způsoby jejich řešení a postupně se s nimi dopracovávat k jedinému konkrétnímu výsledku, nebo klidně i několika různým důležitým cílům, z nichž vykrystalizuje výsledný produkt (lépe řečeno Minimal Viable Product).

Postupná krystalizace nápadu (kombinace problém/řešení)

Postupná krystalizace nápadu (kombinace problém/řešení) (zdroj: http://theagni.com/)

Tohle dopracovávání se k výslednému nápadu, který bude mít cenu realizovat, by mělo probíhat iterativně – tedy postupně by se měl nápad doplňovat připomínkami, validovat, doplňovat, validovat…třeba i v několika paralelních větvích, které budou validovány dále ještě jinými způsoby, aby byla vybraná ta nejlepší. Nejlepším výběrem je myšlen kompromis mezi užitnou hodnotou pro uživatele a mojí schopností tuto věc co nejrychleji realizovat. Tím myslím především finanční stránku věci, protože když je čas a peníze, realizovat se dá cokoliv :-) .

Lean canvas

Lean canvas (zdroj: http://www.ashmaurya.com/)

V hledání tohoto kompromisu pomůže nástroj zvaný lean canvas. Lépe řečeno – mnoho canvasů, protože co canvas, to kombinace uživatel/problém. Není možné napasovat stejné uživatele na různé problémy, protože je prostě nemusí vůbec pálit. Canvas má několik polí, která se vyplňují postupně a samozřejmě canvas samotný se dále vyvíjí na základě komunikace s potenciálními klienty/zákazníky, díky čemuž mohou nevyhovující canvasy zanikat i vznikat nové. Prvotní vytvoření canvasu zabere okolo 20 minut, delší koumání je ztráta času, jak píše autor této techniky, Ash Maurya, v knize Running lean. Když nevím, nevyplňuju, nebo ani vyplnit nemůžu, protože to zatím není jasné – též budeme iterovat a dopracovávat se k nejvhodnějšímu řešení. Lean canvas vychází z Business model canvasu a vhodně jej rozšiřuje pro začínající projekty, dle slov autora: Business canvas na příkladech ukazoval zajeté instituce, jako apple, nebo skype… Lean canvas se soustředí na začínající projekty a na mnoha příkladech ilustruje tuto techniku na vlastním rozjížděném projektu. Nebudu to dále rozmazávat, stojí to za přečtení – druhé vydání je k sehnání na Amazonu za nějakých $16, první vydání bylo možné předplatit v rámci psaní knihy a dostávat je na etapy (nebo zkuste napsat přímo autorovi, je to člověk otevřený ;-) ).

Pivotování

Pivotování (zdroj: http://steveblank.com/)

Ale zpátky k validacím: v této fázi se bude určitě hodně pivotovat, protože mám představu, kdo jsou mými zákazníky, oni jimi však většinou nebudou, musí sami sebe identifikovat s mým produktem. Proto se budou cíle velmi často měnit a v této fázi je to výhodné a také velice vhodné, protože nic reálného zatím neexistuje a měním pouze myšlenky a nahlížení na ně. Jen se k tomu musím přinutit, je to jako u zubaře – včasný zásah bolí mnohem méně. Časem se to nějak ustálí a najednou bude vše „nad slunce jasné“.

Teď už by mohl být pomalu čas na nákup domény, dočkali jsme se! Taky je možné začít vytvářet profil pro aplikaci na sociálních sítích, (nejenom) abychom se dostali uživatelům pod kůži a vytvořili si nějaké publikum. Ale o tom zase až příště, konečně se to stočí k techničtějším věcem, o kterých měla tato série původně celá být!

(Repost z blogu Ubytovníčku.)

Import scripts architecture, pt. II

0

First post of this series ended with code snippet, which invokates importAction itself. It’s not still the importing itself, but we are almost there. This post will be about what happenes, what hapened before and what is going to happen :-) . But first things first, let’s have a look on Factory and Proxy pattern, which are used while calling imports.

Factory pattern is based on idea instead of creating a new object with new operator, factory is asked, which returns a new instance. This is way standard Spring’s bean factory works. It may looks like following diagram (image is from oodesign.com):

Factory Pattern

Factory Pattern (click for source - oodesign.com)

To hide newly created instance behind an interface facade Proxy pattern is used. This functionality is imho basic of AOP programming, because instead of calling original object, something else may be called. Have a look on following diagram (again from oodesign.com):

Proxy Pattern

Proxy Pattern (click for source - oodesign.com)

Now it’s possible to put those patterns together and get real importAction functionality: importAction is just an interface, which hides importActionImpl newly created for every request. This can be seen on following snippet of application context configuration.

<bean id="ImportAction" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetName" value="importAction"/>
    <property name="singleton" value="false"/>
    <property name="proxyInterfaces">
        <list>
            <value>cz.shmoula.imports.ImportAction</value>
        </list>
    </property>
</bean>

<bean id="importAction" class="cz.shmoula.imports.ImportActionImpl" scope="prototype" />

As I wrote a while before – all of this is done due to posibility of calling something else. But back to implementation, for details refer to some AOP documentation. Method importAction.invocateImportScript() is a pointcut, on which two advisors are bound. Have a look at following diagram.

ImportActionAdvice

ImportActionAdvice

This configuration can be achieved by NameMatchMethodPointcutAdvisor, see following snippet for ImportContentAdvisor bean definition and updated ImportAction itself:

<bean id="importContentAdvice" class="cz.shmoula.imports.ImportContentAdvice" />

<bean id="ImportContentAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
    <property name="advice" ref="importContentAdvice"/>
    <property name="mappedNames">
        <list>
            <value>invocateImportScript</value>
        </list>
    </property>
</bean>

<bean id="ImportAction" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetName" value="importAction"/>
    <property name="singleton" value="false"/>
    <property name="proxyInterfaces">
        <list>
            <value>cz.shmoula.imports.ImportAction</value>
        </list>
    </property>
    <property name="interceptorNames">
        <list>
            <value>ImportContentAdvisor</value>
            <value>DeployContentAdvisor</value>
        </list>
    </property>
</bean>

<bean id="importActionTask" class="cz.shmoula.imports.ImportActionTask" scope="prototype">
    <property name="importAction" ref="ImportAction"/>
</bean>

Last few lines is definition of importActionTask I wrote in previous post. In this class importAction.invocateImportScript() method is called. This method has one parameter – map of properties of config file (in format <QName, Serializable>) to read them once and have them all the time and not need to call nodeService.getProperties() again and again – in advisors or importAction class. When this method is called, proxy of importAction catches this calling and calls invoke() method on ImportContentAdvice and DeployContentAdvice. Let’s have a look on invoke() method in ImportContentAdvice:

public Object invoke(MethodInvocation invocation) throws Throwable {
    Object[] objects = invocation.getArguments();
    Object result = null;
    Map<QName, Serializable> propertiesMap = __resolve_arguments_to_map_(objects);

    String lockToken = (String) propertiesMap.get(ContentModel.PROP_NODE_UUID);
    setTokenOnTarget(invocation, lockToken);

    try {
        AuthenticationUtil.setRunAsUser(AuthenticationUtil.getSystemUserName());
        misLockingService.createLock(lockToken);

        object = invocation.proceed();

        misLockingService.releaseLock(lockToken);
    } finally {
        AuthenticationUtil.clearCurrentSecurityContext();
    }

    return result;
}

All what ImportActionAdvice does can be seen on previous snippet. It locks used config file to prevent access from another thread, using node uuid as a locking token. Then it authenticates a system user (mainImportThread runs outside any security context) and invocates next chain (DeployContentAdvisor and after then invocateImportScript(), if everything goes ok). After return from invocation lock is released and security context is cleared. If anything happens inside, lock release is skipped, so some rescue mechanisms are automaticaly initiated after some time. Simple, yet effective, i hope.

Import scripts architecture

1

It has been some time since last post about import scripts from external sources to alfresco repository, I was busy with debugging and threading, but now I have some little time to write some notes on that topic again. I’d like to introduce „architecture“ of my solution, at least a first part of it, so lets start with an image.

Calling ImportAction

Calling ImportAction

MainImportingThread is Runnable implementation, which is initiated on init. Main thread sleeps some time and searches for config files in repository, while not sleep. If found some, ImportActionTask instance is created for each config in separate thread. ImportActionTask checks, if is it possible to run script described by config. If so, run script method on ImportAction is executed. That’s in few words functionality of the first part. Now in more details.

First version used quartz jobs triggered by org.alfresco.util.CronTriggerBean every 10 seconds, but that was temporarily solution, too heavy in my eyes. In second version I tried to use BatchProcessor, but spent few days without got it worked as I want :-( . Finally I used ThreadPoolTaskExecutor with combination of java.lang.Runnable in main thread and it worked like a charm! As I wrote a while ago, MainImportingThread has its own separate thread, which cyclically looks for configuration files in repository. It looks like following snippet:

public void init() {
    taskExecutor.execute(new Runnable() {
        @Override
        public void run() {
            while(keepAlive) {
                Thread.sleep(threadSleepMs);

                for(ResultSetRow row : resultSet) {
                    Object oTarget = pool.makeObject();

                    if(oTarget instanceof ImportActionTask) {
                        ImportActionTask importActionTask =  (ImportActionTask) oTarget;
                        importActionTask.setScriptRef(row.getNodeRef());
                        taskExecutor.execute(importActionTask);
                    }
                    pool.destroyObject(oTarget);
                }
            }
            taskExecutor.shutdown();
            pool.destroy();
        }
    });
}

For every row in result set PoolableObjectFactory creates a new instance of ImportActionTask and pass it to ThreadPoolTaskExecutor to execute. After all that – object is destroyed (destroyObject method is there just for sure – in time this method is called action still runs).

ImportActionTask is Runnable implementation, so every action runs its own thread. After execution isTimeToRun() method is called and action runs only when it’s its time and action is enabled, as it defined in data model illustrated on following diagram.

Config model

Config model

isTimeToRun() method uses fields importCron, importLastAction and isImportActive for its work. Its body looks similar to following simplified code snippet:

private boolean isTimeToRun() {
    if(!model.isImportActive())
        return false;

    String cronString = model.getImportCron();
    Date lastAction = model.getLastAction();

    CronExpression cronExpression = new CronExpression(cronString);
    if(cronExpression.getNextValidTimeAfter(lastAction).getTime() > System.currentTimeMillis())
        return false;

    return true;
}

Previous snippet checks cron expression against time of last successfully executed action using org.quartz.CronExpression and in case of success (ie lastAction+cron < current time) importAction itself may be triggered. But more on this next time, right now just another snippet from ImportActionTask, which calls importAction (received from ProxyFactoryBean):

public void run() {
    if(isTimeToRun) {
        try {
            importAction.invocateImportScript();
        } finally {
            importAction.clean();
        }
    }
}

Custom configs in Alfresco

0

While creating import services I want some configuration mechanism for (not just) BatchProcessor. I like Alfresco’s *-config-custom.xml files (aka Spring framework XMLConfigService), so I did simple custom config service.

Anyone, who customized Alfresco, knows format of this file (web-client-config-custom.xml). I put that structure to new file – mis-config-custom.xml:

<alfresco-config>
    <config evaluator="string-compare" condition="imports">
        <processor>
            <worker-threads-num>3</worker-threads-num>
            <batch-size-num>6</batch-size-num>
            <split-transactions>false</split-transactions>
            <logging-interval>1000</logging-interval>
        </processor>
        <token>
            <time-refresh>1000</time-refresh>
            <time-expiration>20000</time-expiration>
        </token>
    </config>
</alfresco-config>

Please note those constants are just for ilustration purposes now, more on them in scripting series. My MisConfigService extends org.springframework.extensions.config.xml.XMLConfigService, but uses nothing from that class (except init method in bootstrap), I just added some statics inside to keep things together:

public class MisConfigService extends XMLConfigService {
    public MisConfigService(ConfigSource configSource) {
        super(configSource);
    }

    public static String getChildValue(Config config, String elementId, String childName, String defaultValue) {
        ConfigElement myConfig = config.getConfigElement(elementId);
        String result = myConfig.getChildValue(childName);

        return (result==null ? defaultValue : result);
    }

    public static Integer getChildValue(Config config, String elementId, String childName, Integer defaultValue) {
        Integer result = defaultValue;
        try {
            String number = getChildValue(config, elementId, childName, defaultValue.toString());
            result = Integer.parseInt(number);
        } finally { /* O:-) */ }
        return result;
    }

        . . .
}

To instantiate this service just add bean definition with UrlConfigSource(-s), which defines path to custom xml configuration:

<bean id="misConfigService" class="cz.mis.service.MisConfigService" init-method="init">
    <constructor-arg>
        <bean class="org.springframework.extensions.config.source.UrlConfigSource">
            <constructor-arg>
                <list>
                    <value>classpath:alfresco/extension/mis-config-custom.xml</value>
                </list>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>

Now it’s possible to access those properties in code like this:

org.springframework.extensions.config.Config config = configService.getConfig(imports");
Boolean splitTransactions = MisConfigService.getChildValue(config, "processor", "split-transactions", false);

It’s also possible to access attributes of nodes in that config:

ConfigElement myConfig = config.getConfigElement("config-element");
for(ConfigElement child : myConfig.getChildren()) {
    // child.getAttributes()
    // child.getName()
    // child.getValue()
}

That’s all! And now for something completely different! I also spent some time trying to get properties files working and putting those properties into Spring configurations. There’s nice solved thread in Alfresco forums. Prefix helps:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:alfresco/extension/mis-settings.properties</value>
        </list>
    </property>
    <property name="ignoreResourceNotFound" value="true" />
    <property name="placeholderPrefix" value="${mis:" />
    <property name="placeholderSuffix" value="}" />
</bean>

<!-- and I can use stuff from properties file with prefix -->
<bean class="foo.bar.Example">
    <property name="fooAttribute">
        <value>${mis:some.sample.property}</value>
    </property>
</bean>

More on invoking scripts in Alfresco

0

In my last blogpost on this topic I wrote about invoking rhino scripts programatically from Alfresco and about passing its model into freemarker templates. After that I realized that alfresco templateService is unable to render more complex models (nested classes and collections) if called this way (please note on Alfresco webscripts invoked standard way this works correctly!). I think the problem is – no wrapper is used while passing model. I spent lot of time trying many things, read a lot of posts and topics on that, played with built-ins, trying to find that part in Alfresco source, but still no result, just exceptions. But solution is VERY SIMPLE – to write own templateService :-) . Well, maybe this post is more about Freemarker and Rhino than Alfresco, sorry about that!

So, what I want to achieve? Suppose some simple script like this one:

model.bar="foo";
model.foo = "bar";
model.obj = {q:1, w:3, e:9};

model.arr = new Array();
model.arr[0] = {a:1, b:8, c:9, d:{d:66}};
model.arr[1] = {a:2, b:7, c:10, d:{d:66}};
model.arr[2] = {a:3, b:6, c:11, d:{d:66}};
model.arr[3] = {a:4, b:5, c:12, d:{d:66}};

and a Freemarker template simple as that:

q attribute: ${obj.q}

<#list arr?keys as value>
  - ${value} = ${arr[value].a} = ${arr[value].b} = ${arr[value].c}
  <#assign obj_d = arr[value].d>
  + d: ${obj_d.d}
</#list>

Now after calling templateService.processTemplate( . . . ) the output is Exception this and Exception that… That happens, because Rhino creates NativeObjects for those objects and after passing those objects to Freemarker (through Java – without any ObjectWrapper), which doesn’t know how to handle them. The same situation happens when passing a collection with generics like <String, Object> into model – those Objects are standard Java objects and in Freemarker acts like them, they really don’t have attributes like q, a, b, etc. :-) . Similar situation can happen with collections – they needs to be wrapped too. But stop with theory, back to practise. For those interested in wrapping and so, follow Freemarker documentation on wrappers.

Before wrapping – when I printed out keys of model, there were’nt my attributes defined in Javascript. What I found inside is on following listing, note my attributes are also there – bar, foo, arr, obj:

<#assign keys = model?keys>
<#list keys as key>
 + ${key}
</#list>

(** output **)
 + getClass
 + clone
 + put
 + remove
 + get
 + equals
 + entrySet
 + class
 + hashCode
 + foo
 + keySet
 + size
 + clear
 + isEmpty
 + containsKey
 + values
 + arr
 + containsValue
 + empty
 + toString
 + putAll
 + bar
 + obj

Those properties belongs to hashMap and is possible to get rid of them using simpleMapWrapper, this technique is described well in Rizwan Ahmed post.

What about my templateService implementation? First I tried to use JSONtoFmModel and convert javascript objects to strings, but that was no way forward. That was before I discovered wrappers :-) . There is RhinoWrapper class in Freemarker and it solves all my problems! It can be used as simple as this:

Configuration cfg = new Configuration();
RhinoWrapper rhinoWrapper = new RhinoWrapper();

rhinoWrapper.setSimpleMapWrapper(true);
cfg.setObjectWrapper(rhinoWrapper);

Previous listing creates new Freemarker configuration instance and rhino wrapper instance and binds them together. With that configuration it’s possible to create template instance, which takes reader with template content, let’s read it from repository:

NodeRef nodeRef = new NodeRef("workspace://SpacesStore/56ba2237-f776-494a-939b-d259b68c021a");

ContentReader contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
InputStream inputStream = contentReader.getContentInputStream();

Template template = new Template("my_template", new InputStreamReader(inputStream), cfg);

That’s all we need to render. Rendering takes one more parameter – model map, which is HashMap defined in my previous blogpost. To keep it simple (and post a little shorter) I’m writing output to sysout:

OutputStreamWriter streamWriter = new OutputStreamWriter(java.lang.System.out);
template.process(rootMap, streamWriter);

Those few lines and everything works fine now! But I got some silver hairs with trying to solve that, now I can feel like a boss, so I achieved new „Alfresco guru badge“ :-) . Also I’m thinking about some public GIT repository with my Alfresco inventions, what you think about it?

Like an Alfresco guru!

Like an Alfresco guru!

PostScriptum: Can anyone explain me why Alfresco uses Rhino version 1.6R7 which is from year 2007? :-O

Go to Top