Here is the code..
======================================================================================================================
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.web.scripts.Cache;
import org.alfresco.web.scripts.DeclarativeWebScript;
import org.alfresco.web.scripts.Status;
import org.alfresco.web.scripts.WebScriptRequest;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.keenan.core.content.model.CustomContentModel;
import com.keenan.utils.KeenanConstants;
import com.keenan.utils.KeenanUtils;
/**
*
* @author Cignex
* @Package com.keenan.script.pncbridge
* This class is used for creating XML responce of Specific Newsletter from UUID
*/
public class GetNewsLetter extends DeclarativeWebScript
{
Locale currentLocale = Locale.getDefault();
KeenanUtils keenanUtils=new KeenanUtils();
private final Logger logger = Logger.getLogger(GetNewsLetter.class);
private ServiceRegistry serviceRegistry;
/**
* This Method will take request parameters ,search content and create XML responce as per UUID.
*/
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest _request, final Status _status, final Cache _cache)
{
final String _rootPath = KeenanConstants.getPropertyValue(KeenanConstants.NEWSLETTER_PATH);
final Map<String, Object> _returnMap = new HashMap<String, Object>();
String _content =null;
StringBuffer _errorContent = null;
AuthenticationUtil.setRunAsUserSystem();
final String _uuid = _request.getParameter("uuid");
final NodeRef _noderef = keenanUtils.getNodeByUuid(_uuid,_rootPath,this.serviceRegistry);
if (_noderef != null)
{
_content = parseNodeRef(_noderef);
}
else
{
_errorContent = new StringBuffer();
_errorContent.append(KeenanConstants.getPropertyValue(KeenanConstants.NEWSLETTER_ERRORTAG));
_errorContent.append(KeenanConstants.getPropertyValue(KeenanConstants.ERROR_MESSAGE));
_errorContent.append(keenanUtils.endTag(KeenanConstants.getPropertyValue(KeenanConstants.NEWSLETTER_ERRORTAG)));
_content=_errorContent.toString();
}
_returnMap.put(KeenanConstants.getPropertyValue(KeenanConstants.CONTENT_STRING_KEY), _content);
AuthenticationUtil.clearCurrentSecurityContext();
return _returnMap;
}
// Getter of Service Registry
public ServiceRegistry getServiceRegistry()
{
return serviceRegistry;
}
/**
*
* @param _contentNode
* @return
* This method will create the XML responce of particular Newsletter from UUID.
*/
private String parseNodeRef(final NodeRef _contentNode)
{
Map<QName, Serializable> _metadataMap;
_metadataMap = getServiceRegistry().getNodeService().getProperties(_contentNode);
String _content =null;
StringWriter _stringWriter=null;
Document _document, _doc = null;
try
{
_document = keenanUtils.parse(_contentNode);
final DocumentBuilderFactory _dbfacxml = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
docBuilder = _dbfacxml.newDocumentBuilder();
_doc = docBuilder.newDocument();
// Xml generation
final Element root = _doc.createElement(KeenanConstants.getPropertyValue(KeenanConstants.ELEMENT_NEWSLETTER));
_doc.appendChild(root);
// Get uuid from metadata map
final String _uuid = _metadataMap.get(CustomContentModel.PROPERTY_CONTENT_UUID).toString();
final Element _uuidChild = _doc.createElement( KeenanConstants.getPropertyValue(KeenanConstants.ELEMENT_UUID));
_uuidChild.setTextContent(_uuid);
root.appendChild(_uuidChild);
// Get other parameters from generated xml file of newsletter form
final String title = _document.getElementsByTagName( KeenanConstants.getPropertyValue(KeenanConstants.TAG_TITLE)).item(0).getFirstChild().getNodeValue()!=null?_document.getElementsByTagName(KeenanConstants.getPropertyValue(KeenanConstants.TAG_TITLE)).item(0).getFirstChild().getNodeValue():"";
final Element _titleChild = _doc.createElement(KeenanConstants.getPropertyValue(KeenanConstants.getPropertyValue(KeenanConstants.ELEMENT__TITLE)));
_titleChild.setTextContent(title);
root.appendChild(_titleChild);
final Element _publisheddateChild = _doc.createElement(KeenanConstants.getPropertyValue(KeenanConstants.ELEMENT_PUBLISH_DATE));
final String _publishDate = _metadataMap.get(CustomContentModel.PROPERTY_CONTENT_DATE).toString();
_publisheddateChild.setTextContent(keenanUtils.changeDateFormat("yyyy-MM-dd", "E MMM dd hh:mm:ss Z yyyy", _publishDate));
root.appendChild(_publisheddateChild);
final Element _htmlcontentChild = _doc.createElement(KeenanConstants.getPropertyValue(KeenanConstants.ELEMENT_HTMLCONTENT));
final String _htmlcontent = _document.getElementsByTagName(KeenanConstants.getPropertyValue(KeenanConstants.TAG_BODY)).item(0).getFirstChild().getNodeValue()!=null?"<![CDATA[" + _document.getElementsByTagName(KeenanConstants.getPropertyValue(KeenanConstants.TAG_BODY)).item(0).getFirstChild().getNodeValue() + "]]>":"";
_htmlcontentChild.setTextContent(_htmlcontent);
root.appendChild(_htmlcontentChild);
// Transformation to string
final TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans;
trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, KeenanConstants.getPropertyValue(KeenanConstants.YES));
trans.setOutputProperty(OutputKeys.INDENT, KeenanConstants.getPropertyValue(KeenanConstants.YES));
_stringWriter = new StringWriter();
final StreamResult _streamResult = new StreamResult(_stringWriter);
final DOMSource _domSource = new DOMSource(_doc);
trans.transform(_domSource, _streamResult);
_content = _stringWriter.toString();
} catch (final TransformerException e)
{
logger.error(e.getMessage());
} catch (ParserConfigurationException e)
{
logger.error(e.getMessage());
} finally
{
try {
if(_stringWriter!=null)
{
_stringWriter.close();
}
} catch (IOException e) {
logger.error(e.getMessage());
}
}
return _content;
}
// Setter of Service Registry
public void setServiceRegistry(final ServiceRegistry serviceRegistry)
{
this.serviceRegistry = serviceRegistry;
}
}