tutorials
May 07, 2013
Analisi (e modifica) di come Plone genera link ai File - seconda parte
Continua il viaggio e l'analisi dei prodotti per rendere Plone compatibile con software di statistiche
Nella prima parte dell'articolo abbiamo risolto solo una parte del problema per rendere gli URL di Plone più "parlanti".
Vediamo ora come affrontare i problemi restanti.
Fase 2: come trattare il pregresso
Infatti è solo ora che le cose si fanno interessanti...
Ci siamo trovati di fronte a un grosso problema: come "bonificare" la situazione creatasi negli anni precedenti alle modifiche sopra descritte?
Sarebbe stato possibile scrivere una procedura automatica che sistemasse automaticamente tutti i link generati tramite TinyMCE dalla forma canonica alla forma di download modificata?
La risposta per noi è stata "forse... ma meglio di no". Si parlava di centinaia (migliaia) di link, per decine di siti Plone.
L'eventualità di eseguire qualcosa senza un intervento umano era a dir poco terrificante, col rischio di trovare contenuti rovinati dall'operazione dopo mesi.
Molto meglio aiutare l'utente a sistemare il problema, cercando per lui i potenziali link da sistemare ma chiedendo conferma dell'operazione: un lavoro di certo più lungo ma che dà la sicurezza del risultato.
Document Actions
May 03, 2013
Analisi (e modifica) di come Plone genera link ai File: una storia vera (parte 1)
L'esigenza: rendere i link ai file generati in Plone compatibili con software di statistiche e come una serie di prodotti riutilizzabili abbiamo risolto il problema
C'era una volta il File in Plone
Questa Storia parte da un Cliente non molto contento del modo in cui Plone gestisce i file.
Nel caso non lo sappiate, c'è una certa confusione a riguardo.
Se nel vostro sito Plone vi collegate all'indirizzo diretto a un file, nella forma...
http://vostro-host.com/percorso/al/file.pdf
...il file in questione viene "aperto direttamente": gli header inviati da Plone scatenano l'apertura del file "inline", quindi sfruttando eventuali plugin del browser, se presenti.
Questo tipo di comportamento ha problemi di usabilità: utenti che non capiscono di essere ancora "dentro al browser" potrebbero chiudere il browser pensando che si tratti di un programma esterno. Se l'utente poi vuole scaricare il file dal plugin, deve trovarne la funzione all'interno dello stesso.
Eppure se arrivate allo stesso file dall'interfaccia Plone (dal navigatore del sito, da una delle viste, ...) vi troverete a un URL diverso:
http://vostro-host.com/percorso/al/file.pdf/view
Questo è l'indirizzo della vista del contenuto file (file_view) da cui potete vedere alcune informazioni sul file e da dove viene mostrato il link per scaricarlo, che assume invece questa forma:
http://vostro-host.com/percorso/al/file.pdf/at_download/file
Document Actions
Apr 12, 2013
How to automatically refresh your database in "silent mode"
Break away from dependence on the Lotus Notes client to update your database: an easy tutorial to get applications with the latest update every morning
Scenario: a development server, a release server and many production customers' servers: nothing more classic.
Every time a change is deployed on the release server, the best thing would be to "push" it automatically on production server, or as soon as possible (at least).
Document Actions
Apr 03, 2013
PuDB, ovvero: come ho imparato a velocizzare il mio debug
Quando si fa debug di codice poco "docile" è importante avere gli strumenti giusti per non impazzire sprecando tempo prezioso nel trovare il problema

Nella vita di ogni sviluppatore arriva il giorno in cui si incrocia la propria tastiera con codice incomprensibile, dai risultati inspiegabili e che, ovviamente, bisogna correggere. Avere gli strumenti adatti per il debugging è fondamentale.
Come programmatore python ho avuto modo di utilizzare diversi tipi di debugger, dal semplice pdb a una versione più completa: pdb++. Esistono debugger integrati nei vari IDE di sviluppo, di cui però non ho mai approfondito troppo l'uso dal momento che li ho sempre trovato ambienti un po' pesanti. Da amante della shell, divido il mio tempo di sviluppo fra vim e un editor di testo molto semplice come sublime.
Document Actions
Mar 08, 2013
Merging odf files using XDocReport and XPages
Often there is the need to create documents from templates, and the need to fill these templates with data available from other sources: let's take a look at this brief solution
In this scenario, we have a odf template to merge with data.
Working in the Lotus Domino environment, one way to achieve this goal could be a server-side component invoked by an XPagdies, to make the casting process: first, I need something in Java that could do the job for me, and after some search I came across XDocReport.
XDocReport means XML Document reporting
XDocReport provides Java API to merge XML document created with MS Office (docx, pptx) or OpenOffice (odt), LibreOffice (odt) with a Java model to generate report and convert it in another format (PDF, XHTML...).
Basicly, to make the template, main steps are:
- create a template document with MS Word (docx) or OpenOffice (odt, ods)
- compose the body of the document itself with variables to fill in
- use Velocity or Freemarker syntax to set variables to replace.
In this case, I opted to use Velocity inside an ODF file.
Now let's start by explaining how to set up the whole project.
Installing server libraries
External libraries (.jar) that serve the purpose have been installed in the installation directory of the Domino server (jvm/lib/ext path).
These are the files:
commons-collections-3.2.1.jar commons-lang-2.4.jar fr.opensagres.xdocreport.converter-1.0.0.jar fr.opensagres.xdocreport.converter.odt.odfdom-1.0.0.jar fr.opensagres.xdocreport.core-1.0.0.jar fr.opensagres.xdocreport.document-1.0.0.jar fr.opensagres.xdocreport.document.odt-1.0.0.jar fr.opensagres.xdocreport.itext.extension-1.0.0.jar fr.opensagres.xdocreport.template-1.0.0.jar fr.opensagres.xdocreport.template.velocity-1.0.0.jar itext-2.1.7.jar odfdom-java-0.8.7.jar org.odftoolkit.odfdom.converter-0.9.0.jar org.odftoolkit.odfdom.converter.core-1.0.0.jar org.odftoolkit.odfdom.converter.pdf-1.0.0.jar org.odftoolkit.odfdom.converter.xhtml-1.0.0.jar oro-2.0.8.jar velocity-1.7.jar
Dont' forget an important security setting in java.policy file:
Server-side tools
To manage the whole process I used an HTML page with a button that calls (via Ajax) an XPages, that deals with the fusion of the data (HTML string) in the template. At the end, I create the PDF file using iText libraries.
Related to XPages, it only call the servlet in the afterPageLoad event:
com.redturtle.XDocReport.mergeAndPDF( facesContext.getExternalContext().getRequest(), facesContext.getExternalContext().getResponse() );
This is how it looks the package, which includes the servlet:
Finally, this is the servlet code:
package com.redturtle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.odftoolkit.odfdom.converter.pdf.PdfConverter;
import org.odftoolkit.odfdom.converter.pdf.PdfOptions;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
import fr.opensagres.xdocreport.core.document.SyntaxKind;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import lotus.domino.NotesException;
import lotus.domino.Session;
public class XDocReport {
private static String VERSION = "XDocReport Version 1.0.0";
static PrintWriter outClass = null;
private static Session session = null;
public static void mergeAndPDF (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, InterruptedException, NotesException {
// NO DIIOP
session=DominoAccess.getCurrentSession();
try {
// 1) Load ODT file by filling Velocity template engine
InputStream in = new FileInputStream("/tmp/template.odt");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in,TemplateEngineKind.Velocity);
FieldsMetadata metadata = report.createFieldsMetadata();
metadata.addFieldAsTextStyling("comments",SyntaxKind.Html,false);
// 2) Create context Java model
IContext context = report.createContext();
context.put("comments", "<b>hello world</b>");
// 3) Generate report by merging Java model with the ODT
OutputStream out = new FileOutputStream(new File("/tmp/ODTProjectWithVelocity_Out.odt"));
report.process(context, out);
// 1) Load ODT into ODFDOM OdfTextDocument
in= new FileInputStream(new File("/tmp/ODTProjectWithVelocity_Out.odt"));
OdfTextDocument document = OdfTextDocument.loadDocument(in);
// 2) Prepare Pdf options
PdfOptions options = PdfOptions.create();
// 3) Convert OdfTextDocument to PDF via IText
out = new FileOutputStream(new File("/tmp/ODTProjectWithVelocityList_Out.pdf"));
PdfConverter.getInstance().convert(document, out, options);
// Only manage response to Ajax call
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
outClass = new PrintWriter(response.getWriter());
printAgent("OK");
return;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected static void printAgent(String string) {
outClass.println(string);
}
}
Pay attention to these points:
- comments - variable name inside ODF template
- metadata.addFieldAsTextStyling("comments",SyntaxKind.Html,false); - fill variable with something in HTML syntax
- <b>hello world</b> - HTML string to merge
NOTE: DominoAccess and JSFUtil are two very helpful classes written by Karsten Lehmann: see the full code. Thanks to them, DIIOP process for communication is not required.
Pro & Cons.
I think that it's a very powerful approach; the only limitation I've found is the restricted set of styles supported by the merge process (see there for details). Feel free to make some test using this approach, and let me know what you think :-)