Search in sources :

Example 16 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class GitClient method getHeadCommit.

/**
 * Return the head commit of a repository
 *
 * @param gitRepository {@link GitRepository} to retrieve head commit from
 * @return the head commit
 * @throws BusinessException if we cannot read the repositories branches
 */
public RevCommit getHeadCommit(GitRepository gitRepository) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasReadRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        try (RevWalk rw = new RevWalk(git.getRepository())) {
            ObjectId head = git.getRepository().resolve(Constants.HEAD);
            return rw.parseCommit(head);
        }
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) ObjectId(org.eclipse.jgit.lib.ObjectId) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File)

Example 17 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class GitClient method reset.

/**
 * Reset a repository to a given commit
 *
 * @param gitRepository {@link GitRepository} to reset
 * @param commit        Commit to reset onto
 */
public void reset(GitRepository gitRepository, RevCommit commit) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasWriteRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        git.reset().setMode(ResetCommand.ResetType.HARD).setRef(commit.getId().getName()).call();
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } catch (GitAPIException e) {
        throw new BusinessException("Cannot reset repository to commit " + commit.getId().getName(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) File(java.io.File)

Example 18 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class GitClient method currentBranch.

/**
 * Retrieve the name of the current branch of the repository
 *
 * @param gitRepository Repository to get branch name from
 * @return current branch name
 * @throws BusinessException          if repository cannot be opened
 * @throws UserNotAuthorizedException if user does not have read access to the repository
 */
public String currentBranch(GitRepository gitRepository) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasReadRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        return git.getRepository().getBranch();
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) File(java.io.File)

Example 19 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class GitClient method merge.

/**
 * Merge a branch into an other
 *
 * @param gitRepository GitRepository where to merge branch
 * @param from          Branch to get changes
 * @param to            Branch to be updated
 * @return <code>true</code> if the merge has no conflict.
 * @throws BusinessException          if problem happens during merge
 * @throws UserNotAuthorizedException if user does not have write access to the repository
 */
public boolean merge(GitRepository gitRepository, String from, String to) throws BusinessException {
    MeveoUser user = currentUser.get();
    if (!GitHelper.hasWriteRole(user, gitRepository)) {
        throw new UserNotAuthorizedException(user.getUserName());
    }
    final File repositoryDir = GitHelper.getRepositoryDir(user, gitRepository.getCode());
    keyLock.lock(gitRepository.getCode());
    try (Git git = Git.open(repositoryDir)) {
        String previousBranch = git.getRepository().getBranch();
        git.checkout().setCreateBranch(false).setName(to).call();
        try {
            boolean successful = git.rebase().setUpstream(from).call().getStatus().isSuccessful();
            if (!successful) {
                git.rebase().setOperation(RebaseCommand.Operation.ABORT).call();
            }
            return successful;
        } catch (JGitInternalException e) {
            log.warn("Cannot merge {} into {}", from, to, e);
            git.rebase().setOperation(RebaseCommand.Operation.ABORT).call();
            return false;
        } catch (GitAPIException e) {
            throw new BusinessException("Cannot merge " + from + " into " + to, e);
        } finally {
            git.checkout().setCreateBranch(false).setName(previousBranch).call();
        }
    } catch (IOException e) {
        throw new BusinessException("Cannot open repository " + gitRepository.getCode(), e);
    } catch (GitAPIException e) {
        throw new BusinessException("Checkout problem for repository " + gitRepository.getCode(), e);
    } finally {
        keyLock.unlock(gitRepository.getCode());
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) BusinessException(org.meveo.admin.exception.BusinessException) Git(org.eclipse.jgit.api.Git) UserNotAuthorizedException(org.meveo.admin.exception.UserNotAuthorizedException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) MeveoUser(org.meveo.security.MeveoUser) IOException(java.io.IOException) File(java.io.File)

Example 20 with BusinessException

use of org.meveo.admin.exception.BusinessException in project meveo by meveo-org.

the class EntityCustomActionService method addFilesToModule.

@Override
public void addFilesToModule(EntityCustomAction entity, MeveoModule module) throws BusinessException {
    BaseEntityDto businessEntityDto = businessEntitySerializer.serialize(entity);
    String businessEntityDtoSerialize = JacksonUtil.toString(businessEntityDto);
    File gitDirectory = GitHelper.getRepositoryDir(currentUser, module.getCode());
    String cetCode = CustomEntityTemplate.getCodeFromAppliesTo(entity.getAppliesTo());
    if (cetCode == null) {
        cetCode = CustomRelationshipTemplate.getCodeFromAppliesTo(entity.getAppliesTo());
    }
    String path = entity.getClass().getAnnotation(ModuleItem.class).path() + "/" + cetCode;
    File newDir = new File(gitDirectory, path);
    newDir.mkdirs();
    File newJsonFile = new File(newDir, entity.getCode() + ".json");
    try {
        MeveoFileUtils.writeAndPreserveCharset(businessEntityDtoSerialize, newJsonFile);
    } catch (IOException e) {
        throw new BusinessException("File cannot be updated or created", e);
    }
    GitRepository gitRepository = gitRepositoryService.findByCode(module.getCode());
    gitClient.commitFiles(gitRepository, Collections.singletonList(newDir), "Add JSON file for custom action " + cetCode + "." + entity.getCode());
}
Also used : ModuleItem(org.meveo.model.ModuleItem) BaseEntityDto(org.meveo.api.dto.BaseEntityDto) GitRepository(org.meveo.model.git.GitRepository) BusinessException(org.meveo.admin.exception.BusinessException) IOException(java.io.IOException) File(java.io.File)

Aggregations

BusinessException (org.meveo.admin.exception.BusinessException)229 IOException (java.io.IOException)97 File (java.io.File)59 HashMap (java.util.HashMap)50 EntityDoesNotExistsException (org.meveo.api.exception.EntityDoesNotExistsException)50 ArrayList (java.util.ArrayList)48 MeveoApiException (org.meveo.api.exception.MeveoApiException)39 ELException (org.meveo.elresolver.ELException)39 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)38 CustomEntityTemplate (org.meveo.model.customEntities.CustomEntityTemplate)37 Map (java.util.Map)34 BundleKey (org.jboss.seam.international.status.builder.BundleKey)30 TransactionAttribute (javax.ejb.TransactionAttribute)28 CustomEntityInstance (org.meveo.model.customEntities.CustomEntityInstance)27 List (java.util.List)25 MeveoModule (org.meveo.model.module.MeveoModule)25 NoResultException (javax.persistence.NoResultException)24 HashSet (java.util.HashSet)22 Response (javax.ws.rs.core.Response)22 Collection (java.util.Collection)20