List of usage examples for org.dom4j Document getRootElement
Element getRootElement();
From source file:com.collabnet.ccf.core.transformer.XsltProcessor.java
License:Open Source License
public Transformer constructFileNameAndFetchTransformer(Document record) throws GenericArtifactParsingException { String fileName = null;/* w ww. ja v a 2 s. c om*/ Transformer transform = null; // this branch is only used if the xslt dir is set, otherwise, the xslt // file will be used if (!StringUtils.isEmpty(this.xsltDir)) { Element element = XPathUtils.getRootElement(record); String sourceSystemId = XPathUtils.getAttributeValue(element, SOURCE_SYSTEM_ID); String targetSystemId = XPathUtils.getAttributeValue(element, TARGET_SYSTEM_ID); String sourceRepositoryId = XPathUtils.getAttributeValue(element, SOURCE_REPOSITORY_ID); String targetRepositoryId = XPathUtils.getAttributeValue(element, TARGET_REPOSITORY_ID); sourceRepositoryId = CCFUtils.getTempFileName(sourceRepositoryId); targetRepositoryId = CCFUtils.getTempFileName(targetRepositoryId); String xsltDir = this.xsltDir; fileName = xsltDir + sourceSystemId + PARAM_DELIMITER + sourceRepositoryId + PARAM_DELIMITER + targetSystemId + PARAM_DELIMITER + targetRepositoryId + ".xsl"; } else if (!StringUtils.isEmpty(this.xsltFile)) { fileName = this.xsltFile; } transform = xsltFileNameTransformerMap.get(fileName); if (transform == null) { transform = loadXSLT(new File(fileName), record.getRootElement()); xsltFileNameTransformerMap.put(fileName, transform); } return transform; }
From source file:com.collabnet.ccf.core.transformer.XsltProcessor.java
License:Open Source License
/** * Apply the transform to the record. The record can be either a XML string * or a dom4j document object//from ww w . j av a 2 s .c o m * * @param record * the message record * * @return a String[] with one String resulting from the transform * * @throws ProcessingException * if the record type is not supported */ public Object[] process(Object record) throws ProcessingException { if (record == null) return null; Document document = null; Element element = null; if (record instanceof Document) { Transformer transform = null; try { document = (Document) record; element = document.getRootElement(); try { String artifactAction = XPathUtils.getAttributeValue(element, GenericArtifactHelper.ARTIFACT_ACTION); String transactionId = XPathUtils.getAttributeValue(element, GenericArtifactHelper.TRANSACTION_ID); String errorCode = XPathUtils.getAttributeValue(element, GenericArtifactHelper.ERROR_CODE); // pass artifacts with ignore action if (artifactAction != null && artifactAction.equals(GenericArtifactHelper.ARTIFACT_ACTION_IGNORE)) { return new Object[] { document }; } // do not transform artifacts to be replayed (unless specific error code is set) if (transactionId != null && !transactionId.equals(GenericArtifact.VALUE_UNKNOWN)) { if (errorCode == null || !errorCode.equals(GenericArtifact.ERROR_REPLAYED_WITH_TRANSFORMATION)) { return new Object[] { document }; } } } catch (GenericArtifactParsingException e) { // do nothing, this artifact does not seem to be a generic // artifact } transform = constructFileNameAndFetchTransformer(document); } catch (GenericArtifactParsingException e) { String cause = "Problem occured while parsing the Document to contruct the file name and fetching transformer"; log.error(cause, e); XPathUtils.addAttribute(element, GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_GENERIC_ARTIFACT_PARSING); throw new CCFRuntimeException(cause, e); } return transform((Document) record, transform, element); } // if we get this far then we cannot process the record String cause = "Invalid record (type: " + record.getClass().toString() + "). Cannot apply transform"; log.error(cause); throw new CCFRuntimeException(cause); }
From source file:com.collabnet.ccf.core.utils.CCFUtils.java
License:Open Source License
public static String getTempFileName(Document document) throws GenericArtifactParsingException { Element element = document.getRootElement(); String sourceSystemId = XPathUtils.getAttributeValue(element, GenericArtifactHelper.SOURCE_SYSTEM_ID); String sourceRepositoryId = XPathUtils.getAttributeValue(element, GenericArtifactHelper.SOURCE_REPOSITORY_ID); String targetSystemId = XPathUtils.getAttributeValue(element, GenericArtifactHelper.TARGET_SYSTEM_ID); String targetRepositoryId = XPathUtils.getAttributeValue(element, GenericArtifactHelper.TARGET_REPOSITORY_ID); String repositoryKey = sourceSystemId + delimiter + sourceRepositoryId + delimiter + targetSystemId + delimiter + targetRepositoryId; for (String replaceCharacter : replaceCharacters) { repositoryKey = repositoryKey.replaceAll(replaceCharacter, delimiter); }//from ww w . j a va 2 s . c om return repositoryKey; }
From source file:com.collabnet.ccf.core.utils.XPathUtils.java
License:Open Source License
/** * Extracts the artifact-root-element out of a Dom4J XML document * /*from w w w .j a v a 2 s . com*/ * @param document * XML document in question * @return generic artifact root-element * @throws GenericArtifactParsingException * thrown if document is not compliant to the generic artifact * schema */ public static Element getRootElement(Document document) throws GenericArtifactParsingException { Element rootElement = document.getRootElement(); if (rootElement == null) { throw new GenericArtifactParsingException( "Received empty XML document. The reason might be an invalid data mapping."); } if (!ARTIFACT_ROOT_ELEMENT_NAME.equals(rootElement.getName())) throw new GenericArtifactParsingException("Root-element of XML document is not named " + ARTIFACT_ROOT_ELEMENT_NAME + "but " + rootElement.getName()); if (!CCF_ARTIFACT_NAMESPACE.equals(rootElement.getNamespaceURI())) throw new GenericArtifactParsingException("Namespace-URI of root-element of XML document is not named " + CCF_ARTIFACT_NAMESPACE + "but " + rootElement.getNamespaceURI()); return rootElement; }
From source file:com.collabnet.ccf.pi.qc.v90.QCWriter.java
License:Open Source License
@Override public boolean handleException(Throwable rootCause, ConnectionManager<IConnection> connectionManager, Document ga) { if (rootCause == null) return false; if (rootCause instanceof ConnectionException) { Throwable cause = rootCause.getCause(); handleException(cause, connectionManager, ga); if (connectionManager.isEnableRetryAfterNetworkTimeout()) { return true; }/* w w w . j a v a 2 s . c om*/ } else if (rootCause instanceof com.jacob.com.ComFailException) { com.jacob.com.ComFailException comEx = (com.jacob.com.ComFailException) rootCause; String message = comEx.getMessage(); boolean connectionErrorOccured = false; if (message.contains("Server is not available")) { connectionErrorOccured = true; } else if (message.contains("Your Quality Center session has been disconnected")) { connectionErrorOccured = true; this.reInitCOM(); } else if (message.contains("The Object is locked by")) { if (!immediatelyQuarantineLockedDefects) { connectionErrorOccured = true; } else { // set new error code ga.getRootElement().addAttribute(GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_OBJECT_LOCKED); } } else if (message.contains("The server threw an exception.")) { connectionErrorOccured = true; this.reInitCOM(); } else if (message.contains("Session authenticity broken")) { connectionErrorOccured = true; this.reInitCOM(); } else if (message.contains("Server has been disconnected")) { connectionErrorOccured = true; this.reInitCOM(); } else if (message.contains("Project is not connected")) { connectionErrorOccured = true; this.reInitCOM(); } else if (message.contains("You do not have the required permissions to execute this action.") && !isQuarantineIfQCReportsPermissionDenied()) { connectionErrorOccured = true; this.reInitCOM(); } else if (message.contains("Failed to Run Query")) { connectionErrorOccured = true; this.reInitCOM(); } else if (message.contains("Failed to Check Out") || message.contains("Failed to lock REQ entity")) { // set new error code ga.getRootElement().addAttribute(GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_OBJECT_CHECKED_OUT); } else if (message.contains("Failed to Connect Project")) { log.warn("The QC Project might have been de-activated. Please activate this project"); connectionErrorOccured = true; this.reInitCOM(); } if (connectionManager.isEnableRetryAfterNetworkTimeout() && connectionErrorOccured) { return true; } } else if (rootCause instanceof DefectAlreadyLockedException) { if (!immediatelyQuarantineLockedDefects) { return true; } else { // set new error code ga.getRootElement().addAttribute(GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_OBJECT_LOCKED); } } else if (rootCause instanceof CCFRuntimeException) { Throwable cause = rootCause.getCause(); return handleException(cause, connectionManager, ga); } return false; }
From source file:com.collabnet.ccf.pi.sfee.v44.SFEEWriter.java
License:Open Source License
public Document createArtifact(Document data) { GenericArtifact ga = null;//from ww w. j a va 2s. c om try { ga = GenericArtifactHelper.createGenericArtifactJavaObject(data); } catch (GenericArtifactParsingException e) { String cause = "Problem occured while parsing the GenericArtifact into Document"; log.error(cause, e); XPathUtils.addAttribute(data.getRootElement(), GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_GENERIC_ARTIFACT_PARSING); throw new CCFRuntimeException(cause, e); } this.initializeArtifact(ga); String targetRepositoryId = ga.getTargetRepositoryId(); String targetArtifactId = ga.getTargetArtifactId(); String tracker = targetRepositoryId; Connection connection = connect(ga); ArtifactSoapDO result = null; try { result = this.createArtifact(ga, tracker, connection); // update Id field after creating the artifact targetArtifactId = result.getId(); SFEEGAHelper.addField(ga, ArtifactMetaData.SFEEFields.id.getFieldName(), targetArtifactId, GenericArtifactField.VALUE_FIELD_TYPE_MANDATORY_FIELD, GenericArtifactField.FieldValueTypeValue.STRING); } catch (NumberFormatException e) { log.error("Wrong data format of attribute for artifact " + data.asXML(), e); return null; } finally { disconnect(connection); } if (result != null) { this.populateTargetArtifactAttributes(ga, result); } return this.returnDocument(ga); }
From source file:com.collabnet.ccf.pi.sfee.v44.SFEEWriter.java
License:Open Source License
@Override public Document[] deleteAttachment(Document gaDocument) { GenericArtifact ga = null;/*from ww w .j av a 2s.co m*/ try { ga = GenericArtifactHelper.createGenericArtifactJavaObject(gaDocument); } catch (GenericArtifactParsingException e) { String cause = "Problem occured while parsing the GenericArtifact into Document"; log.error(cause, e); XPathUtils.addAttribute(gaDocument.getRootElement(), GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_GENERIC_ARTIFACT_PARSING); throw new CCFRuntimeException(cause, e); } this.initializeArtifact(ga); // String targetRepositoryId = ga.getTargetRepositoryId(); String targetArtifactId = ga.getTargetArtifactId(); String artifactId = ga.getDepParentTargetArtifactId(); // String tracker = targetRepositoryId; Connection connection = null; GenericArtifact parentArtifact = null; try { connection = connect(ga); attachmentHandler.deleteAttachment(connection, targetArtifactId, artifactId, ga); log.info("Attachment " + targetArtifactId + "is deleted"); ArtifactSoapDO artifact = trackerHandler.getTrackerItem(connection.getSessionId(), artifactId); parentArtifact = new GenericArtifact(); parentArtifact.setArtifactType(GenericArtifact.ArtifactTypeValue.PLAINARTIFACT); parentArtifact.setArtifactAction(GenericArtifact.ArtifactActionValue.UPDATE); parentArtifact.setArtifactMode(GenericArtifact.ArtifactModeValue.CHANGEDFIELDSONLY); parentArtifact.setConflictResolutionPriority(ga.getConflictResolutionPriority()); parentArtifact.setSourceArtifactId(ga.getDepParentSourceArtifactId()); parentArtifact.setSourceArtifactLastModifiedDate(ga.getSourceArtifactLastModifiedDate()); parentArtifact.setSourceArtifactVersion(ga.getSourceArtifactVersion()); parentArtifact.setSourceRepositoryId(ga.getSourceRepositoryId()); parentArtifact.setSourceSystemId(ga.getSourceSystemId()); parentArtifact.setSourceSystemKind(ga.getSourceSystemKind()); parentArtifact.setSourceRepositoryKind(ga.getSourceRepositoryKind()); parentArtifact.setSourceSystemTimezone(ga.getSourceSystemTimezone()); parentArtifact.setTargetArtifactId(artifactId); parentArtifact.setTargetArtifactLastModifiedDate(DateUtil.format(artifact.getLastModifiedDate())); parentArtifact.setTargetArtifactVersion(Integer.toString(artifact.getVersion())); parentArtifact.setTargetRepositoryId(ga.getTargetRepositoryId()); parentArtifact.setTargetRepositoryKind(ga.getTargetRepositoryKind()); parentArtifact.setTargetSystemId(ga.getTargetSystemId()); parentArtifact.setTargetSystemKind(ga.getTargetSystemKind()); parentArtifact.setTargetSystemTimezone(ga.getTargetSystemTimezone()); } catch (RemoteException e) { String message = "Exception while deleting attachment " + artifactId; log.error(message, e); throw new CCFRuntimeException(message, e); } finally { if (connection != null) { this.disconnect(connection); } } Document returnDocument = null; Document returnParentDocument = null; try { returnDocument = GenericArtifactHelper.createGenericArtifactXMLDocument(ga); returnParentDocument = GenericArtifactHelper.createGenericArtifactXMLDocument(parentArtifact); } catch (GenericArtifactParsingException e) { String message = "Exception while deleting attachment " + artifactId + ". Could not parse Generic artifact"; log.error(message, e); throw new CCFRuntimeException(message, e); } return new Document[] { returnDocument, returnParentDocument }; }
From source file:com.collabnet.ccf.pi.sfee.v44.SFEEWriter.java
License:Open Source License
@Override public Document updateArtifact(Document data) { GenericArtifact ga = null;// w ww . j ava 2 s .c o m try { ga = GenericArtifactHelper.createGenericArtifactJavaObject(data); } catch (GenericArtifactParsingException e) { String cause = "Problem occured while parsing the GenericArtifact into Document"; log.error(cause, e); XPathUtils.addAttribute(data.getRootElement(), GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_GENERIC_ARTIFACT_PARSING); throw new CCFRuntimeException(cause, e); } this.initializeArtifact(ga); String targetRepositoryId = ga.getTargetRepositoryId(); String tracker = targetRepositoryId; Connection connection = connect(ga); ArtifactSoapDO result = null; try { // update and do conflict resolution result = this.updateArtifact(ga, tracker, connection); } catch (NumberFormatException e) { String cause = "Number format exception while trying to extract the field data."; log.error(cause, e); XPathUtils.addAttribute(data.getRootElement(), GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_GENERIC_ARTIFACT_PARSING); throw new CCFRuntimeException(cause, e); } finally { disconnect(connection); } // otherwise a conflict has happened and the generic artifact has been // already prepared if (result != null) { this.populateTargetArtifactAttributes(ga, result); } return this.returnDocument(ga); }
From source file:com.collabnet.ccf.teamforge.TFWriter.java
License:Open Source License
public Document createArtifact(Document data) { GenericArtifact ga = null;/* w w w. ja va 2 s . c o m*/ try { ga = GenericArtifactHelper.createGenericArtifactJavaObject(data); } catch (GenericArtifactParsingException e) { String cause = "Problem occured while parsing the GenericArtifact into Document"; log.error(cause, e); XPathUtils.addAttribute(data.getRootElement(), GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_GENERIC_ARTIFACT_PARSING); throw new CCFRuntimeException(cause, e); } String targetRepositoryId = ga.getTargetRepositoryId(); String targetArtifactId = ga.getTargetArtifactId(); String tracker = targetRepositoryId; Connection connection = connect(ga); try { if (TFConnectionFactory.isTrackerRepository(targetRepositoryId)) { ArtifactDO result = null; try { this.initializeArtifact(ga); result = this.createArtifact(ga, tracker, connection); // update Id field after creating the artifact targetArtifactId = result.getId(); TFGAHelper.addField(ga, TFArtifactMetaData.TFFields.id.getFieldName(), targetArtifactId, GenericArtifactField.VALUE_FIELD_TYPE_MANDATORY_FIELD, GenericArtifactField.FieldValueTypeValue.STRING); this.populateTargetArtifactAttributes(ga, result); return this.returnDocument(ga); } catch (NumberFormatException e) { log.error("Wrong data format of attribute for artifact " + data.asXML(), e); return null; } } else if (TFConnectionFactory.isPlanningFolderRepository(targetRepositoryId)) { // we write planning folders, so first do a check whether we // support this feature if (!connection.supports53()) { // we do not support planning folders throw new CCFRuntimeException( "Planning Folders are not supported since TeamForge target system does not provide them."); } PlanningFolderDO result = null; String project = TFConnectionFactory.extractProjectFromRepositoryId(targetRepositoryId); result = createPlanningFolder(ga, project, connection); targetArtifactId = result.getId(); TFGAHelper.addField(ga, TFArtifactMetaData.TFFields.id.getFieldName(), targetArtifactId, GenericArtifactField.VALUE_FIELD_TYPE_MANDATORY_FIELD, GenericArtifactField.FieldValueTypeValue.STRING); populateTargetArtifactAttributesFromPlanningFolder(ga, result); return returnDocument(ga); } else if (TFConnectionFactory.isTrackerMetaDataRepository(targetRepositoryId)) { TrackerDO result = null; String trackerId = TFConnectionFactory.extractTrackerFromMetaDataRepositoryId(targetRepositoryId); result = updateTrackerMetaData(ga, trackerId, connection); populateTargetArtifactAttributesFromTracker(ga, result); return returnDocument(ga); } else { throw new CCFRuntimeException("Unknown repository id format: " + targetRepositoryId); } } finally { disconnect(connection); } }
From source file:com.collabnet.ccf.teamforge.TFWriter.java
License:Open Source License
@Override public Document deleteArtifact(Document gaDocument) { GenericArtifact ga = null;/*from w w w . j a v a2s. co m*/ try { ga = GenericArtifactHelper.createGenericArtifactJavaObject(gaDocument); } catch (GenericArtifactParsingException e) { String cause = "Problem occured while parsing the GenericArtifact into Document"; log.error(cause, e); XPathUtils.addAttribute(gaDocument.getRootElement(), GenericArtifactHelper.ERROR_CODE, GenericArtifact.ERROR_GENERIC_ARTIFACT_PARSING); throw new CCFRuntimeException(cause, e); } // now we have to figure out whether we have to delete a planning folder // or a tracker item String targetRepositoryId = ga.getTargetRepositoryId(); String targetArtifactId = ga.getTargetArtifactId(); Connection connection = null; GenericArtifact deletedArtifact = new GenericArtifact(); // make sure that we do not update the synchronization status record for // replayed attachments deletedArtifact.setTransactionId(ga.getTransactionId()); deletedArtifact.setArtifactType(GenericArtifact.ArtifactTypeValue.PLAINARTIFACT); deletedArtifact.setArtifactAction(GenericArtifact.ArtifactActionValue.DELETE); deletedArtifact.setArtifactMode(GenericArtifact.ArtifactModeValue.CHANGEDFIELDSONLY); deletedArtifact.setConflictResolutionPriority(ga.getConflictResolutionPriority()); deletedArtifact.setSourceArtifactId(ga.getSourceArtifactId()); deletedArtifact.setSourceArtifactLastModifiedDate(ga.getSourceArtifactLastModifiedDate()); deletedArtifact.setSourceArtifactVersion(ga.getSourceArtifactVersion()); deletedArtifact.setSourceRepositoryId(ga.getSourceRepositoryId()); deletedArtifact.setSourceSystemId(ga.getSourceSystemId()); deletedArtifact.setSourceSystemKind(ga.getSourceSystemKind()); deletedArtifact.setSourceRepositoryKind(ga.getSourceRepositoryKind()); deletedArtifact.setSourceSystemTimezone(ga.getSourceSystemTimezone()); deletedArtifact.setTargetArtifactId(targetArtifactId); deletedArtifact.setTargetRepositoryId(ga.getTargetRepositoryId()); deletedArtifact.setTargetRepositoryKind(ga.getTargetRepositoryKind()); deletedArtifact.setTargetSystemId(ga.getTargetSystemId()); deletedArtifact.setTargetSystemKind(ga.getTargetSystemKind()); deletedArtifact.setTargetSystemTimezone(ga.getTargetSystemTimezone()); try { connection = connect(ga); // now we have to figure out whether we have to delete a planning // folder or a tracker item if (TFConnectionFactory.isTrackerRepository(targetRepositoryId)) { ArtifactDO artifact = null; try { artifact = trackerHandler.getTrackerItem(connection, targetArtifactId); } catch (AxisFault e) { javax.xml.namespace.QName faultCode = e.getFaultCode(); if (faultCode.getLocalPart().equals("NoSuchObjectFault")) { log.warn("Artifact " + targetArtifactId + " does not exist any more!"); } else { throw e; } } if (artifact != null) { deletedArtifact .setTargetArtifactLastModifiedDate(DateUtil.format(artifact.getLastModifiedDate())); deletedArtifact.setTargetArtifactVersion(Integer.toString(artifact.getVersion())); try { trackerHandler.removeArtifact(connection, targetArtifactId); } catch (AxisFault e) { javax.xml.namespace.QName faultCode = e.getFaultCode(); if (faultCode.getLocalPart().equals("NoSuchObjectFault")) { log.warn("Artifact " + targetArtifactId + " does not exist any more!"); } else { throw e; } } log.info("Artifact " + targetArtifactId + " is deleted successfully."); } } else if (TFConnectionFactory.isPlanningFolderRepository(targetRepositoryId)) { PlanningFolderDO artifact = null; try { artifact = connection.getPlanningClient().getPlanningFolderData(targetArtifactId); } catch (AxisFault e) { javax.xml.namespace.QName faultCode = e.getFaultCode(); if (faultCode.getLocalPart().equals("NoSuchObjectFault")) { log.warn("Artifact " + targetArtifactId + " does not exist any more!"); } else { throw e; } } if (artifact != null) { deletedArtifact .setTargetArtifactLastModifiedDate(DateUtil.format(artifact.getLastModifiedDate())); deletedArtifact.setTargetArtifactVersion(Integer.toString(artifact.getVersion())); try { connection.getPlanningClient().deletePlanningFolder(targetArtifactId); } catch (AxisFault e) { javax.xml.namespace.QName faultCode = e.getFaultCode(); if (faultCode.getLocalPart().equals("NoSuchObjectFault")) { log.warn("Artifact " + targetArtifactId + " does not exist any more!"); } else { throw e; } } log.info("Artifact " + targetArtifactId + " is deleted successfully."); } } else { log.warn("deleteArtifact is not implemented for repository " + targetRepositoryId); } } catch (RemoteException e) { String message = "Exception while deleting artifact " + targetArtifactId; log.error(message, e); throw new CCFRuntimeException(message, e); } finally { if (connection != null) { this.disconnect(connection); } } Document returnDocument = null; try { returnDocument = GenericArtifactHelper.createGenericArtifactXMLDocument(deletedArtifact); } catch (GenericArtifactParsingException e) { String message = "Exception while deleting artifact " + targetArtifactId + ". Could not parse Generic artifact"; log.error(message, e); throw new CCFRuntimeException(message, e); } return returnDocument; }