package org.activiti.cycle.impl.service; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.activiti.cycle.BrokenLinkException; import org.activiti.cycle.Content; import org.activiti.cycle.CycleComponentFactory; import org.activiti.cycle.RepositoryArtifact; import org.activiti.cycle.RepositoryArtifactLink; import org.activiti.cycle.RepositoryConnector; import org.activiti.cycle.RepositoryException; import org.activiti.cycle.RepositoryFolder; import org.activiti.cycle.RepositoryNode; import org.activiti.cycle.RepositoryNodeCollection; import org.activiti.cycle.RepositoryNodeNotFoundException; import org.activiti.cycle.action.ParameterizedAction; import org.activiti.cycle.context.CycleSessionContext; import org.activiti.cycle.impl.RepositoryFolderImpl; import org.activiti.cycle.impl.RepositoryNodeCollectionImpl; import org.activiti.cycle.impl.action.fom.FormHandler; import org.activiti.cycle.impl.components.RuntimeConnectorList; import org.activiti.cycle.impl.connector.util.TransactionalConnectorUtils; import org.activiti.cycle.impl.db.CycleLinkDao; import org.activiti.cycle.impl.db.entity.RepositoryArtifactLinkEntity; import org.activiti.cycle.service.CycleRepositoryService; /** * @author bernd.ruecker@camunda.com * @author Nils Preusker (nils.preusker@camunda.com) */ public class CycleRepositoryServiceImpl implements CycleRepositoryService { private CycleServiceConfiguration cycleServiceConfiguration; private CycleLinkDao linkDao; public CycleRepositoryServiceImpl() { } /** * perform initialization after dependencies are set. */ public void initialize() { // perform initialization } public void setCycleServiceConfiguration(CycleServiceConfiguration cycleServiceConfiguration) { this.cycleServiceConfiguration = cycleServiceConfiguration; } public void setLinkDao(CycleLinkDao linkDao) { this.linkDao = linkDao; } // implementation of CycleService methods public boolean login(String username, String password, String connectorId) { RepositoryConnector conn = getRepositoryConnector(connectorId); if (conn != null) { return conn.login(username, password); } return false; } protected List getRuntimeRepositoryConnectors() { RuntimeConnectorList connectorList = CycleSessionContext.get(RuntimeConnectorList.class); return connectorList.getConnectors(); } /** * commit pending changes in all repository connectors configured */ public void commitPendingChanges(String comment) { for (RepositoryConnector connector : getRuntimeRepositoryConnectors()) { TransactionalConnectorUtils.commitTransaction(connector, comment); } } public RepositoryNodeCollection getChildren(String connectorId, String nodeId) { // special handling for root if ("/".equals(connectorId)) { return getRepoRootFolders(); } RepositoryConnector connector = getRepositoryConnector(connectorId); return connector.getChildren(nodeId); } public RepositoryNodeCollection getRepoRootFolders() { ArrayList nodes = new ArrayList(); for (RepositoryConnector connector : getRuntimeRepositoryConnectors()) { RepositoryFolderImpl folder = new RepositoryFolderImpl(connector.getId(), "/"); folder.getMetadata().setName(connector.getName()); folder.getMetadata().setParentFolderId("/"); nodes.add(folder); } return new RepositoryNodeCollectionImpl(nodes); } public RepositoryNode getRepositoryNode(String connectorId, String nodeId) { RepositoryConnector connector = getRepositoryConnector(connectorId); RepositoryNode repositoryArtifact = connector.getRepositoryNode(nodeId); return repositoryArtifact; } public RepositoryArtifact getRepositoryArtifact(String connectorId, String artifactId) { RepositoryConnector connector = getRepositoryConnector(connectorId); RepositoryArtifact repositoryArtifact = connector.getRepositoryArtifact(artifactId); return repositoryArtifact; } public Content getRepositoryArtifactPreview(String connectorId, String artifactId) throws RepositoryNodeNotFoundException { RepositoryConnector connector = getRepositoryConnector(connectorId); return connector.getRepositoryArtifactPreview(artifactId); } public RepositoryFolder getRepositoryFolder(String connectorId, String artifactId) { RepositoryConnector connector = getRepositoryConnector(connectorId); RepositoryFolder repositoryFolder = connector.getRepositoryFolder(artifactId); return repositoryFolder; } public RepositoryArtifact createArtifact(String connectorId, String parentFolderId, String artifactName, String artifactType, Content artifactContent) throws RepositoryNodeNotFoundException { return getRepositoryConnector(connectorId).createArtifact(parentFolderId, artifactName, artifactType, artifactContent); } public RepositoryArtifact createEmptyArtifact(String connectorId, String parentFolderId, String artifactName, String artifactType) { return getRepositoryConnector(connectorId).createEmptyArtifact(parentFolderId, artifactName, artifactType); } public RepositoryArtifact createArtifactFromContentRepresentation(String connectorId, String parentFolderId, String artifactName, String artifactType, String contentRepresentationName, Content artifactContent) throws RepositoryNodeNotFoundException { return getRepositoryConnector(connectorId).createArtifactFromContentRepresentation(parentFolderId, artifactName, artifactType, contentRepresentationName, artifactContent); } public void updateContent(String connectorId, String artifactId, Content content) throws RepositoryNodeNotFoundException { RepositoryConnector connector = getRepositoryConnector(connectorId); connector.updateContent(artifactId, content); } public void updateContent(String connectorId, String artifactId, String contentRepresentationName, Content content) throws RepositoryNodeNotFoundException { RepositoryConnector connector = getRepositoryConnector(artifactId); connector.updateContent(artifactId, contentRepresentationName, content); } public RepositoryFolder createFolder(String connectorId, String parentFolderId, String name) throws RepositoryNodeNotFoundException { return getRepositoryConnector(connectorId).createFolder(parentFolderId, name); } public void deleteArtifact(String connectorId, String artifactId) { getRepositoryConnector(connectorId).deleteArtifact(artifactId); } public void deleteFolder(String connectorId, String folderId) { getRepositoryConnector(connectorId).deleteFolder(folderId); } public Content getContent(String connectorId, String artifactId) throws RepositoryNodeNotFoundException { return getRepositoryConnector(connectorId).getContent(artifactId); } public void executeParameterizedAction(String connectorId, String artifactId, String actionId, Map parameters) throws Exception { ParameterizedAction action = cycleServiceConfiguration.getPluginService().getParameterizedActionById(actionId); String form = action.getFormAsHtml(); FormHandler formHandler = CycleComponentFactory.getCycleComponentInstance(FormHandler.class, FormHandler.class); formHandler.setValues(form, parameters); RepositoryConnector connector = getRepositoryConnector(connectorId); // TODO: (Nils Preusker, 20.10.2010), find a better way to solve this! for (String key : parameters.keySet()) { if (key.equals("targetConnectorId")) { if (parameters.get(key) != null && parameters.get(key) instanceof String) { RepositoryConnector targetConnector = getRepositoryConnector((String) parameters.get(key)); parameters.put(key, targetConnector); } } } connector.executeParameterizedAction(artifactId, actionId, parameters); } public String getActionFormTemplate(String connectorId, String artifactId, String actionId) { // Retrieve the artifact from the repository RepositoryArtifact artifact = getRepositoryArtifact(connectorId, artifactId); // Retrieve the action and its form String form = null; for (ParameterizedAction action : cycleServiceConfiguration.getPluginService().getParameterizedActions(artifact)) { if (action.getId().equals(actionId)) { form = action.getFormAsHtml(); break; } } // evaluate expressions in the form using the FormHandler. FormHandler formHandler = CycleComponentFactory.getCycleComponentInstance(FormHandler.class, FormHandler.class); form = formHandler.parseForm(form); return form; } // public List getSupportedArtifactTypes(String connectorId, // String folderId) { // if (folderId == null || folderId.length() <= 1) { // // "virtual" root folder doesn't support any artifact types // return new ArrayList(); // } // return // getRepositoryConnector(connectorId).getSupportedArtifactTypes(folderId); // } // RepositoryArtifactLink specific methods public void addArtifactLink(RepositoryArtifactLink repositoryArtifactLink) { if (repositoryArtifactLink instanceof RepositoryArtifactLinkEntity) { linkDao.insertArtifactLink((RepositoryArtifactLinkEntity) repositoryArtifactLink); } else { RepositoryArtifactLinkEntity cycleLink = new RepositoryArtifactLinkEntity(); cycleLink.setId(repositoryArtifactLink.getId()); // set source artifact attributes cycleLink.setSourceConnectorId(repositoryArtifactLink.getSourceArtifact().getConnectorId()); cycleLink.setSourceArtifactId(repositoryArtifactLink.getSourceArtifact().getNodeId()); cycleLink.setSourceElementId(repositoryArtifactLink.getSourceElementId()); cycleLink.setSourceElementName(repositoryArtifactLink.getSourceElementName()); // cycleLink.setSourceRevision(repositoryArtifactLink.getSourceArtifact().getArtifactType().getRevision()); // set target artifact attributes cycleLink.setTargetConnectorId(repositoryArtifactLink.getTargetArtifact().getConnectorId()); cycleLink.setTargetArtifactId(repositoryArtifactLink.getTargetArtifact().getNodeId()); cycleLink.setTargetElementId(repositoryArtifactLink.getTargetElementId()); cycleLink.setTargetElementName(repositoryArtifactLink.getTargetElementName()); // cycleLink.setTargetRevision(repositoryArtifactLink.getTargetArtifact().getArtifactType().getRevision()); cycleLink.setLinkType(repositoryArtifactLink.getLinkType()); cycleLink.setComment(repositoryArtifactLink.getComment()); cycleLink.setLinkedBothWays(false); linkDao.insertArtifactLink(cycleLink); } } public List getArtifactLinks(String sourceConnectorId, String sourceArtifactId) { List artifactLinks = new ArrayList(); List linkResultList = linkDao.getOutgoingArtifactLinks(sourceConnectorId, sourceArtifactId); for (RepositoryArtifactLinkEntity entity : linkResultList) { try { entity.resolveArtifacts(this); artifactLinks.add(entity); } catch (BrokenLinkException e) { // ignoring this link } } return artifactLinks; } public List getIncomingArtifactLinks(String targetConnectorId, String targetArtifactId) { List artifactLinks = new ArrayList(); List linkResultList = linkDao.getIncomingArtifactLinks(targetConnectorId, targetArtifactId); for (RepositoryArtifactLinkEntity entity : linkResultList) { try { entity.resolveArtifacts(this); artifactLinks.add(entity); } catch (BrokenLinkException e) { // ignoring this link } } return artifactLinks; } public void deleteLink(String linkId) { linkDao.deleteArtifactLink(linkId); } private RepositoryConnector getRepositoryConnector(String connectorId) { RuntimeConnectorList list = CycleSessionContext.get(RuntimeConnectorList.class); RepositoryConnector connector = list.getConnectorById(connectorId); if (connector == null) { throw new RepositoryException("Couldn't find Repository Connector with id '" + connectorId + "'"); } return connector; } }