1 17 18 19 20 package org.apache.lenya.cms.cocoon.flow; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.Map ; 26 27 import org.apache.cocoon.components.flow.javascript.fom.FOM_Cocoon; 28 import org.apache.cocoon.environment.ObjectModelHelper; 29 import org.apache.cocoon.environment.Request; 30 import org.apache.cocoon.environment.Session; 31 import org.apache.lenya.ac.AccessControlException; 32 import org.apache.lenya.ac.Identity; 33 import org.apache.lenya.ac.Machine; 34 import org.apache.lenya.ac.Role; 35 import org.apache.lenya.ac.User; 36 import org.apache.lenya.ac.impl.PolicyAuthorizer; 37 import org.apache.lenya.cms.publication.DocumentHelper; 38 import org.apache.lenya.cms.publication.PageEnvelope; 39 import org.apache.lenya.cms.publication.PageEnvelopeException; 40 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 41 import org.apache.lenya.cms.publication.Publication; 42 import org.apache.lenya.cms.rc.FileReservedCheckInException; 43 import org.apache.lenya.cms.rc.RCEnvironment; 44 import org.apache.lenya.cms.rc.RevisionController; 45 import org.apache.lenya.cms.workflow.WorkflowDocument; 46 import org.apache.lenya.cms.workflow.WorkflowFactory; 47 import org.apache.lenya.workflow.Situation; 48 import org.apache.lenya.workflow.WorkflowException; 49 import org.apache.log4j.Category; 50 51 57 public class FlowHelper { 58 59 private static final Category log = Category.getInstance(FlowHelper.class); 60 61 64 public FlowHelper() { 65 } 66 67 73 public Situation getSituation(FOM_Cocoon cocoon) throws AccessControlException { 74 Request request = ObjectModelHelper.getRequest(cocoon.getObjectModel()); 75 Session session = request.getSession(); 76 Identity identity = (Identity) session.getAttribute(Identity.class.getName()); 77 78 String userId = ""; 79 String ipAddress = ""; 80 81 User user = identity.getUser(); 82 if (user != null) { 83 userId = user.getId(); 84 } 85 86 Machine machine = identity.getMachine(); 87 if (machine != null) { 88 ipAddress = machine.getIp(); 89 } 90 91 Role[] roles = PolicyAuthorizer.getRoles(request); 92 String [] roleIds = new String [roles.length]; 93 for (int i = 0; i < roles.length; i++) { 94 roleIds[i] = roles[i].getId(); 95 } 96 97 WorkflowFactory factory = WorkflowFactory.newInstance(); 98 Situation situation = factory.buildSituation(roleIds, userId, ipAddress); 99 return situation; 100 } 101 102 108 public PageEnvelope getPageEnvelope(FOM_Cocoon cocoon) throws PageEnvelopeException { 109 PageEnvelopeFactory factory = PageEnvelopeFactory.getInstance(); 110 return factory.getPageEnvelope(cocoon.getObjectModel()); 111 } 112 113 118 public String getRequestURI(FOM_Cocoon cocoon) { 119 return cocoon.getRequest().getRequestURI(); 120 } 121 122 127 public Request getRequest(FOM_Cocoon cocoon) { 128 return cocoon.getRequest(); 129 } 130 131 136 public Map getObjectModel(FOM_Cocoon cocoon) { 137 return cocoon.getObjectModel(); 138 } 139 140 146 public DocumentHelper getDocumentHelper(FOM_Cocoon cocoon) { 147 return new DocumentHelper(cocoon.getObjectModel()); 148 } 149 150 public static final String SEPARATOR = ":"; 151 152 161 public String getImageParameterValue(FOM_Cocoon cocoon, String parameterName) { 162 163 log.debug("Resolving parameter value for name [" + parameterName + "]"); 164 165 Request request = cocoon.getRequest(); 166 String value = request.getParameter(parameterName); 167 168 if (value == null) { 169 String prefix = parameterName + SEPARATOR; 170 Enumeration e = request.getParameterNames(); 171 while (e.hasMoreElements() && value == null) { 172 String name = (String ) e.nextElement(); 173 if (name.startsWith(prefix)) { 174 log.debug("Complete parameter name: [" + name + "]"); 175 value = name.substring(prefix.length(), name.length() - 2); 176 log.debug("Resolved value: [" + value + "]"); 177 } 178 } 179 } 180 181 return value; 182 } 183 184 192 public void triggerWorkflow(FOM_Cocoon cocoon, String event) 193 throws WorkflowException, PageEnvelopeException, AccessControlException { 194 final WorkflowDocument wf = (WorkflowDocument)WorkflowFactory.newInstance().buildInstance(getPageEnvelope(cocoon).getDocument()); 195 wf.invoke(getSituation(cocoon), event); 196 } 197 198 206 public RevisionController getRevisionController(FOM_Cocoon cocoon) 207 throws PageEnvelopeException, IOException { 208 final Publication publication = getPageEnvelope(cocoon).getPublication(); 209 final String publicationPath = publication.getDirectory().getAbsolutePath(); 210 final RCEnvironment rcEnvironment = RCEnvironment.getInstance(publication.getServletContext().getAbsolutePath()); 211 String rcmlDirectory = rcEnvironment.getRCMLDirectory(); 212 rcmlDirectory = publicationPath + File.separator + rcmlDirectory; 213 String backupDirectory = rcEnvironment.getBackupDirectory(); 214 backupDirectory = publicationPath + File.separator + backupDirectory; 215 216 return new RevisionController(rcmlDirectory, backupDirectory, publicationPath); 217 } 218 219 227 public void reservedCheckIn(FOM_Cocoon cocoon, boolean backup) 228 throws FileReservedCheckInException, Exception 229 { 230 final Identity identity = (Identity) ObjectModelHelper.getRequest(cocoon.getObjectModel()).getSession().getAttribute(Identity.class.getName()); 231 final PageEnvelope pageEnvelope = getPageEnvelope(cocoon); 232 final Publication publication = getPageEnvelope(cocoon).getPublication(); 233 final String filename = pageEnvelope.getDocument().getFile().getAbsolutePath().substring(publication.getDirectory().getAbsolutePath().length()); 234 getRevisionController(cocoon).reservedCheckIn(filename, identity.getUser().getId(), backup); 235 } 236 } 237 | Popular Tags |