From odf-commits-return-153-apmail-incubator-odf-commits-archive=incubator.apache.org@incubator.apache.org Fri Sep 2 09:31:17 2011 Return-Path: X-Original-To: apmail-incubator-odf-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-odf-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 454B87A43 for ; Fri, 2 Sep 2011 09:31:17 +0000 (UTC) Received: (qmail 40490 invoked by uid 500); 2 Sep 2011 09:31:17 -0000 Delivered-To: apmail-incubator-odf-commits-archive@incubator.apache.org Received: (qmail 40354 invoked by uid 500); 2 Sep 2011 09:31:13 -0000 Mailing-List: contact odf-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: odf-dev@incubator.apache.org Delivered-To: mailing list odf-commits@incubator.apache.org Received: (qmail 40347 invoked by uid 99); 2 Sep 2011 09:31:12 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Sep 2011 09:31:12 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 02 Sep 2011 09:31:09 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id D4AF523889E7; Fri, 2 Sep 2011 09:30:48 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1164432 - /incubator/odf/site/trunk/content/odftoolkit/simple/demo/demo7.mdtext Date: Fri, 02 Sep 2011 09:30:48 -0000 To: odf-commits@incubator.apache.org From: devinhan@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110902093048.D4AF523889E7@eris.apache.org> Author: devinhan Date: Fri Sep 2 09:30:48 2011 New Revision: 1164432 URL: http://svn.apache.org/viewvc?rev=1164432&view=rev Log: clear tags Modified: incubator/odf/site/trunk/content/odftoolkit/simple/demo/demo7.mdtext Modified: incubator/odf/site/trunk/content/odftoolkit/simple/demo/demo7.mdtext URL: http://svn.apache.org/viewvc/incubator/odf/site/trunk/content/odftoolkit/simple/demo/demo7.mdtext?rev=1164432&r1=1164431&r2=1164432&view=diff ============================================================================== --- incubator/odf/site/trunk/content/odftoolkit/simple/demo/demo7.mdtext (original) +++ incubator/odf/site/trunk/content/odftoolkit/simple/demo/demo7.mdtext Fri Sep 2 09:30:48 2011 @@ -1,221 +1,155 @@ - - - - - - -Demos - SimpleODF - - - - - - - -
-
-
-
-
- -
- - - - -
-

We have introduced several samples about how to use Simple ODF API to generate document from the predefined templete, for example, Demo 3: Navigation API in Hotel Receipt. I believe you have known how easy it is. But sounds like, we never talk about how to create a template document using Simple ODF API.

-Today, let's do it.

-Simple ODF API provides Fields API since version 0.5. OpenDocument text documents or OpenDocument text content embedded in other types of documents may contain variable text elements called fields. Fields are commonly used for, a page number field displays the number of the page on which it appears or a creation date field displays the date on which a document was created. Besides, there are several advance field, such as, variable field and condition field.

-This demo provides a Java application which can create the offer letter template document from a sample and use it to generate new offer letters with the information from a spreadsheet. The template document contains some fields whose content is decided by the information from data file.

-The following picture show a sample offer letter. I have labeled the variable content with blod and color style.

-

-This offer was generated on "2011-01-20". It also can be in any other date, so this text content should be replaced by a date field. "Tom King" is the receiver, this name can be replaced with a variable field, then we can send it to any other perpson. Similarly, "software engineer" is the job title of "Tom King", "R&D" is the branch which he belongs to, and "2011-03-12" is his onboard date, all of them should be replaced by variable field in a template document.

-Salary and allowance are more complex. Different job titles and branches have different rules. Simplicity, we assume that except "Secretary" is 5,500,00, all of the other job titles are 6,000.00. We also assume that employees from "Sales" don't have allowance. Condition field or Hidden Text field is needed to realize these rules. The same as certificates/testimonials, for campus hires, they need supply "Educational certificates", while for professinal hires, they need supply "Experience certificates from previous employers" and "Copy of resignation/acceptation letter and relieving letter". This is another use case of condition field.

-After replaced all of these variable content with fields, the template document is ready. The process of generate new offer letter is very simple. Only need to load the following data, update the variable values with the data and save as a new document.

-

-No need text content search, no need target replace. It's faster than the content replace way. Beside, the variable content is highlighted automatically by Symphony or Open Office when the document is opend. The following is a generated letter sample.

-

-As using fields, the new offer letter is easily updated without inconsistent, even though after generated. You can do it in Symphony or Open Office. -

-
-
- -
-

- There are two methods in the class, createFieldTemplate() is used to create template document from sample, and generateOfferLetterDocument() is used to generated new document using template and data file.
- -

-	public static void main(String[] args) {
-		try {
-			// create offer letter template with sample offer letter.
-			createFieldTemplate();
-			// generate offer letters for the candidates from spreadsheet document.
-			generateOfferLetterDocument();
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-	}
-    
-

-

Below is the source code of method createFieldTemplate(). The basic process is find the variable content and replace it with a field. We use FixedDateField, UserVariableField, ConditionField and HiddenTextField in this method. Simple ODF API provides more types of fields. For more information, please reference the Javadoc or CookBook.
- -

-	public static void createFieldTemplate() throws Exception {
-		TextDocument sampleDocument = TextDocument.loadDocument("Tom's Offer.odt");
-		// replace offer date with FixedDateField
-		TextNavigation search = new TextNavigation("2011-01-20", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection.replaceWithSimpleField(Field.FieldType.FIXED_DATE_FIELD);
-		}
-		// replace candidate name with UserVariableField
-		VariableField candidateVar = Fields.createUserVariableField(sampleDocument, "candidateVar", "Tom King");
-		search = new TextNavigation("Tom King", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection.replaceWithVariableField(candidateVar);
-		}
-		// replace job title with UserVariableField
-		VariableField jobTitleVar = Fields.createUserVariableField(sampleDocument, "jobTitleVar", "software engineer");
-		search = new TextNavigation("software engineer", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection.replaceWithVariableField(jobTitleVar);
-		}
-		// replace branch with UserVariableField
-		VariableField branchVar = Fields.createUserVariableField(sampleDocument, "branchVar", "R&D");
-		search = new TextNavigation("R&D", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection.replaceWithVariableField(branchVar);
-		}
-		// replace onboard date with UserVariableField
-		VariableField onBoardDateVar = Fields.createUserVariableField(sampleDocument, "onBoardDateVar", "2011-03-12");
-		search = new TextNavigation("2011-03-12", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection.replaceWithVariableField(onBoardDateVar);
-		}
-		// replace salary with ConditionField
-		search = new TextNavigation("6,000.00", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection.replaceWithConditionField("jobTitleVar != \"Secretary\"", "6,000.00", "5,500.00");
-		}
-		// replace consolidated allowance with HiddenTextField
-		search = new TextNavigation(" and a Consolidated Allowance of 400\\$ per month", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection.replaceWithHiddenTextField("branchVar == \"Sales\"", " and a Consolidated Allowance of 400$ per month");
-		}
-		// replace certificates/testimonials with ConditionField
-		Fields.createUserVariableField(sampleDocument, "campusHireVar", "N");
-		search = new TextNavigation("\\* Educational certificates", sampleDocument);
-		while (search.hasNext()) {
-			TextSelection item = (TextSelection) search.nextSelection();
-			FieldSelection fieldSelection = new FieldSelection(item);
-			fieldSelection
-					.replaceWithConditionField(
-							"campusHireVar == \"Y\"",
-							"* Educational certificates",
-							"* Experience certificates from previous employers\n* Copy of resignation/acceptation letter and relieving letter");
-		}
-		sampleDocument.save("OfferTemplate.odt");
-	}
-    
-

-

Method generateOfferLetterDocument() is easier. Just load the template document and data docuemnt and fill new value to all of the variables. getVariableFieldByName() is a method of VariableContainer used to iterate variable field by name. -
- -

-	public static void generateOfferLetterDocument() throws Exception {
-		TextDocument templateDocument = TextDocument.loadDocument("OfferTemplate.odt");
-		SpreadsheetDocument dataDocument = SpreadsheetDocument.loadDocument("Candidates.ods");
-		Table table = dataDocument.getTableByName("Sheet1");
-		int rowCount = table.getRowCount();
-		for(int i = 1; i < rowCount; i++)
-		{
-			Row row = table.getRowByIndex(i);
-			String name = row.getCellByIndex(0).getDisplayText();
-			VariableField candidateVar = templateDocument.getVariableFieldByName("candidateVar");
-			candidateVar.updateField(name, null);
-			String jobTitle = row.getCellByIndex(1).getDisplayText();
-			VariableField jobTitleVar = templateDocument.getVariableFieldByName("jobTitleVar");
-			jobTitleVar.updateField(jobTitle, null);
-			String branch = row.getCellByIndex(2).getDisplayText();
-			VariableField branchVar = templateDocument.getVariableFieldByName("branchVar");
-			branchVar.updateField(branch, null);
-			String isCampusHire = row.getCellByIndex(3).getDisplayText();
-			VariableField campusHireVar = templateDocument.getVariableFieldByName("campusHireVar");
-			campusHireVar.updateField(isCampusHire, null);
-			String onBoradDate = row.getCellByIndex(4).getDisplayText();
-			VariableField onBoardDateVar = templateDocument.getVariableFieldByName("onBoardDateVar");
-			onBoardDateVar.updateField(onBoradDate, null);
-			templateDocument.save(name+"'s offer letter.odt");
-		}
-	}
-    
-

-
-
- -
-

- Powered by the Simple Java API for ODF version 0.5. -

-

- You can download the code of this demo from here. -

-
- -
- -
-
- - - - - +Simple Demos > Offer Letter with Text Fields + +**Overview** + +We have introduced several samples about how to use Simple ODF API to generate document from the predefined templete, for example, Demo: [Navigation API in Hotel Receipt][1]. I believe you have known how easy it is. But sounds like, we never talk about how to create a template document using Simple ODF API. + +Today, let's do it. + +Simple API provides Fields API since version 0.5. OpenDocument text documents or OpenDocument text content embedded in other types of documents may contain variable text elements called fields. Fields are commonly used for, a page number field displays the number of the page on which it appears or a creation date field displays the date on which a document was created. Besides, there are several advance field, such as, variable field and condition field. + +This demo provides a Java application which can create the offer letter template document from a sample and use it to generate new offer letters with the information from a spreadsheet. The template document contains some fields whose content is decided by the information from data file. + +The following picture show a sample offer letter. I have labeled the variable content with blod and color style. + +![image1][2] + +This offer was generated on "2011-01-20". It also can be in any other date, so this text content should be replaced by a date field. "Tom King" is the receiver, this name can be replaced with a variable field, then we can send it to any other perpson. Similarly, "software engineer" is the job title of "Tom King", "R&D" is the branch which he belongs to, and "2011-03-12" is his onboard date, all of them should be replaced by variable field in a template document.

+Salary and allowance are more complex. Different job titles and branches have different rules. Simplicity, we assume that except "Secretary" is 5,500,00, all of the other job titles are 6,000.00. We also assume that employees from "Sales" don't have allowance. Condition field or Hidden Text field is needed to realize these rules. The same as certificates/testimonials, for campus hires, they need supply "Educational certificates", while for professinal hires, they need supply "Experience certificates from previous employers" and "Copy of resignation/acceptation letter and relieving letter". This is another use case of condition field.

+After replaced all of these variable content with fields, the template document is ready. The process of generate new offer letter is very simple. Only need to load the following data, update the variable values with the data and save as a new document. + +![image2][3] + +No need text content search, no need target replace. It's faster than the content replace way. Beside, the variable content is highlighted automatically by Symphony or Open Office when the document is opend. The following is a generated letter sample. + +![image3][4] + +As using fields, the new offer letter is easily updated without inconsistent, even though after generated. You can do it in Symphony or Open Office. +Code Introduction +There are two methods in the class, createFieldTemplate() is used to create template document from sample, and generateOfferLetterDocument() is used to generated new document using template and data file. + + public static void main(String[] args) { + try { + // create offer letter template with sample offer letter. + createFieldTemplate(); + // generate offer letters for the candidates from spreadsheet document. + generateOfferLetterDocument(); + } catch (Exception e) { + e.printStackTrace(); + } + } +Below is the source code of method createFieldTemplate(). The basic process is find the variable content and replace it with a field. We use FixedDateField, UserVariableField, ConditionField and HiddenTextField in this method. Simple ODF API provides more types of fields. For more information, please reference the [Javadoc][5] or [CookBook][6]. + + public static void createFieldTemplate() throws Exception { + TextDocument sampleDocument = TextDocument.loadDocument("Tom's Offer.odt"); + // replace offer date with FixedDateField + TextNavigation search = new TextNavigation("2011-01-20", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection.replaceWithSimpleField(Field.FieldType.FIXED_DATE_FIELD); + } + // replace candidate name with UserVariableField + VariableField candidateVar = Fields.createUserVariableField(sampleDocument, "candidateVar", "Tom King"); + search = new TextNavigation("Tom King", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection.replaceWithVariableField(candidateVar); + } + // replace job title with UserVariableField + VariableField jobTitleVar = Fields.createUserVariableField(sampleDocument, "jobTitleVar", "software engineer"); + search = new TextNavigation("software engineer", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection.replaceWithVariableField(jobTitleVar); + } + // replace branch with UserVariableField + VariableField branchVar = Fields.createUserVariableField(sampleDocument, "branchVar", "R&D"); + search = new TextNavigation("R&D", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection.replaceWithVariableField(branchVar); + } + // replace onboard date with UserVariableField + VariableField onBoardDateVar = Fields.createUserVariableField(sampleDocument, "onBoardDateVar", "2011-03-12"); + search = new TextNavigation("2011-03-12", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection.replaceWithVariableField(onBoardDateVar); + } + // replace salary with ConditionField + search = new TextNavigation("6,000.00", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection.replaceWithConditionField("jobTitleVar != \"Secretary\"", "6,000.00", "5,500.00"); + } + // replace consolidated allowance with HiddenTextField + search = new TextNavigation(" and a Consolidated Allowance of 400\\$ per month", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection.replaceWithHiddenTextField("branchVar == \"Sales\"", " and a Consolidated Allowance of 400$ per month"); + } + // replace certificates/testimonials with ConditionField + Fields.createUserVariableField(sampleDocument, "campusHireVar", "N"); + search = new TextNavigation("\\* Educational certificates", sampleDocument); + while (search.hasNext()) { + TextSelection item = (TextSelection) search.nextSelection(); + FieldSelection fieldSelection = new FieldSelection(item); + fieldSelection + .replaceWithConditionField( + "campusHireVar == \"Y\"", + "* Educational certificates", + "* Experience certificates from previous employers\n* Copy of resignation/acceptation letter and relieving letter"); + } + sampleDocument.save("OfferTemplate.odt"); + } + +Method generateOfferLetterDocument() is easier. Just load the template document and data docuemnt and fill new value to all of the variables. getVariableFieldByName() is a method of VariableContainer used to iterate variable field by name. + + public static void generateOfferLetterDocument() throws Exception { + TextDocument templateDocument = TextDocument.loadDocument("OfferTemplate.odt"); + SpreadsheetDocument dataDocument = SpreadsheetDocument.loadDocument("Candidates.ods"); + Table table = dataDocument.getTableByName("Sheet1"); + int rowCount = table.getRowCount(); + for(int i = 1; i < rowCount; i++) + { + Row row = table.getRowByIndex(i); + String name = row.getCellByIndex(0).getDisplayText(); + VariableField candidateVar = templateDocument.getVariableFieldByName("candidateVar"); + candidateVar.updateField(name, null); + String jobTitle = row.getCellByIndex(1).getDisplayText(); + VariableField jobTitleVar = templateDocument.getVariableFieldByName("jobTitleVar"); + jobTitleVar.updateField(jobTitle, null); + String branch = row.getCellByIndex(2).getDisplayText(); + VariableField branchVar = templateDocument.getVariableFieldByName("branchVar"); + branchVar.updateField(branch, null); + String isCampusHire = row.getCellByIndex(3).getDisplayText(); + VariableField campusHireVar = templateDocument.getVariableFieldByName("campusHireVar"); + campusHireVar.updateField(isCampusHire, null); + String onBoradDate = row.getCellByIndex(4).getDisplayText(); + VariableField onBoardDateVar = templateDocument.getVariableFieldByName("onBoardDateVar"); + onBoardDateVar.updateField(onBoradDate, null); + templateDocument.save(name+"'s offer letter.odt"); + } + } + +**Download** + +Powered by the Simple Java API for ODF version [0.5][7]. +You can download the code of this demo from [here][8]. + + + [1]: demo3.html + [2]: image/offer-letter-sample.png + [3]: image/candidate-data.png + [4]: image/generated-offer-letter.png + [5]: ../javadoc/index.html + [6]: ../cookbook/index.html + [7]: ../downloads.html + [8]: demo7.zip \ No newline at end of file