/* * Copyright (c) 2011 NoMagic, Inc. All Rights Reserved. */ package com.nomagic.magicdraw.examples.teamwork; import com.nomagic.magicdraw.core.Application; import com.nomagic.magicdraw.core.modules.ModuleUsage; import com.nomagic.magicdraw.core.project.ProjectDescriptor; import com.nomagic.magicdraw.core.project.ProjectDescriptorsFactory; import com.nomagic.magicdraw.teamwork.application.BranchData; import com.nomagic.magicdraw.teamwork.application.TeamworkUtils; import com.nomagic.task.SimpleProgressStatus; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import java.net.URI; import java.rmi.RemoteException; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * Teamwork API usage sample. * * @author Martynas Lelevicius */ @SuppressWarnings({"UnusedDeclaration"}) public class TeamworkSample { /** * Branch the given project and open the branch. User must be already logged in. * * @param projectQualifiedName project qualified name. * @throws RemoteException remote exception. */ public static void branchProject(@Nonnull String projectQualifiedName) throws RemoteException { branchProjectVersion(projectQualifiedName, -1, null, "myBranch", "new branch"); } /** * Branch the given project with given modules. * * @param projectQualifiedName project qualified name * @param modules names of modules to branch * @throws RemoteException remote exception. */ public static void branchWithSelectedModules(@Nonnull String projectQualifiedName, @Nonnull Set modules) throws RemoteException { ProjectDescriptor projectDescriptor = TeamworkUtils .getRemoteProjectDescriptorByQualifiedName(projectQualifiedName); if (projectDescriptor == null) { return; } Set modulesToBranch = new HashSet<>(); //noinspection ConstantConditions for (String id : TeamworkUtils.getUsedModules(projectDescriptor, false).keySet()) { final ProjectDescriptor descriptor = TeamworkUtils.getRemoteProjectDescriptor(id); if (descriptor != null && modules.contains(ProjectDescriptorsFactory.getProjectName(descriptor.getURI()))) { modulesToBranch.add(id); } } branchProjectVersion(projectQualifiedName, -1, modulesToBranch, "myBranch", "branch with selected modules"); } /** * Branch the given project given version and open the branch. User must be already logged in. * * @param projectQualifiedName project qualified name. * @param version project version to branch from (-1 for latest version) * @param modules modules (Ids) to branch * @param branchName name of the branch. * @param comment branch comment. * @throws RemoteException remote exception. */ public static void branchProjectVersion(@Nonnull String projectQualifiedName, int version, @CheckForNull Set modules, @Nonnull String branchName, @CheckForNull String comment) throws RemoteException { // get project descriptor ProjectDescriptor projectDescriptor = TeamworkUtils .getRemoteProjectDescriptorByQualifiedName(projectQualifiedName); if (projectDescriptor == null) { return; } if (version > -1) { // if not latest version create descriptor for actual project version final URI uri = projectDescriptor.getURI(); final String id = ProjectDescriptorsFactory.getRemoteID(uri); final String projectName = ProjectDescriptorsFactory.getProjectName(uri); if (id != null) { projectDescriptor = ProjectDescriptorsFactory.createRemoteProjectDescriptor(id, projectName, version); } } if (projectDescriptor == null) { return; } // branch project with all modules final Map branched = TeamworkUtils .branchProject(projectDescriptor, modules, branchName, comment); if (branched != null) { final String id = ProjectDescriptorsFactory.getRemoteID(projectDescriptor.getURI()); final String branchedProjectId = branched.get(id); if (branchedProjectId != null) { // load branched project final ProjectDescriptor branchedDescriptor = TeamworkUtils.getRemoteProjectDescriptor( branchedProjectId); if (branchedDescriptor != null) { Application.getInstance().getProjectsManager().loadProject(branchedDescriptor, true); } } } } /** * Change used module branch. * * @param moduleUsage module usage. * @param moduleBranchQualifiedName module branch project qualified name. * @param version module branch version. * @throws Exception exception. */ public static void changeUsedModuleBranch(ModuleUsage moduleUsage, String moduleBranchQualifiedName, int version) throws Exception { TeamworkUtils.changeModuleBranch(moduleUsage, moduleBranchQualifiedName, version, new SimpleProgressStatus()); } /** * Rename branch. * * @param projectName project name. * @param branchName branch name. * @param newBranchName new branch name. * @throws RemoteException remote exception */ public static void renameBranch(@Nonnull String projectName, @Nonnull String branchName, @Nonnull String newBranchName) throws RemoteException { // construct qualified name final String qualifiedName = TeamworkUtils.generateProjectQualifiedName(projectName, new String[]{branchName}); final ProjectDescriptor projectDescriptor = TeamworkUtils .getRemoteProjectDescriptorByQualifiedName(qualifiedName); if (projectDescriptor != null) { // rename branch TeamworkUtils.setTeamworkProjectBranchName(projectDescriptor, newBranchName); } } /** * Retrieve branch data. * * @param trunkProjectName trunk project name. * @throws RemoteException remote exception */ public static void getBranchData(@Nonnull String trunkProjectName) throws RemoteException { final ProjectDescriptor trunkProjectDescriptor = TeamworkUtils .getRemoteProjectDescriptorByQualifiedName(trunkProjectName); if (trunkProjectDescriptor != null) { final Set branchesOfTrunk = TeamworkUtils.getBranches(trunkProjectDescriptor); if (branchesOfTrunk != null) { final BranchData branchOfTrunk = branchesOfTrunk.iterator().next(); final String trunkProjectId = branchOfTrunk.getBranchedFromId(); // trunk version the branch is created from final int trunkVersion = branchOfTrunk.getBranchedFromVersion(); final ProjectDescriptor branchOfTrunkDescriptor = TeamworkUtils .getRemoteProjectDescriptor(branchOfTrunk.getBranchId()); if (branchOfTrunkDescriptor != null) { final BranchData trunk = TeamworkUtils.getBranchedFrom(branchOfTrunkDescriptor); final Set branchesOfBranch = TeamworkUtils.getBranches(branchOfTrunkDescriptor); if (branchesOfBranch != null) { final BranchData branchOfBranch = branchesOfBranch.iterator().next(); final ProjectDescriptor branchOfBranchDescriptor = TeamworkUtils.getRemoteProjectDescriptor( branchOfBranch.getBranchId()); if (branchOfBranchDescriptor != null) { final BranchData branch = TeamworkUtils.getBranchedFrom(branchOfBranchDescriptor); } } } } } } }