/**
  * Method useWorkspaceResource.
  *
  * @param aVersion R4EFileVersion
  * @return boolean
  */
 public static boolean useWorkspaceResource(R4EFileVersion aVersion) {
   // Get handle to local storage repository
   try {
     if (null != R4EUIModelController.getActiveReview()) {
       final IRFSRegistry localRepository =
           RFSRegistryFactory.getRegistry(R4EUIModelController.getActiveReview().getReview());
       if (null != localRepository) {
         // If resource is available in the workspace, use it.  Otherwise use the local repo
         // version
         if ((null != aVersion) && (null != aVersion.getResource())) {
           final String workspaceFileId =
               localRepository.blobIdFor(((IFile) aVersion.getResource()).getContents());
           final String repoFileId = aVersion.getLocalVersionID();
           if ((null != workspaceFileId) && workspaceFileId.equals((repoFileId))) {
             return true;
           }
         }
       }
     }
   } catch (ReviewsFileStorageException e) {
     R4EUIPlugin.Ftracer.traceWarning("Exception: " + e.toString() + " (" + e.getMessage() + ")");
   } catch (CoreException e) {
     R4EUIPlugin.Ftracer.traceWarning("Exception: " + e.toString() + " (" + e.getMessage() + ")");
   }
   return false;
 }
 /**
  * Method updateFileVersion
  *
  * @param aTargetFileVer R4EFileVersion
  * @param aSrcFile IFile
  */
 public static void updateFileVersion(R4EFileVersion aTargetFileVer, IFile aSrcFile) {
   if ((null != aTargetFileVer) && (null != aSrcFile)) {
     aTargetFileVer.setName(aSrcFile.getName());
     aTargetFileVer.setRepositoryPath(
         ""); // No repositories for workspace files since they are not in source control
     aTargetFileVer.setResource(aSrcFile);
     aTargetFileVer.setPlatformURI(ResourceUtils.toPlatformURIStr(aSrcFile));
   }
 }
 /**
  *
  * <!-- begin-user-doc -->
  * <!-- end-user-doc -->
  *
  * @generated
  */
 public R4EFileVersion getFile() {
   if (file != null && file.eIsProxy()) {
     InternalEObject oldFile = (InternalEObject) file;
     file = (R4EFileVersion) eResolveProxy(oldFile);
     if (file != oldFile) {
       InternalEObject newFile = (InternalEObject) file;
       NotificationChain msgs =
           oldFile.eInverseRemove(
               this,
               EOPPOSITE_FEATURE_BASE - RModelPackage.R4E_ANOMALY_TEXT_POSITION__FILE,
               null,
               null);
       if (newFile.eInternalContainer() == null) {
         msgs =
             newFile.eInverseAdd(
                 this,
                 EOPPOSITE_FEATURE_BASE - RModelPackage.R4E_ANOMALY_TEXT_POSITION__FILE,
                 null,
                 msgs);
       }
       if (msgs != null) msgs.dispatch();
       if (eNotificationRequired())
         eNotify(
             new ENotificationImpl(
                 this,
                 Notification.RESOLVE,
                 RModelPackage.R4E_ANOMALY_TEXT_POSITION__FILE,
                 oldFile,
                 file));
     }
   }
   return file;
 }
  /**
   * Method copyWorkspaceFileToLocalRepository.
   *
   * @param aLocalRepository IRFSRegistry
   * @param aFile IFile
   * @return IFile
   * @throws CoreException
   * @throws ReviewsFileStorageException
   */
  public static R4EFileVersion copyWorkspaceFileToLocalRepository(
      IRFSRegistry aLocalRepository, IFile aFile)
      throws CoreException, ReviewsFileStorageException {
    R4EFileVersion tmpFileVersion = null;
    if ((null != aLocalRepository) && (null != aFile)) {
      // Create and Set value in temporary File version
      tmpFileVersion = RModelFactory.eINSTANCE.createR4EFileVersion();
      if ((null != tmpFileVersion)) {
        updateFileVersion(tmpFileVersion, aFile);

        // Push a local copy to local review repository, and obtain the local id
        tmpFileVersion.setLocalVersionID(aLocalRepository.registerReviewBlob(aFile.getContents()));
        tmpFileVersion.setVersionID(NO_SOURCE_CONTROL_ID_TEXT);
      }
    }
    return tmpFileVersion;
  }
  /**
   * Method copyRemoteFileToLocalRepository.
   *
   * @param aLocalRepository IRFSRegistry
   * @param aArtifact ScmArtifact
   * @return IFile
   * @throws CoreException
   * @throws ReviewsFileStorageException
   */
  public static R4EFileVersion copyRemoteFileToLocalRepository(
      IRFSRegistry aLocalRepository, ScmArtifact aArtifact)
      throws ReviewsFileStorageException, CoreException {

    if ((null == aArtifact)
        || (null == aArtifact.getPath())
        || aArtifact.getPath().equals(INVALID_PATH)) {
      return null; // File not found in remote repository
    }
    final IFileRevision fileRev = aArtifact.getFileRevision(null);
    if (null == fileRev) {
      return null;
    }

    // Pull file from the version control system
    InputStream iStream = null;
    final IStorage fileStore = fileRev.getStorage(null);
    if (null == fileStore) {
      return null;
    }
    try {
      iStream = fileStore.getContents();
    } catch (CoreException e) {
      R4EUIPlugin.Ftracer.traceInfo("Exception: " + e.toString() + " (" + e.getMessage() + ")");
      return null;
    }

    // Create and Set value in temporary File version
    final R4EFileVersion tmpFileVersion = RModelFactory.eINSTANCE.createR4EFileVersion();
    if ((null != tmpFileVersion) && (null != aLocalRepository)) {
      updateFileVersion(tmpFileVersion, aArtifact);

      // Push a local copy to local review repository, and obtain the local id
      tmpFileVersion.setLocalVersionID(aLocalRepository.registerReviewBlob(iStream));
      if (null != iStream) {
        try {
          iStream.close();
        } catch (IOException e) {
          R4EUIPlugin.Ftracer.traceWarning("Exception while closing stream, " + e.toString());
        }
      }
    }
    return tmpFileVersion;
  }
  /**
   * Method updateFileVersion
   *
   * @param aTargetFileVer R4EFileVersion
   * @param aScmArt ScmArtifact
   * @throws CoreException
   */
  public static void updateFileVersion(R4EFileVersion aTargetFileVer, ScmArtifact aScmArt)
      throws CoreException {

    if ((null != aTargetFileVer) && (null != aScmArt)) {
      aTargetFileVer.setName(aScmArt.getFileRevision(null).getName());
      aTargetFileVer.setVersionID(aScmArt.getId());
      final IFileRevision fileRev = aScmArt.getFileRevision(null);
      if (null != fileRev) {
        final IStorage fileStore = fileRev.getStorage(null);
        if (null != fileStore) {
          final IPath filePath = fileStore.getFullPath();
          if (null != filePath) {
            aTargetFileVer.setRepositoryPath(filePath.toPortableString());
          }
        }
      }

      final String fileRelPath = aScmArt.getProjectRelativePath();
      if (null == fileRelPath) {
        R4EUIPlugin.Ftracer.traceDebug(
            "Invalid relative file path in scmArtifact with path: " + aScmArt.getPath());
      }
      final IProject project = ResourceUtils.getProject(aScmArt.getProjectName());
      final IResource resource = ResourceUtils.findResource(project, fileRelPath);

      aTargetFileVer.setPlatformURI(ResourceUtils.toPlatformURIStr(resource));
      aTargetFileVer.setResource(resource);

      final String projPlatformURI = ResourceUtils.toPlatformURIStr(project);
      if (null == projPlatformURI) {
        R4EUIPlugin.Ftracer.traceDebug(
            "Unable to resolve the project: "
                + aScmArt.getProjectName()
                + " platform's URI, in scmArtifact with path: "
                + aScmArt.getPath());
      }
    }
  }
 /**
  * Resolve and transfer values from ScmArtifact to R4EFileVersion
  *
  * @param aTargetFileVer R4EFileVersion
  * @param aSourceFileVer R4EFileVersion
  */
 public static void copyFileVersionData(
     R4EFileVersion aTargetFileVer, R4EFileVersion aSourceFileVer) {
   if ((null != aTargetFileVer) && (null != aSourceFileVer)) {
     aTargetFileVer.setName(aSourceFileVer.getName());
     aTargetFileVer.setVersionID(aSourceFileVer.getVersionID());
     aTargetFileVer.setRepositoryPath(aSourceFileVer.getRepositoryPath());
     aTargetFileVer.setLocalVersionID(aSourceFileVer.getLocalVersionID());
     aTargetFileVer.setPlatformURI(aSourceFileVer.getPlatformURI());
     aTargetFileVer.setResource(aSourceFileVer.getResource());
   }
 }
  /**
   * Method copyRemoteFileToLocalRepository.
   *
   * @param aLock ReentrantLock
   * @param aLocalRepository IRFSRegistry
   * @param aArtifact ScmArtifact
   * @param aMainMonitor IProgressMonitor
   * @param aSubMonitor IProgressMonitor
   * @return IFile
   * @throws CoreException
   * @throws ReviewsFileStorageException
   * @throws MainJobCancelledException
   * @throws SubJobCancelledException
   */
  public static R4EFileVersion copyRemoteFileToLocalRepository(
      ReentrantLock aLock,
      IRFSRegistry aLocalRepository,
      ScmArtifact aArtifact,
      IProgressMonitor aMainMonitor)
      throws ReviewsFileStorageException, CoreException {
    ReentrantLock lock = aLock;
    if (null == lock) {
      lock = new ReentrantLock();
    }

    // Check if Main Job or Sub Job was cancelled before fetching
    if (aMainMonitor.isCanceled()) {
      throw new CoreException(
          new Status(
              IStatus.CANCEL,
              R4EUIPlugin.PLUGIN_ID,
              IStatus.CANCEL,
              R4EUIConstants.CANCEL_EXCEPTION_MSG,
              null));
    }

    if ((null == aArtifact)
        || (null == aArtifact.getPath())
        || aArtifact.getPath().equals(INVALID_PATH)) {
      return null; // File not found in remote repository
    }

    // Tracing benchmarks
    Date fetchStart = null;
    if (Tracer.isInfo()) {
      fetchStart = new Date();
    }

    final IFileRevision fileRev = aArtifact.getFileRevision(null);
    if (null == fileRev) {
      return null;
    }

    // Pull file from the version control system
    InputStream iStream = null;
    final IStorage fileStore = fileRev.getStorage(null);
    if (null == fileStore) {
      return null;
    }
    try {
      iStream = fileStore.getContents();
    } catch (CoreException e) {
      R4EUIPlugin.Ftracer.traceInfo("Exception: " + e.toString() + " (" + e.getMessage() + ")");
      return null;
    }

    // Create and Set value in temporary File version
    final R4EFileVersion tmpFileVersion = RModelFactory.eINSTANCE.createR4EFileVersion();
    if ((null != tmpFileVersion) && (null != aLocalRepository)) {
      updateFileVersion(tmpFileVersion, aArtifact);

      // Check if Main Job or Sub Job was cancelled before pushing a local copy
      if (aMainMonitor.isCanceled()) {
        throw new CoreException(
            new Status(
                IStatus.CANCEL,
                R4EUIPlugin.PLUGIN_ID,
                IStatus.CANCEL,
                R4EUIConstants.CANCEL_EXCEPTION_MSG,
                null));
      }

      // Tracing benchmarks
      Date blobRegStart = null;
      if (fetchStart != null) {
        blobRegStart = new Date();
      }

      // Push a local copy to local review repository, and obtain the local id
      lock.lock();
      try {
        tmpFileVersion.setLocalVersionID(aLocalRepository.registerReviewBlob(iStream));
      } finally {
        lock.unlock();

        if (fetchStart != null) {
          long downloadTime = blobRegStart.getTime() - fetchStart.getTime();
          long uploadTime = (new Date()).getTime() - blobRegStart.getTime();
          R4EUIPlugin.Ftracer.traceInfo(
              "Registered blob for "
                  + fileRev.getName()
                  + " "
                  + //$NON-NLS-1$//$NON-NLS-2$
                  aArtifact.getId()
                  + ", fetch (ms): "
                  + downloadTime
                  + ", push (ms) "
                  + uploadTime); //$NON-NLS-1$ //$NON-NLS-2$
        }

        if (null != iStream) {
          try {
            iStream.close();
          } catch (IOException e) {
            R4EUIPlugin.Ftracer.traceWarning("Exception while closing stream, " + e.toString());
          }
        }
      }
    }
    return tmpFileVersion;
  }
 /**
  * Method isSameFileVersion.
  *
  * @param aFile1 R4EFileVersion
  * @param aFile2 R4EFileVersion
  * @return boolean
  */
 public static boolean isSameFileVersion(R4EFileVersion aFile1, R4EFileVersion aFile2) {
   return (compareStrings(aFile1.getPlatformURI(), aFile2.getPlatformURI())
       && compareStrings(aFile1.getRepositoryPath(), aFile2.getRepositoryPath())
       && compareStrings(aFile1.getVersionID(), aFile2.getVersionID()));
 }