CMS « Intelligrape Groovy & Grails Blogs

Archive for the ‘ CMS ’ Category

Alfresco : Render html code written in tinymce

Posted by Amit Jain on August 18th, 2009

Hi,

We were using tinymce on alfresco labs 3 stable version. Lately, client came up with the new requirement to display video’s in the page rendered from text given in tinymce. Since coupling between alfresco and current version of tinymce was so tight, we did not include the latest version of tinymce, which might have supported video’s too.

So we decided to render html given in tinymce ourselves, after loading the domain object. The Sample code is given below :

import java.util.regex.Pattern
import java.util.regex.Matcher
String sampleText = new String('''###<width>369</width><height>174</height><image>/bw/images/preview.jpg</image><url>http://www.mediacollege.com/video-gallery/testclips/barsandtone.flv</url> ###''')

sampleText = replaceHtmlTags(sampleText)

def replaceHtmlTags(def text){
try{
   Pattern pattern = Pattern.compile('''###(.*?)###'''); // searches the string wrapped up with "###"
   Matcher matcher
   def tagStr,updatedTagStr
   matcher = pattern.matcher(text);
   int count=1
   while (matcher.find()) {
       tagStr = matcher.group()
       updatedTagStr = new String(tagStr)
       updatedTagStr = updatedTagStr.replaceAll("&lt;", "<")
       updatedTagStr = updatedTagStr.replaceAll("&gt;", ">")
       updatedTagStr = updatedTagStr.replaceAll("&quot;", "\"")
       updatedTagStr = updatedTagStr.replaceAll("&amp;", "&")
       updatedTagStr = updatedTagStr.replaceAll("###", "")
       text = text.replace(tagStr, updatedTagStr)
   }
}catch(Exception e){
    println "Exception caused " + e.message
}
   return text
}

With this code in place, html code wrapped up with “###” in tinymce, can be rendered as HTML tags rather than just a string. This gives lot of power to the user, as it can be used to show videos, slides from slideshare and anything we can display using html. for example, to play youtube video on our webpage along with other content, we just need to copy the embed or object tag from youtube to our tinymce and call replaceHtmlTags function we just saw on that text and the video would be up and running.

Hope this helped.

Cheers!
~~Amit Jain~~
amit@intelligrape.com

  • Share/Bookmark
Posted in CMS

How to add content to WCM with Java Backed Webscripts?

Posted by Himanshu Seth on January 6th, 2009

Hi,

Content can be written to the wcm repository by simply using the AVMService class. The instance of this service is available through the instance of ServiceRegistry class. I’ll again point out that if you are starting with Java backed web-scripts to read/write content from/to alfresco wcm, then the example script at http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples is a good place to start. The Abstract class that it defines can be used as a base for other web-scripts. The specific method that can be used to create a node in our web project is the createFile method in the AVMService. This method returns the outputStream to the newly created object. The parameters for the createFile method are;
• Path: It is the path to the directory in which the file is to be created.
• FileName: It is the name with which the file will be created.
Once we have the output stream for the newly created file, we can just write the contents to the file using the write method (it is used to write a byte array to the stream).

Thus creating the file can be done in three (simple) steps:-
1. Create a file using the createFile method of AVMService.
2. Write the contents in the form of byte array to the output stream returned by the method called in 1.
3. Close the output stream.

Your feedback and  suggestions are welcome.

Regards
~~Himanshu Seth~~

http://www.IntelliGrape.com

  • Share/Bookmark
Posted in CMS

How to retrieve data from Alfresco WCM repository using Java backed web-scripts?

Posted by Himanshu Seth on January 6th, 2009

Retrieveing contents from Alfresco WCM repository constitutes of two things:-

1) Retrieving the contents of a particular file.
2) Retrieving a list of files from a particular location.

We were to achieve this by using Java-backed web-scripts and the example script at http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples#SimpleWebScript.java was very useful.
The abstract class “AbstractRepositoryWebScript.java” used in this example provides a base for how to work with Alfresco Java API. The ServiceRegistry instance that it gets through springs provides handle to various other services that are very useful. Lets see how we can use this bean to retrieve contents from alfresco.

1) Nodes in alfresco are identified by their alfresco node reference. This reference for nodes in alfresco wcm are very similar to their actual content path. For example: Our web projects name is “test”. In our project, the path to a file that we want to retrieve is “media/releases/content/press-release1.xml”. The alfresco node reference for this file will be
“avm://test/-1;www;avm_webapps;ROOT;media;releases;content;press-release1.xml”. The point is, that “avm://test/-1;www;avm_webapps;ROOT” is the reference to the staging sandbox of the “test” web project. It can be seen very clearly that the rest of the reference is actually the path to the file separated by ‘;’ instead of ‘/’. So when we want a reference to this file, we can create a reference by:
NodeRef nodeRef = new NodeRef("avm://test/-1;www;avm_webapps;ROOT;media;releases;content;press-release1.xml");

Now to stream back the contents of this file we can use the output function defined in the AbstractRepositorywebScript in the above mentioned example.

2) The second issue that we faced was to retrieve the list of files in a particular folder/space in our WCM repository. Lets say we want to get a list of files in the folder “media/releases.contents”. ServiceRegistry provides a lot of services which include the Nodeservice, AVMNodeService, FileFolderService, etc. The one which i found useful for retrieving a list of files from a particular folder in our WCM repository was the FileFolderService. It has a method “lookup”, which searches for files matching a particular name pattern mentioned. The parameters that this function takes are:
• Noderef to the parent node from which it will start searching.
• The string name pattern to be searched. e.g. “*.xml” or “*.*”.
• Boolean value to search in sub-folders or not.
Using these parameters, this function returns a List of FileInfo objects matching the criteria.
So if we want to get a list of all xml files in our media/releases folder but do not want the searching to be done its sub-folders, then our script will look like:

NodeRef parentNode = new NodeRef("avm://test/-1;www;avm_webapps;ROOT;media;releases");
List<FileInfo> files = this.services.getFileFolderService.lookup(parentNode,"*.xml",false);

Your feedback and  suggestions are welcome.

Regards
~~Himanshu Seth~~

http://www.IntelliGrape.com

  • Share/Bookmark
Posted in CMS