import com.technia.tvc.core.TVCException;
import com.technia.tvc.core.db.BusinessObjectUtils;
import com.technia.tvc.core.db.domain.DomainObject;
import com.technia.tvc.core.db.domain.to.FileTO;
import com.technia.tvc.core.html.io.XMLWriter;
import com.technia.tvc.core.util.XMLUtils;
import com.technia.tvc.office.server.fpd.Handler;
import com.technia.tvc.office.server.fpd.HandlerCtx;
import com.technia.tvc.office.server.fpd.HandlerCtx.PackageEntry;
import com.technia.tvc.office.server.fpd.HandlerCtx.TableExportEntry;
import com.technia.tvc.office.server.fpd.HandlerCtx.XMLWriterEntry;
import java.io.StringReader;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
public class TestHandler implements Handler {
public void perform(HandlerCtx ctx) throws TVCException {
/*
* Test MQL
*/
String[] adminObjects = new String[]
{ "type", "attribute", "person", "role", "group", "page" };
for (int i = 0; i < adminObjects.length; i++) {
PackageEntry admin = ctx.addMQL("list " + adminObjects[i]);
admin.setSaveAs(adminObjects[i] + "s.txt");
admin.setSaveIn("admin");
}
/*
* Test URL
*/
PackageEntry url = ctx.addURL("/tvc/core/images/tvc_logo_big.gif");
url.setSaveIn("images");
url.setSaveAs("tvc_logo.gif");
/*
* Test adding input stream
*/
PackageEntry is = ctx.add(getClass().getResourceAsStream("TestHandler.class"));
is.setSaveIn("data");
is.setSaveAs("TestHandler.class");
/*
* Test adding reader
*/
PackageEntry reader = ctx.add(new StringReader("Hello world"));
reader.setSaveIn("misc");
reader.setSaveAs("helloworld1.txt");
/*
* Test adding plain string
*/
PackageEntry data = ctx.add("Hello World!");
data.setSaveIn("misc");
data.setSaveAs("helloworld2.txt");
/*
* Test adding page-object content
*/
PackageEntry page = ctx.addPageContent("TVC File Package Download Config");
page.setSaveAs("TVCFilePackageDownloadConfig.xml");
page.setSaveIn("admin");
/*
* Test XMLWriter
*/
XMLWriterEntry xml = ctx.addXML();
xml.setSaveIn("misc");
xml.setSaveAs("xmldata.xml");
XMLWriter xmlWriter = xml.getWriter();
xmlWriter.startElement("hello");
xmlWriter.startElement("world");
xmlWriter.finish();
/*
* Test adding a DOM Document
*/
try {
Document document = XMLUtils.createBuilder(null).newDocument();
document.appendChild(document.createElement("HelloWorld"));
document.getDocumentElement().appendChild(document.createElement("Test"));
PackageEntry doc = ctx.addXML(document);
doc.setSaveIn("misc");
doc.setSaveAs("doc.xml");
} catch (ParserConfigurationException pce) {
throw new TVCException(pce);
}
if (ctx.getUserData().getObjectIdCount() > 0) {
String ids[] = ctx.getUserData().getObjectIds();
for (int count = 0; count < ids.length; count++) {
process(ctx, ids[count]);
}
}
/*
* Test adding a CSV export of a simple query
*/
TableExportEntry tee = ctx.addTableData("Part", "*", "*", "*", "*", "", 100, true, null);
tee.setCSVOutput();
tee.setSaveIn("csv");
tee.setSaveAs("Parts_FromQuery.csv");
}
private void process(HandlerCtx ctx, String objectId) throws TVCException {
/*
* Table Export
*/
TableExportEntry tee = ctx.addExpandedData(objectId, "role_GlobalUser", new String[]
{ "EBOM From" }, 0, "TVC EBOM");
tee.setXMLOutput();
tee.setSaveAs("table_export.xml");
tee.setSaveIn(objectId);
/*
* Matrix Export
*/
String tnr[] = BusinessObjectUtils.getTNR(BusinessObjectUtils.newBusinessObject(objectId));
PackageEntry export = ctx.addExportBus(
tnr[0], tnr[1], tnr[2],
false, false, false, true,
true, true, true, false, null, null);
export.setSaveIn(objectId);
export.setSaveAs("export.xml");
/*
* Get related documents, if any, and do some additional testing.
*/
DomainObject dom = DomainObject.newInstance(objectId);
List<DomainObject> l = dom.getRelatedObjects("DOCUMENTS", "*", true, false);
for (int i = 0; i < l.size(); i++) {
DomainObject related = l.get(i);
/*
* Export again...
*/
PackageEntry exportEntry = ctx.addExportBus(
related.getType(),
related.getName(),
related.getRevision());
exportEntry.setSaveIn(objectId);
exportEntry.setSaveAs(String.format("%s_%s.xml",
related.getName(),
related.getRevision()));
/*
* Test file checkout
*/
FileTO files[] = related.getFiles();
for (int j = 0; j < files.length; j++) {
PackageEntry fileEntry = null;
if ((j % 2) == 0) {
fileEntry = ctx.addFileDependency(
related.getObjectId(),
files[j].getFormat(),
files[j].getFileName());
} else {
fileEntry = ctx.addFile(
related.getObjectId(),
files[j].getFormat(),
files[j].getFileName());
}
fileEntry.setSaveAs(files[j].getFileName());
fileEntry.setSaveIn(objectId + "/files");
}
}
}
}