Skip to content

Commit

Permalink
pushing WIP for Vit to look at
Browse files Browse the repository at this point in the history
Signed-off-by: Katelyn Nienaber <katelyn.nienaber@broadcom.com>
  • Loading branch information
katelynienaber committed Oct 22, 2020
1 parent c9ae4f1 commit 92557a9
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 141 deletions.
117 changes: 72 additions & 45 deletions src/EndevorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ export class EndevorController {
repo.id = this.findNextId(connectionLabel);
}
const newRepoNode: EndevorNode = new EndevorNode(repo);
this.connections.get(connectionLabel).loadRepository(repo);
const conn = this.connections.get(connectionLabel);
if (conn) { conn.loadRepository(repo); }
this._rootNode.children.forEach(child => {
if (child.getEntity().getName() === connectionLabel) {
const entity = child.getEntity();
if (entity && entity.getName() === connectionLabel) {
child.children.push(newRepoNode);
}
});
Expand All @@ -71,6 +73,7 @@ export class EndevorController {
return Array.from(this.connections.values());
}
public addConnection(connection: Connection) {
if (!connection.name) { connection.name = "" }
this.connections.set(connection.name, connection);
const newConnectionNode = new EndevorNode(connection);
this._rootNode.children.push(newConnectionNode);
Expand All @@ -86,26 +89,32 @@ export class EndevorController {
}

public removeRepository(repoName: string, connectionLabel: string) {
this.connections.get(connectionLabel).getRepositoryMap().delete(repoName);
const conn = this.connections.get(connectionLabel);
if (conn) {
conn.getRepositoryMap().delete(repoName);
}
this._rootNode.children.forEach((connection, index) => {
if (connection.label === connectionLabel) {
this._rootNode.children[index].children = connection.children.filter(repo => repo.label !== repoName);
}
});
}

public updateRepositoryName(oldRepoName: string, newRepoName: string, connectionlabel: string) {
public updateRepositoryName(oldRepoName: string, newRepoName: string, connectionLabel: string) {
const newMap = new Map();
this.connections.get(connectionlabel).getRepositoryMap().forEach((repo, name) => {
if (name === oldRepoName) {
repo.setName(newRepoName);
newMap.set(newRepoName, repo);
} else {
newMap.set(name, repo);
}
});
this.connections.get(connectionlabel).repositories = newMap;
const cnxIdx = this.rootNode.children.findIndex(node => node.label === connectionlabel);
const conn = this.connections.get(connectionLabel);
if (conn) {
conn.getRepositoryMap().forEach((repo, name) => {
if (name === oldRepoName) {
repo.setName(newRepoName);
newMap.set(newRepoName, repo);
} else {
newMap.set(name, repo);
}
});
conn.repositories = newMap;
}
const cnxIdx = this.rootNode.children.findIndex(node => node.label === connectionLabel);
const repoIdx = this.rootNode.children[cnxIdx].children.findIndex(repo => repo.label === oldRepoName);
this.rootNode.children[cnxIdx].children[repoIdx].label = newRepoName;
}
Expand Down Expand Up @@ -164,17 +173,25 @@ export class EndevorController {
}
updatedRepos.set(repoToKeep.getName(), repoToKeep);
});
let currentRepos = this.connections.get(connName).repositories;
currentRepos = updatedRepos;
currentRepos.forEach(repo => EndevorController.instance.addRepository(repo, repo.getProfileLabel()));
let currentRepos = updatedRepos;
currentRepos.forEach(repo => {
let profileLabel = repo.getProfileLabel();
if (!profileLabel) { profileLabel = "" }
EndevorController.instance.addRepository(repo, profileLabel);
})
this.updateIDs(connName);
}
});
}


public isRepoInConnection(repoName: string, connectionLabel: string): boolean {
const repoMap = this.connections.get(connectionLabel).getRepositoryMap();
return repoMap.get(repoName) ? true : false ;
const conn = this.connections.get(connectionLabel);
if (conn) {
const repoMap = conn.getRepositoryMap();
return repoMap.get(repoName) ? true : false;
}
return false;
}

// tslint:disable-next-line: member-ordering
Expand All @@ -183,12 +200,15 @@ export class EndevorController {
return undefined;
}
const connection = this.findNodeByConnectionName(connectionLabel);
for (const node of connection.children) {
const nodeRepo: Repository | undefined = node.getRepository();
if (nodeRepo && nodeRepo.id === id) {
return node;
if (connection) {
for (const node of connection.children) {
const nodeRepo: Repository | undefined = node.getRepository();
if (nodeRepo && nodeRepo.id === id) {
return node;
}
}
}
return undefined;
}

// tslint:disable-next-line: member-ordering
Expand All @@ -197,7 +217,7 @@ export class EndevorController {
return undefined;
}
for (const node of this._rootNode.children) {
if (node.getEntity().getName() === name) {
if (node.getEntity() && node.getEntity()!.getName() === name) {
return node;
}
}
Expand All @@ -207,37 +227,44 @@ export class EndevorController {
* Function used to determine next available `id`.
*/
public findNextId(connectionLabel: string): number {
const repoMap = this.connections.get(connectionLabel).getRepositoryMap();
let iDArray: boolean[] = new Array(repoMap.size);
iDArray.fill(true);
repoMap.forEach(repo => {
if (repo.id !== undefined) {
iDArray[repo.id] = false;
}
});
for (let i = 0; i < iDArray.length; i++) {
if (iDArray[i]) {
return i;
const conn = this.connections.get(connectionLabel);
if (conn) {
const repoMap = conn.getRepositoryMap();
let iDArray: boolean[] = new Array(repoMap.size);
iDArray.fill(true);
repoMap.forEach(repo => {
if (repo.id !== undefined) {
iDArray[repo.id] = false;
}
});
for (let i = 0; i < iDArray.length; i++) {
if (iDArray[i]) {
return i;
}
}
return iDArray.length;
}
return iDArray.length;
return -1;
}

/**
* In case `id` is not present in the settings.json, this function will determine it
* and store in the settings.json.
*/
private updateIDs(connectionLabel: string) {
const repoMap = this.connections.get(connectionLabel).getRepositoryMap();
let saveRepos: boolean = false;
repoMap.forEach(repo => {
if (repo.id === undefined) {
repo.id = EndevorController.instance.findNextId(connectionLabel);
saveRepos = true;
const conn = this.connections.get(connectionLabel);
if (conn) {
const repoMap = conn.getRepositoryMap();
let saveRepos: boolean = false;
repoMap.forEach(repo => {
if (repo.id === undefined) {
repo.id = EndevorController.instance.findNextId(connectionLabel);
saveRepos = true;
}
});
if (saveRepos) {
this.updateSettings();
}
});
if (saveRepos) {
this.updateSettings();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/commands/DeleteHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function deleteHost(arg:any){
if (repo) {
vscode.window.showWarningMessage("Remove Configuration: " + repo.getName() + "?", "OK").then(message => {
if (message === "OK") {
EndevorController.instance.removeRepository(repo.getName(), repo.getProfileLabel());
const profileLabel = repo.getProfileLabel() ? repo.getProfileLabel() : "";
EndevorController.instance.removeRepository(repo.getName(), profileLabel!);
EndevorController.instance.updateSettings();
}
});
Expand Down
25 changes: 15 additions & 10 deletions src/commands/HostDialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class HostDialogs {
);
}
const createPick = new utils.FilterDescriptor("\uFF0B " + createNewProfile);
const items: vscode.QuickPickItem[] = profileNamesList.map(element => new utils.FilterItem(element));
const items: vscode.QuickPickItem[] = profileNamesList.map(element => new utils.FilterItem(element!));
const placeholder = "Choose \"Create new...\" to define a new profile or select an existing one";

const quickpick = vscode.window.createQuickPick();
Expand All @@ -63,11 +63,10 @@ export class HostDialogs {

if (chosenProfile === "") {
let newProfileName: any;
let profileName: string;
let profileName: string | undefined;
const options = {
placeHolder: "Profile Name",
prompt: "Enter a name for the profile",
value: profileName,
prompt: "Enter a name for the profile"
};
profileName = await vscode.window.showInputBox(options);
if (!profileName) {
Expand All @@ -84,15 +83,20 @@ export class HostDialogs {
try {
const newProfile = Profiles.getInstance().listProfiles().find(
profile => profile.name === newProfileName);
const profileToAdd = new Connection(newProfile);
EndevorController.instance.addConnection(profileToAdd);
if (newProfile) {
const profileToAdd = new Connection(newProfile);
EndevorController.instance.addConnection(profileToAdd);
}
} catch (error) {
vscode.window.showErrorMessage("Error while adding new profile");
}
}
} else if (chosenProfile) {
const profileToAdd = new Connection(allProfiles.find(profile => profile.name === chosenProfile));
EndevorController.instance.addConnection(profileToAdd);
const profileToUse = allProfiles.find(profile => profile.name === chosenProfile);
if (profileToUse) {
const profileToAdd = new Connection(profileToUse);
EndevorController.instance.addConnection(profileToAdd);
}
} else {
vscode.window.showInformationMessage("Operation cancelled");
}
Expand Down Expand Up @@ -149,16 +153,17 @@ export class HostDialogs {
const repo: Repository | undefined = context.getRepository();
if (repo) {
const newName = await HostDialogs.showHostNameInput(repo);
const profileLabel = repo.getProfileLabel() ? repo.getProfileLabel() : "";

if (newName === undefined) {
return;
}
if (EndevorController.instance.isRepoInConnection(newName, repo.getProfileLabel())) {
if (EndevorController.instance.isRepoInConnection(newName, profileLabel!)) {
window.showErrorMessage("Configuration with name " + newName + " already exists");
return;
}
const oldName = repo.getName();
EndevorController.instance.updateRepositoryName(oldName, newName, repo.getProfileLabel());
EndevorController.instance.updateRepositoryName(oldName, newName, profileLabel!);
EndevorController.instance.updateSettings();
window.showInformationMessage(`Configuration ${oldName} was renamed to ${newName}.`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ export class Connection extends EndevorEntity implements IProfileLoaded {
return this;
}

public getName(): string {
public getName(): string | undefined {
return this.name;
}
public getDescription(): string {
return "";
}

public getProfile(): IProfile {
public getProfile(): IProfile | undefined {
return this.profile;
}

Expand Down
2 changes: 1 addition & 1 deletion src/model/EndevorEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { Repository } from "./Repository";

export abstract class EndevorEntity {
abstract getName(): string;
abstract getName(): string | undefined;
abstract getDescription(): string;
abstract getRepository(): Repository;
}
12 changes: 6 additions & 6 deletions src/model/IConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import { IProfile } from "@zowe/imperative";

export interface IConnection extends IProfile {
name: string;
host: string;
port: number;
user: string;
password: string;
rejectUnauthorized: boolean;
protocol: string;
host: string | undefined;
port: number | undefined;
user: string | undefined;
password: string | undefined;
rejectUnauthorized: boolean | undefined;
protocol: string | undefined;
}
2 changes: 1 addition & 1 deletion src/model/IEndevorInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Host {
id?: number;
name: string;
url: string;
username: string;
username?: string;
password?: string;
datasource: string;
filters?: Filter[];
Expand Down
10 changes: 5 additions & 5 deletions src/model/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export class Repository extends EndevorEntity {
private _id?: number;
private name: string;
private url: string;
private username: string;
private username: string | undefined;
private password: string | undefined;
private datasource: string;
private _environments: Map<string, Environment>;
private _filters: EndevorFilter[];
private _map: EndevorFilter;
private _profileLabel: string;
private _profileLabel: string | undefined;

constructor(name: string, url: string, username: string, password: string | undefined, datasource: string, profileLabel: string, id?: number) {
constructor(name: string, url: string, username: string | undefined, password: string | undefined, datasource: string, profileLabel: string | undefined, id?: number) {
super();
this._id = id;
this.name = name;
Expand Down Expand Up @@ -109,7 +109,7 @@ export class Repository extends EndevorEntity {
this._profileLabel = value;
}

public getProfileLabel(): string {
public getProfileLabel(): string | undefined {
return this._profileLabel;
}

Expand All @@ -121,7 +121,7 @@ export class Repository extends EndevorEntity {
this.username = value;
}

public getUsername(): string {
public getUsername(): string | undefined {
return this.username;
}

Expand Down
Loading

0 comments on commit 92557a9

Please sign in to comment.