This blog describes how to create e-mail template in Alfresco and use it for sending e-mail.
Template
E-mail templates are kept in folder Company Home/Data Dictionary/Email Templates, but in general they can be kept in any folder. Let’s assume, that in our template we want to use some variables, which values will be passed during invocation of e-mail action. Our template is saved in Company Home/Data Dictionary/Email Templates/sample_template.ftl and is defined as follows:
Variable ‘firstName’ should be replaced with the name of user the message is send to.
E-mail send action
We will invoke e-mail sending action using Alfresco JavaScript API:
varmail=actions.create("mail");// List of comma separated user names e-mail should be send tomail.parameters.to_many=admin;// Subject of e-mailmail.parameters.subject="Sample e-mail";// Map of variables to be used in the templatevarmap=newObject();map["firstName"]=person.properties["cm:firstName"];// Path to template to be used in e-mailmail.parameters.template=companyhome.childByNamePath("Data Dictionary/Email Templates/sample_template.ftl");// Map of variables to be used in the templatemail.parameters.template_model=map;// Execute e-mail send actionmail.execute(companyhome);
See the screencast below for the description of new moosh feature – code generation. You can download moosh from Moodle plugins, also see the documentation.
Imagine that you have few developers working on Java project. Code of the project is committed to GIT repository and there are ANT scripts implemented to facilitate builds and deployments of the system as well as additional activities that might be useful. The developers using this infrastructure push code changes to GIT repository and automatic build and deployment of the latest code version should be automatically deployed on Tomcat server. This blog describes simple automatic build system that users GIT hooks to invoke appropriate ANT tasks depending on files that were changed in the system.
Users
There are two users that are crucial to run the automatic builds:
git – this user is owner of GIT repository and the commits are made as git user
tomcat – this user should run Tomcat
To allow the deployments, which involve stopping and starting Tomcat server, git user should be able to run tomcat as tomcat user. This functionality can be enabled in /etc/sudoers file.
Run:
sudo visudo
and add the following line
git ALL = (tomcat) NOPASSWD: ALL
To run a command as Tomcat user type
sudo -u tomcat {command name}
If you set some variables you might want not to reset them once you run sudo command. To do so add variable names to sudoers file
Defaults env_keep += "JAVA_HOME"
Project structure
Let’s assume that our project has the following structure:
project – source of the project to be compiled and deployed
src – source code
configuration – Tomcat configuration files
git – GIT configuration files
post-receive – script to be invoked after push is made to GIT repository
build.xml – ant build script
Ant script
Let’s assume that ant script has following targets defined:
build-deploy – build the project and deploy it on Tomcat server
Depending on code changes the following targets should be run:
on change in project/src folder build-deploy tasks should run
on change in configuration folder deploy-configuration task should run
Git repository
Hooks
Git allows to run actions on several events, which mechanism is implemented using hooks. There are several hooks defined in git. Some of them are client side and some of them are server side. More information regarding hooks can be found here
Bare git repository is located on the server. Hooks are located in {git repository path}/hooks/. We are going to use server-side post-receive hook to make automatic builds. The post-receive hook runs after the entire process is completed and can be used to update other services or notify users. It takes a list of references that are being pushed from stdin.
The hooks will run as user which logs into GIT therefore it is important to ensure that tasks can be run as this user or user can run tasks as other user.
Configuration
To use ant build file (build.xml) and post-receive script, the checkout of the repository has to be done on the same server:
git clone {local path to repository}
The checkout should be done as tomcat user.
Each time push will be made to the repository, the checkout repository should be updated (git pull) and then the files from the repository could be used for builds. Having both files in the repository (build.xml and post-receive) allows dynamic configuration changes and flexible build system without a need to make any changes on the server.
post-receive hook configuration
We can invoke post-receive script which is in our git repository by invoking the script from post-receive hook in GIT repository. To do so post-receive file should be created in {git repository path}/hooks/ and made executable. The file, which is in fact bash script, should include the following commands:
#!/bin/shTEMP_GIT_FILE="/tmp/git.txt"# Path to cloned version of git repositoryGIT_CLONE="/opt/git-clone"cd"$GIT_CLONE"# Update cloned version of git repository as tomcat usersudo-u tomcat git pull > /dev/null
rm$TEMP_GIT_FILEwhile read line
do
echo$line >>$TEMP_GIT_FILEdone# Run post-receive script located in git repositorycat$TEMP_GIT_FILE | ./git/post-receive
Automatic build script – post-receive
The following listing presents automatic build script. The script is located in GIT repository in git folder, so whenever it is changed the changes are automatically applied. The comments regarding parts of the script are within the code.
#!/bin/sh# Log all the messages to the fileexec >> /opt/logs/git.log
# Path variablesTOMCAT="/opt/tomcat"GIT="/opt/git"GIT_CLONE="/opt/git-clone"# Export variablesexport JAVA_HOME="/opt/java/jdk1.7.0_07"export PATH="$PATH":"$JAVA_HOME/bin"export ANT_HOME="/opt/ant/apache-ant-1.8.4"export PATH="$PATH":"$ANT_HOME/bin"export TOMCAT_HOME=$TOMCAT# Global variables to track code changesconfiguration_change_global=1
code_change_global=1
echo"***********************************************************"echo"BUILD"echo$(date)echo"***********************************************************"cd"$GIT"# Variables given while read oldrev newrev refname
do# Commitcommit=$(git show $newrev | grep'commit')# Date of commitdate=$(git show $newrev | grep'Date:')# Author of commitauthor=$(git show $newrev | grep'Author:')echo"-----------------------------------------------------------"echo"$commit"echo"$date"echo"$author"echo"-----------------------------------------------------------"# Code changes since last commit
git diff --name-only$newrev$oldrev | grep'project/src' > /dev/null
code_change=$?
git diff --name-only$newrev$oldrev | grep'configuration' > /dev/null
configuration_change=$?if[$code_change-eq 0 ]then
code_change_global=0
echo"Source code changed"fi
if[$configuration_change-eq 0 ]then
configuration_change_global=0
echo"Configuration changed"fi
done# Stop Tomcat cd"$TOMCAT/bin"sudo-u tomcat ./shutdown.sh
sleep 15
pid=$(sudo-u tomcat ps aux | grep"tomca[t]" | awk'{print $2}')if[-n"$pid"]then
sudo-u tomcat kill-9$pidsleep 5
fi
echo""cd"$GIT_CLONE"sudo-u tomcat git pull > /dev/null
echo"***********************************************************"if[$code_change_global-eq 0 ]then
echo""echo"COMPILE AND DEPLOY CODE"sudo-u tomcat ant build-deploy
success=$?if[$?-eq 0 ]then
echo"SUCCESSFUL"else
echo"FAILED"fi
echo"-----------------------------------------------------------"fi
if[$configuration_change_global-eq 0 ]then
echo""echo"DEPLOY CONFIGURATION"sudo-u tomcat ant deploy-configuration
success=$?if[$?-eq 0 ]then
echo"SUCCESSFUL"else
echo"FAILED"fi
echo"-----------------------------------------------------------"fi
echo""#Start Tomcat cd"$TOMCAT/bin"sudo-u tomcat ./startup.sh
Bash and zsh both have a very neat feature for expanding a single word into more: brace expansion.
For the simplest example, imagine you need to move file.old as file.new. Normally you type:
$ mv file.old file.new
Brace expansion can save you a little bit of typing:
$ mv file.{old,new}
The command actually executed will be identical to the previous one, “file.{old,new}” gets converted into “file.old file.new”.
You can also provide a number range inside the brace:
Only a zsh feature is padding. You can add leading zeros to generated number to make them all the same (character) size, by adding zero(s) to the third parameter:
I needed to locate current user’s home directory, to find and parse a config file like ~/config.ini. The current user is a user that is currently running a PHP CLI script. I have found a neat solution in drush, ready to copy & paste into your function:
// getenv('HOME') isn't set on windows and generates a Notice.$home=getenv('HOME');if(empty($home)){if(!empty($_SERVER['HOMEDRIVE'])&&!empty($_SERVER['HOMEPATH'])){// home on windows$home=$_SERVER['HOMEDRIVE'].$_SERVER['HOMEPATH'];}}returnempty($home)?NULL:$home;
Normally, when you want to redirect (standard) output of a script to a file, you run it with a redirection, like:
$ ./script.sh > out.log
However – you can also accomplish the same from inside the script by using exec. The following will write “Test” and then current time into /tmp/out.log”
I try to keep the same .zshrc dotfile on all the machines I use. I store the “master” copy in SVN, so any improvements I make can easily go to other computers.
However, some parts of zshrc configuration only make sense for particular machine. To keep one common master copy but also allow for some “local” configuration, you can simply use an include in your .zshrc like this:
solr can be easily extended to handle binary files and extract the information from them. Apache Tika library is used for the file analysis.
This can be set up in solr by using Extracting Request Handler that is already set up in solrconfig.xml. All we need to do is to add extra libraries. Once you have your solr set up, copy:
contrib/extraction/lib/* (from the downloaded solr package) into /var/lib/tomcat6/webapps/solr/WEB-INF/lib
solr/apache-solr-4.0.0-BETA/dist/apache-solr-cell-4.0.0-BETA.jar into /var/lib/tomcat6/webapps/solr/WEB-INF/lib
Restart tomcat and index sample document (I’m using test.pdf that I have in the same, current directory). Handler is available at update/extract:
I had a problem when I created document/folder name in Alfresco Share that included Polish characters, e.g., ą, ę, ł, ż, ź. The Polish characters at first were rendered correctly but in the database (MySQL) they were saved wrong, which means that ? character was put instead of Polish letters. To make it more confusing the names were rendered wrong not necessary straight away but for example after server restart. Such behaviour implies that there had to be a communication issue between Alfresco and database and the data were saved using wrong encoding in the database. Alfresco application had file names saved in cache so it did not use the database to obtain their names. When server was restarted the data were read from the database and ? were shown. Unfortunately having ? characters caused problems to read for example folder content.
The solution to this issue was to change default encoding in the database to UTF-8. In MySQL it can be done in configuration file (/etc/mysql/my.cnf on Ubuntu). The following lines should be added to the appropriate sections in configuration file.
Update on 2012.09.20: updated for Solr 4.0-BETA (from ALPHA, thanks for the comment Dorthe).
Update on 2013.07.09: updated for Solr 4.3.1
Update on 2013.07.28: the guide works with Solr 4.4, Ubuntu server 13.04 and tomcat7, just replace tomcat6 with tomcat7 and /var/lib/tomcat6/shared with /var/lib/tomcat7/lib
This short guide will describe how to install solr 4 on Ubuntu server. The versions I’m using are: Ubuntu Server 12.04 and Apache Solr 4.3.1. I will also show how to test the installation & perform a sample indexing and query.
Installation on tomcat Ubuntu 12.04 LTS
1. Install packages
apt-get install tomcat6 curl
2. Download solr 4 from http://lucene.apache.org/solr (at the time of writing it was solr-4.3.1.tgz)
3. Choose directory for solr – know as SOLR_HOME. I’ve chosen to put it into /opt/solr so my SOLR_HOME=/opt/solr. Replace /opt/solr with your own location if you want a different one.
4. Extract downloaded files anywhere and copy following to your $SOLR_HOME and to your tomcat installation:
copy example/solr/* to /opt/solr
copy example/webapps/solr.war to /opt/solr
copy example/lib/ext/* to /var/lib/tomcat6/shared
5. Edit dataDir in solr configuration file /opt/solr/collection1/conf/solrconfig.xml:
solr installation files come with sample schema.xml (we’ve already copied it into our $SOLR_HOME) and some .xml files with sample data we can import. We will use one of them to test if UTF-8 encoding is working as expected.
1. Go to the directory with extracted solr installation files and import utf8-example.xml using curl