Greats thanks Andreas !
Now I can create a visible document in my publication with my transformer ! But I have another problem...
On Wed, 04 Feb 2009 22:53:32 +0100, Andreas Hartmann
wrote:
> Hi André,
>
> Florent André schrieb:
>
>> And this "marvellous" :p) patchwork of code throw me a beautiful error
> that
>> I don't really understand :( :
>>
>> stacktracejava.lang.RuntimeException: This session [unmodifiable] is not
>> modifiable!
>
> You can only write to the Lenya repository in a transaction. For an
> example how to start a transaction, you could take a look at the
> AbstractUsecase class. Basically you have to create a new modifiable
> session:
>
> Session session = RepositoryUtil.createSession(manager, identity, true);
>
> After your operation is finished, you have to call session.commit(). But
> you have to be aware that this operation will throw a
> ConcurrentModificationException if someone else has modified the same
> resource in the meantime (optimistic offline lock). So you should expect
> this exception and handle it properly.
>
> To avoid this, you'd have to check-out the repository node of the
> document before starting the transaction, but this could lead to blocked
> documents when an error occurs.
Thank for this explanation ! For now, only my transformer can write on reserved document so I don't have this problem... but in a next future I have to solve this problem...
For now, I would like to define the content of my new doc... I try something (mark with --------------- ) that do this :
- remove the default content of the resource but
- don't write the content I want in the document
This is the code I write :
org.apache.cocoon.environment.Session cocoonSession = request.getSession();
// TODO : offert the option to define the identity of the autor
Identity identity = (Identity) cocoonSession.getAttribute(Identity.class.getName());
Session session = RepositoryUtil.createSession(manager, identity, true);
//create the documentFactory
DocumentFactory factory = DocumentUtil.createDocumentFactory(this.manager, session);
//select the publication
Publication pub = factory.getPublication("dev");
DocumentManager docManager = null;
ServiceSelector selector = null;
SiteManager siteManager = null;
ServiceSelector resourceTypeSelector = null;
resourceTypeSelector = (ServiceSelector) this.manager.lookup(
ResourceType.ROLE + "Selector");
//Initialization of document
ResourceType type = (ResourceType) resourceTypeSelector.select("xhtml");
docManager = (DocumentManager) this.manager.lookup(DocumentManager.ROLE);
ResourceType.Sample sample = type.getSample(type.getSampleNames()[0]);
/**TODO : test if the document already exist. If the doc exist this error was thrown :
//stacktraceorg.apache.lenya.cms.publication.DocumentException: The link [/tutorial/tototof:en] is already contained
//in site [org.apache.lenya.cms.site.tree2.sitetreei...@1fdf894] at
*/
org.apache.lenya.cms.publication.Document doc = docManager.add(factory, type, sample.getUri(), pub,
Publication.AUTHORING_AREA,"/tutorial/withcontent", "en", "xml","WithContent",true);
doc.setMimeType(sample.getMimeType());
// End initialization of document
//----------------------------------------------------------------------------------------------------------
//Start : test to write content in document
//exemple from : modules/editors/java/src/org/apache/lenya/cms/editors/EditDocument.java
//@seen : the validation and workflow treatment in the EditDocument.java
//org.w3c.dom.Document xmlDoc = SourceUtil.readDOM(sourceUri, this.manager);
//create the document test content
DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
DocumentBuilder constructeur = fabrique.newDocumentBuilder();
org.w3c.dom.Document xmlDoc = constructeur.newDocument();
// DOM properties
xmlDoc.setXmlVersion("1.0");
xmlDoc.setXmlStandalone(true);
Element root = xmlDoc.createElement("html");
root.appendChild(xmlDoc.createComment("Content of the document"));
Element head = xmlDoc.createElement("head");
root.appendChild(head);
Element titl = xmlDoc.createElement("title");
titl.setTextContent("The title");
head.appendChild(titl);
Element body = xmlDoc.createElement("body");
root.appendChild(body);
Element title = xmlDoc.createElement("h1");
title.setTextContent("Whaou ! My first content ! ");
root.appendChild(title);
Element firstPara = xmlDoc.createElement("p");
firstPara.setTextContent("COOOOOOL !");
root.appendChild(firstPara);
xmlDoc.appendChild(root);
//Write the dom content in the lenya Document
org.apache.lenya.cms.cocoon.source.SourceUtil.writeDOM(xmlDoc, doc.getOutputStream());
//-----------------------------------------------------------------------------------------------------
//Start site management ?? this code is usefull if I use the document
manager ??
SiteStructure site = pub.getArea(Publication.AUTHORING_AREA).getSite();
site.getRepositoryNode().lock();
try {
selector = (ServiceSelector) this.manager.lookup(SiteManager.ROLE + "Selector");
siteManager = (SiteManager) selector.select(pub.getSiteManagerHint());
/// siteManager.add("/tutorial/TOTO", doc);
} finally {
selector.release(siteManager);
this.manager.release(selector);
}
site.getRepositoryNode().unlock();
//Write the document for this session
//TODO : see the Andreas mail and catch the error
session.commit();
Thanks for you help
have a good night
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]