Usecase:- When client says he will upload a csv with contents in it. For every upload a sitearea has to be created and contents need to be placed there. Then other users will go through the approval process.
Step1 :- We need to create a custom action and apply it to publish action(Before entering the stage).Create a dynamic web project and place below plugin.xml in it.
<?xml version="1.0" encoding="UTF-8"?>
<plugin id="com.Poc.workflow.CustomCopyImageWorkflow" name="Custom Copy Image Action Factory" version="1.0.0" provider-name="IBM">
<extension-point id= "CustomCopyActionFactory" name="CustomCopyActionFactory" />
<extension point="com.ibm.workplace.wcm.api.CustomWorkflowActionFactory" id= "SSPCopyImageActionFactory" >
<provider class= "com.Poc.workflow.factory.CustomFactory"/>
</extension>
</plugin>
Step 2 :- First create a class implementing CustomWorkflowActionFactory so that you can control the workflow process.
package com.Poc.workflow.factory;
import java.util.Locale;
import com.Poc.workflow.action.CreatingContentsCustomWorkflowAction;
import com.ibm.workplace.wcm.api.Document;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowAction;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowActionFactory;
public class CustomFactory implements CustomWorkflowActionFactory {
@Override
public CustomWorkflowAction getAction(String arg0, Document arg1) {
// TODO Auto-generated method stub
return new CreatingContentsCustomWorkflowAction();
}
@Override
public String getActionDescription(Locale arg0, String arg1) {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
@Override
public String[] getActionNames() {
// TODO Auto-generated method stub
String names[] ={"WorkFlowToCreateContentItem"};
return names;
}
@Override
public String getActionTitle(Locale arg0, String arg1) {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
@Override
public String getName() {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
@Override
public String getTitle(Locale arg0) {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
}
Step3 :- Now create your custom workflow by implementing CustomWorkflowAction
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import com.Poc.workflow.util.CsvParser;
import com.Poc.workflow.util.TimeStampUtil;
import com.ibm.workplace.wcm.api.ChildPosition;
import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.ContentComponent;
import com.ibm.workplace.wcm.api.Document;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.FileComponent;
import com.ibm.workplace.wcm.api.NumericComponent;
import com.ibm.workplace.wcm.api.Repository;
import com.ibm.workplace.wcm.api.SiteArea;
import com.ibm.workplace.wcm.api.TextComponent;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.WebContentCustomWorkflowService;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowAction;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowActionResult;
import com.ibm.workplace.wcm.api.custom.Directive;
import com.ibm.workplace.wcm.api.custom.Directives;
public class CreatingContentsCustomWorkflowAction implements
CustomWorkflowAction {
Logger logger = Logger.getLogger(CreatingContentsCustomWorkflowAction.class.getName());
WebContentCustomWorkflowService webContentCustomWorkflowService = null;
InitialContext initContext = null;
CustomWorkflowActionResult result = null;
Directive directive = null;
String message = null;
public static int counter =0;
@Override
public CustomWorkflowActionResult execute(Document arg0) {
Repository repository = null;
List<String[]> dataList = null;
Workspace workspace = null;
//Retrieve the csv and then create a content
if (arg0 instanceof Content) {
directive = Directives.CONTINUE;
Content content = (Content)arg0;
try{
repository = WCM_API.getRepository();
// get workspace for current user
workspace = repository.getWorkspace();
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("TCP_DesignLibrary"));
//Retrieve the csv details
directive = Directives.CONTINUE;
ContentComponent cc = (ContentComponent)content.getComponent("UploadCSV");
if(cc instanceof FileComponent){
FileComponent fc = (FileComponent)cc;
InputStream inputStream = fc.getFileStream();
CsvParser csvParser = new CsvParser();
csvParser.parseFile(inputStream);
dataList =csvParser.getRows();
}
}catch(Exception exp){
exp.printStackTrace();
logger.info("Exception while retrieving image");
}
// retrieve repository
try{
// define authoring template
String authoringTemplateName = new String("TranferDirection_csvEntity_AT");
// define parent sitearea
String parentSiteAreaName = new String("TranferDirection_Row_SA");
// define workflow
String workflowName = new String("TCP_workflow");
DocumentId authoringTemplateId = null;
DocumentId parentSiteAreaId = null;
DocumentId ATSA_id = null;
DocumentId childSiteAreaId = null;
DocumentId siblingId = null;
DocumentId siteAreasiblingId = null;
DocumentId workflowId = null;
// find authoring template by name
DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);
if (authoringTemplateIterator.hasNext()){
authoringTemplateId = authoringTemplateIterator.nextId();
logger.info("Authoring Template found : " + authoringTemplateName + "<br>");
}else {
logger.info("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}
// Top parent site-area by name
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.Site,parentSiteAreaName);
if (siteAreaIterator.hasNext()){
parentSiteAreaId = siteAreaIterator.nextId();
logger.info("Sitearea Found : " + parentSiteAreaName + "<br>");
}else {
logger.info("Could not find Sitearea : " + parentSiteAreaName + "<br>");
}
// find sitearea by name
DocumentIdIterator siteAreaIterator2 = workspace.findByName(DocumentTypes.SiteAreaTemplate,"ATSA_TranferDirectionTemplate");
if (siteAreaIterator2.hasNext()){
ATSA_id = siteAreaIterator2.nextId();
logger.info("Sitearea Found : " + parentSiteAreaName + "<br>");
}else {
logger.info("Could not find Sitearea : " + parentSiteAreaName + "<br>");
}
// create the sub-site area
SiteArea ss = workspace.createSiteArea(ATSA_id,parentSiteAreaId,null,ChildPosition.END);
String childSiteAreaName = "Csv"+Integer.toString(counter);
ss.setName(childSiteAreaName);
ss.setTitle(childSiteAreaName);
ContentComponent cc2 = (ContentComponent)ss.getComponent("TransferId");
if(cc2 instanceof TextComponent){
TextComponent tc = (TextComponent)cc2;
tc.setText(TimeStampUtil.timeStamp());
ss.setComponent("TransferId", (ContentComponent)tc);
}
ContentComponent cc3 = (ContentComponent)ss.getComponent("Status");
if(cc2 instanceof TextComponent){
TextComponent tc = (TextComponent)cc3;
tc.setText("Pending Store Ops");
ss.setComponent("Status", (ContentComponent)tc);
}
String[] saveMessage = workspace.save((Document)ss);
// find sub sitearea by name
DocumentIdIterator siteAreaIterator3 = workspace.findByName(DocumentTypes.SiteArea,childSiteAreaName);
if (siteAreaIterator3.hasNext()){
childSiteAreaId = siteAreaIterator3.nextId();
logger.info("Sitearea Found : " + childSiteAreaId + "<br>");
}else {
logger.info("Could not find Sitearea : " + childSiteAreaName + "<br>");
}
// find workflow by name
DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow,workflowName);
if (workflowIterator.hasNext()){
workflowId = workflowIterator.nextId();
logger.info("Workflow found *: " + workflowName + "<br>");
}else {
logger.info("Could not find Workflow: " + workflowName + "<br>");
}
if((authoringTemplateId!=null) && (parentSiteAreaId!=null) && (workflowId!=null)){
for (String[] strings : dataList) {
// create new content
counter=counter+1;
Content newContent = workspace.createContent(authoringTemplateId,childSiteAreaId,siblingId,ChildPosition.END);
newContent.setName("TransferDirection_Content"+Integer.toString(counter));
newContent.setTitle("NewContent_csv_row"+Integer.toString(counter));
newContent.setDescription("New Content created for individual css");
newContent.setWorkflowId(workflowId);
ContentComponent contentComponent = newContent.getComponent("Sending");
if(contentComponent instanceof TextComponent){
TextComponent tc = (TextComponent)contentComponent;
logger.info("First element :-"+tc.getName());
tc.setText(strings[0]);
logger.info("First element content :-"+tc.getText());
newContent.setComponent("Sending", (ContentComponent)tc);
}
ContentComponent contentComponent2 = newContent.getComponent("Recieving");
if(contentComponent2 instanceof TextComponent){
TextComponent tc2 = (TextComponent)contentComponent2;
logger.info("Second element :-"+tc2.getName());
tc2.setText(strings[1]);
logger.info("Second element content :-"+tc2.getText());
newContent.setComponent("Recieving", (ContentComponent)tc2);
}
ContentComponent contentComponent3 = newContent.getComponent("Qty");
if(contentComponent3 instanceof NumericComponent){
NumericComponent tc3 = (NumericComponent)contentComponent3;
logger.info("Third element :-"+tc3.getName());
tc3.setNumber(Integer.parseInt(strings[2]));
logger.info("Third element content :-"+tc3.getNumber());
newContent.setComponent("Qty", (ContentComponent)tc3);
}
String[] saveMessage2 = workspace.save((Document)newContent);
logger.info("Save message :- "+saveMessage2.toString());
if (saveMessage2.length==0) {
logger.info (" <BR> Created new Content under " + childSiteAreaName );
}
}
}else {
logger.info ("Could not create new content");
}
}catch(Exception e){
e.printStackTrace();
}
}
//Logic whether to navigate to other stage or not
try {
initContext = new InitialContext();
webContentCustomWorkflowService = (WebContentCustomWorkflowService) initContext.lookup("portal:service/wcm/WebContentCustomWorkflowService");
result = webContentCustomWorkflowService.createResult(directive,message);
logger.info("Getting base wcm service");
} catch (Exception ex) {
message = "An exception has occured in ex " + ex.getMessage();
directive = Directives.ROLLBACK_DOCUMENT;
ex.printStackTrace();
}
return result;
}
@Override
public Date getExecuteDate(Document arg0) {
// TODO Auto-generated method stub
return DATE_EXECUTE_NOW;
}
}
Step4 :- Create all utility classes needed for it
package com.Poc.workflow.util;
import java.io.BufferedReader;
public class CsvParser {
private int rowCount; // Number of rows (excluding header row)
private int fieldCount; // Number of fields in each row
private String[] headers; // Field names (assumed to be in first row)
private List<String[]> rows = new ArrayList<String[]>(); // The data
public CsvParser() {
// Must have a no-argument constructor as a bean.
}
public void parseFile(InputStream inputStream) {
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
} catch (Exception e) {
e.printStackTrace();
return;
}
String line;
boolean firstRow = true;
try {
while ((line = br.readLine()) != null) {
//Parsing Csv
if (firstRow) {
headers = tokenize(line);
setFieldCount(headers.length);
firstRow = false;
}else{
String[] row = tokenize(line);
rows.add(row);
rowCount++;
}
}
} catch (IOException e1) {
e1.printStackTrace();
return;
}
}
protected String[] tokenize(String line) {
StringTokenizer st = new StringTokenizer(line, ",");
int tokenCount = st.countTokens();
String[] record = new String[tokenCount];
for (int i = 0; i < tokenCount; i++) {
record[i] = (String) st.nextToken();
}
return record;
}
public int getFieldCount() {
return fieldCount;
}
public void setFieldCount(int fieldCount) {
this.fieldCount = fieldCount;
}
public String[] getHeaders() {
return headers;
}
public void setHeaders(String[] headers) {
this.headers = headers;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Headers: ");
addStringArray(headers, sb);
Iterator it = rows.iterator();
int rowCount = 0;
while (it.hasNext()) {
rowCount++;
sb.append("Row " + rowCount + ": ");
String[] row = (String []) it.next();
addStringArray(row, sb);
}
return sb.toString();
}
protected StringBuffer addStringArray(String[] array, StringBuffer buffer) {
for (int i = 0; i < array.length; i++) {
buffer.append(array[i] + ",");
}
buffer.append("\n");
return buffer;
}
}
package com.Poc.workflow.util;
import java.sql.Timestamp;
public class TimeStampUtil
{
public static String timeStamp()
{
java.util.Date date= new java.util.Date();
Timestamp tm = new Timestamp(date.getTime());
System.out.println(tm.toString());
return tm.toString();
}
}
Step 5 :- Deploy this war file and go to web content management and map custom action. Refere to my previous custom email action blog for help in mapping actions.
Step1 :- We need to create a custom action and apply it to publish action(Before entering the stage).Create a dynamic web project and place below plugin.xml in it.
<?xml version="1.0" encoding="UTF-8"?>
<plugin id="com.Poc.workflow.CustomCopyImageWorkflow" name="Custom Copy Image Action Factory" version="1.0.0" provider-name="IBM">
<extension-point id= "CustomCopyActionFactory" name="CustomCopyActionFactory" />
<extension point="com.ibm.workplace.wcm.api.CustomWorkflowActionFactory" id= "SSPCopyImageActionFactory" >
<provider class= "com.Poc.workflow.factory.CustomFactory"/>
</extension>
</plugin>
Step 2 :- First create a class implementing CustomWorkflowActionFactory so that you can control the workflow process.
package com.Poc.workflow.factory;
import java.util.Locale;
import com.Poc.workflow.action.CreatingContentsCustomWorkflowAction;
import com.ibm.workplace.wcm.api.Document;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowAction;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowActionFactory;
public class CustomFactory implements CustomWorkflowActionFactory {
@Override
public CustomWorkflowAction getAction(String arg0, Document arg1) {
// TODO Auto-generated method stub
return new CreatingContentsCustomWorkflowAction();
}
@Override
public String getActionDescription(Locale arg0, String arg1) {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
@Override
public String[] getActionNames() {
// TODO Auto-generated method stub
String names[] ={"WorkFlowToCreateContentItem"};
return names;
}
@Override
public String getActionTitle(Locale arg0, String arg1) {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
@Override
public String getName() {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
@Override
public String getTitle(Locale arg0) {
// TODO Auto-generated method stub
return "WorkFlowToCreateContentItem";
}
}
Step3 :- Now create your custom workflow by implementing CustomWorkflowAction
CSV PARSER
package com.Poc.workflow.action;import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import com.Poc.workflow.util.CsvParser;
import com.Poc.workflow.util.TimeStampUtil;
import com.ibm.workplace.wcm.api.ChildPosition;
import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.ContentComponent;
import com.ibm.workplace.wcm.api.Document;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.FileComponent;
import com.ibm.workplace.wcm.api.NumericComponent;
import com.ibm.workplace.wcm.api.Repository;
import com.ibm.workplace.wcm.api.SiteArea;
import com.ibm.workplace.wcm.api.TextComponent;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.WebContentCustomWorkflowService;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowAction;
import com.ibm.workplace.wcm.api.custom.CustomWorkflowActionResult;
import com.ibm.workplace.wcm.api.custom.Directive;
import com.ibm.workplace.wcm.api.custom.Directives;
public class CreatingContentsCustomWorkflowAction implements
CustomWorkflowAction {
Logger logger = Logger.getLogger(CreatingContentsCustomWorkflowAction.class.getName());
WebContentCustomWorkflowService webContentCustomWorkflowService = null;
InitialContext initContext = null;
CustomWorkflowActionResult result = null;
Directive directive = null;
String message = null;
public static int counter =0;
@Override
public CustomWorkflowActionResult execute(Document arg0) {
Repository repository = null;
List<String[]> dataList = null;
Workspace workspace = null;
//Retrieve the csv and then create a content
if (arg0 instanceof Content) {
directive = Directives.CONTINUE;
Content content = (Content)arg0;
try{
repository = WCM_API.getRepository();
// get workspace for current user
workspace = repository.getWorkspace();
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("TCP_DesignLibrary"));
//Retrieve the csv details
directive = Directives.CONTINUE;
ContentComponent cc = (ContentComponent)content.getComponent("UploadCSV");
if(cc instanceof FileComponent){
FileComponent fc = (FileComponent)cc;
InputStream inputStream = fc.getFileStream();
CsvParser csvParser = new CsvParser();
csvParser.parseFile(inputStream);
dataList =csvParser.getRows();
}
}catch(Exception exp){
exp.printStackTrace();
logger.info("Exception while retrieving image");
}
// retrieve repository
try{
// define authoring template
String authoringTemplateName = new String("TranferDirection_csvEntity_AT");
// define parent sitearea
String parentSiteAreaName = new String("TranferDirection_Row_SA");
// define workflow
String workflowName = new String("TCP_workflow");
DocumentId authoringTemplateId = null;
DocumentId parentSiteAreaId = null;
DocumentId ATSA_id = null;
DocumentId childSiteAreaId = null;
DocumentId siblingId = null;
DocumentId siteAreasiblingId = null;
DocumentId workflowId = null;
// find authoring template by name
DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);
if (authoringTemplateIterator.hasNext()){
authoringTemplateId = authoringTemplateIterator.nextId();
logger.info("Authoring Template found : " + authoringTemplateName + "<br>");
}else {
logger.info("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}
// Top parent site-area by name
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.Site,parentSiteAreaName);
if (siteAreaIterator.hasNext()){
parentSiteAreaId = siteAreaIterator.nextId();
logger.info("Sitearea Found : " + parentSiteAreaName + "<br>");
}else {
logger.info("Could not find Sitearea : " + parentSiteAreaName + "<br>");
}
// find sitearea by name
DocumentIdIterator siteAreaIterator2 = workspace.findByName(DocumentTypes.SiteAreaTemplate,"ATSA_TranferDirectionTemplate");
if (siteAreaIterator2.hasNext()){
ATSA_id = siteAreaIterator2.nextId();
logger.info("Sitearea Found : " + parentSiteAreaName + "<br>");
}else {
logger.info("Could not find Sitearea : " + parentSiteAreaName + "<br>");
}
// create the sub-site area
SiteArea ss = workspace.createSiteArea(ATSA_id,parentSiteAreaId,null,ChildPosition.END);
String childSiteAreaName = "Csv"+Integer.toString(counter);
ss.setName(childSiteAreaName);
ss.setTitle(childSiteAreaName);
ContentComponent cc2 = (ContentComponent)ss.getComponent("TransferId");
if(cc2 instanceof TextComponent){
TextComponent tc = (TextComponent)cc2;
tc.setText(TimeStampUtil.timeStamp());
ss.setComponent("TransferId", (ContentComponent)tc);
}
ContentComponent cc3 = (ContentComponent)ss.getComponent("Status");
if(cc2 instanceof TextComponent){
TextComponent tc = (TextComponent)cc3;
tc.setText("Pending Store Ops");
ss.setComponent("Status", (ContentComponent)tc);
}
String[] saveMessage = workspace.save((Document)ss);
// find sub sitearea by name
DocumentIdIterator siteAreaIterator3 = workspace.findByName(DocumentTypes.SiteArea,childSiteAreaName);
if (siteAreaIterator3.hasNext()){
childSiteAreaId = siteAreaIterator3.nextId();
logger.info("Sitearea Found : " + childSiteAreaId + "<br>");
}else {
logger.info("Could not find Sitearea : " + childSiteAreaName + "<br>");
}
// find workflow by name
DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow,workflowName);
if (workflowIterator.hasNext()){
workflowId = workflowIterator.nextId();
logger.info("Workflow found *: " + workflowName + "<br>");
}else {
logger.info("Could not find Workflow: " + workflowName + "<br>");
}
if((authoringTemplateId!=null) && (parentSiteAreaId!=null) && (workflowId!=null)){
for (String[] strings : dataList) {
// create new content
counter=counter+1;
Content newContent = workspace.createContent(authoringTemplateId,childSiteAreaId,siblingId,ChildPosition.END);
newContent.setName("TransferDirection_Content"+Integer.toString(counter));
newContent.setTitle("NewContent_csv_row"+Integer.toString(counter));
newContent.setDescription("New Content created for individual css");
newContent.setWorkflowId(workflowId);
ContentComponent contentComponent = newContent.getComponent("Sending");
if(contentComponent instanceof TextComponent){
TextComponent tc = (TextComponent)contentComponent;
logger.info("First element :-"+tc.getName());
tc.setText(strings[0]);
logger.info("First element content :-"+tc.getText());
newContent.setComponent("Sending", (ContentComponent)tc);
}
ContentComponent contentComponent2 = newContent.getComponent("Recieving");
if(contentComponent2 instanceof TextComponent){
TextComponent tc2 = (TextComponent)contentComponent2;
logger.info("Second element :-"+tc2.getName());
tc2.setText(strings[1]);
logger.info("Second element content :-"+tc2.getText());
newContent.setComponent("Recieving", (ContentComponent)tc2);
}
ContentComponent contentComponent3 = newContent.getComponent("Qty");
if(contentComponent3 instanceof NumericComponent){
NumericComponent tc3 = (NumericComponent)contentComponent3;
logger.info("Third element :-"+tc3.getName());
tc3.setNumber(Integer.parseInt(strings[2]));
logger.info("Third element content :-"+tc3.getNumber());
newContent.setComponent("Qty", (ContentComponent)tc3);
}
String[] saveMessage2 = workspace.save((Document)newContent);
logger.info("Save message :- "+saveMessage2.toString());
if (saveMessage2.length==0) {
logger.info (" <BR> Created new Content under " + childSiteAreaName );
}
}
}else {
logger.info ("Could not create new content");
}
}catch(Exception e){
e.printStackTrace();
}
}
//Logic whether to navigate to other stage or not
try {
initContext = new InitialContext();
webContentCustomWorkflowService = (WebContentCustomWorkflowService) initContext.lookup("portal:service/wcm/WebContentCustomWorkflowService");
result = webContentCustomWorkflowService.createResult(directive,message);
logger.info("Getting base wcm service");
} catch (Exception ex) {
message = "An exception has occured in ex " + ex.getMessage();
directive = Directives.ROLLBACK_DOCUMENT;
ex.printStackTrace();
}
return result;
}
@Override
public Date getExecuteDate(Document arg0) {
// TODO Auto-generated method stub
return DATE_EXECUTE_NOW;
}
}
Step4 :- Create all utility classes needed for it
package com.Poc.workflow.util;
import java.io.BufferedReader;
public class CsvParser {
private int rowCount; // Number of rows (excluding header row)
private int fieldCount; // Number of fields in each row
private String[] headers; // Field names (assumed to be in first row)
private List<String[]> rows = new ArrayList<String[]>(); // The data
public CsvParser() {
// Must have a no-argument constructor as a bean.
}
public void parseFile(InputStream inputStream) {
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
} catch (Exception e) {
e.printStackTrace();
return;
}
String line;
boolean firstRow = true;
try {
while ((line = br.readLine()) != null) {
//Parsing Csv
if (firstRow) {
headers = tokenize(line);
setFieldCount(headers.length);
firstRow = false;
}else{
String[] row = tokenize(line);
rows.add(row);
rowCount++;
}
}
} catch (IOException e1) {
e1.printStackTrace();
return;
}
}
protected String[] tokenize(String line) {
StringTokenizer st = new StringTokenizer(line, ",");
int tokenCount = st.countTokens();
String[] record = new String[tokenCount];
for (int i = 0; i < tokenCount; i++) {
record[i] = (String) st.nextToken();
}
return record;
}
public int getFieldCount() {
return fieldCount;
}
public void setFieldCount(int fieldCount) {
this.fieldCount = fieldCount;
}
public String[] getHeaders() {
return headers;
}
public void setHeaders(String[] headers) {
this.headers = headers;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Headers: ");
addStringArray(headers, sb);
Iterator it = rows.iterator();
int rowCount = 0;
while (it.hasNext()) {
rowCount++;
sb.append("Row " + rowCount + ": ");
String[] row = (String []) it.next();
addStringArray(row, sb);
}
return sb.toString();
}
protected StringBuffer addStringArray(String[] array, StringBuffer buffer) {
for (int i = 0; i < array.length; i++) {
buffer.append(array[i] + ",");
}
buffer.append("\n");
return buffer;
}
}
Timestamp utility
package com.Poc.workflow.util;
import java.sql.Timestamp;
public class TimeStampUtil
{
public static String timeStamp()
{
java.util.Date date= new java.util.Date();
Timestamp tm = new Timestamp(date.getTime());
System.out.println(tm.toString());
return tm.toString();
}
}
Step 5 :- Deploy this war file and go to web content management and map custom action. Refere to my previous custom email action blog for help in mapping actions.
No comments:
Post a Comment