AnsweredAssumed Answered

Replace Data Using ContentWriter object to word Document In Alfresco using Document Node Reference

Question asked by deeps on May 20, 2016
I am doing Bulk Upload Task in Alfresco.

Before this i created custom action to call java code, i also successfully read data from excel sheet, and i found node reference of target document as well as source Document. Using that node reference i am also able to create new multiple Documents. Now My requirement is, I want to replace Excel Data in that newly created Document. I tried to replace it, But It replacing the String only in First line of document, and it also deleting Rest of the existing contents inside newly created document. I have written Below code for this.

In below code i am first simply trying to replace some hard coded data to the Document. But My requirement is i want to replace the data inside document which i already read from excel file.

Java Code`public class MoveReplacedActionExecuter extends ActionExecuterAbstractBase { InputStream is; Cell cell= null; public static final String NAME = "move-replaced";
private FileFolderService fileFolderService; private NodeService nodeService; private ContentService contentService; private SearchService searchService;
@Override protected void addParameterDefinitions(List paramList) {
}
public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {       

try
{
    ContentReader contentReader = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
     is = contentReader.getContentInputStream();
}
catch(NullPointerException ne)
{
    System.out.println("Null Pointer Exception"+ne);
}

try{     

    Workbook workbook = new XSSFWorkbook(is);

    Sheet firstSheet = workbook.getSheetAt(0);
    Iterator<Row> iterator = firstSheet.rowIterator();

    while (iterator.hasNext()) {
        ArrayList<String> al = new ArrayList<>();          
        System.out.println("");
        Row nextRow = iterator.next();
        Iterator<Cell> cellIterator = nextRow.cellIterator();          
        while (cellIterator.hasNext()) {               
       cell = cellIterator.next();              
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print("\t"+cell.getStringCellValue());
                    al.add(cell.getStringCellValue());                 
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print("\t"+cell.getBooleanCellValue());
                    al.add(String.valueOf(cell.getBooleanCellValue()));
                       break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print("\t"+cell.getNumericCellValue());
                    al.add(String.valueOf(cell.getNumericCellValue()));                  
                    break;
            }                                      
        }         
    }  

    is.close();
}
catch(Exception e)
{
e.printStackTrace();
}
String query="PATH:\"/app:company_home/cm:Dipak/cm:OfferLetterTemplate.doc\"";
SearchParameters sp=new SearchParameters();
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE,"SpacesStore");
sp.addStore(storeRef);
sp.setLanguage(SearchService.LANGUAGE_LUCENE);
sp.setQuery(query);
ResultSet resultSet= searchService.query(sp);
System.out.println("Result Set"+resultSet.length());
NodeRef sourceNodeRef=null;
for (ResultSetRow row : resultSet) {
     NodeRef currentNodeRef = row.getNodeRef();
     sourceNodeRef=currentNodeRef;
     System.out.println(currentNodeRef.toString());
     }  
NodeRef n=new NodeRef("workspace://SpacesStore/78342318-37b8-4b42-aadc-bb0ed5d413d9"); 

try {
    org.alfresco.service.cmr.model.FileInfo fi= fileFolderService.copy(sourceNodeRef,  n, "JustCreated"+Math.random()+".doc");
    NodeRef newNode= fi.getNodeRef();

     QName TYPE_AUTHORTY = QName.createQName("sunpharma.hr.model","hrdoctype");
     nodeService.setType(newNode, TYPE_AUTHORTY);

    ContentReader contentReader1 = contentService.getReader(newNode, ContentModel.PROP_CONTENT);
     InputStream is2 = contentReader1.getContentInputStream();

     POIFSFileSystem fs = new POIFSFileSystem(is2);
     HWPFDocument doc = new HWPFDocument(fs);

     doc = replaceText1(doc, "Company", "Datamatics");          
     ContentWriter writerDoc = contentService.getWriter(newNode, ContentModel.PROP_CONTENT, true); 
     writerDoc.putContent(doc.getDocumentText());       

} catch (FileExistsException | FileNotFoundException e) {

    e.printStackTrace();
} catch (IOException e) {

    e.printStackTrace();
}

}
    private static HWPFDocument replaceText1(HWPFDocument doc, String findText, String replaceText){
        System.out.println("In the method replacetext"+replaceText);

        Range r1 = doc.getRange();
        System.out.println("Range of Doc : "+r1);

        for (int i = 0; i < r1.numSections(); ++i ) {
            Section s = r1.getSection(i);           
            for (int x = 0; x < s.numParagraphs(); x++) {
                Paragraph p = s.getParagraph(x);              
                for (int z = 0; z < p.numCharacterRuns(); z++) {
                    CharacterRun run = p.getCharacterRun(z);
                    String text = run.text();
                    if(text.contains(findText)) {
                        run.replaceText(findText, replaceText);                       
                    }
                    else{
                        System.out.println("NO text found");
                    }
                }
            }
        }
        return doc;
    }
    public void setFileFolderService(FileFolderService fileFolderService) {
        this.fileFolderService = fileFolderService;
    }   
    public void setNodeService(NodeService nodeService)
    {
        this.nodeService = nodeService;
    } 
    public void setContentService(ContentService contentService)
    {
        this.contentService = contentService;
    }   
    public void setSearchService(SearchService searchService) {
        this.searchService = searchService;
    }
}



Please review code and help me in This small task.

Thanks in Advance

Outcomes