Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T667 validate unique project name in creation form #299

Merged
merged 6 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public interface MigrationProjectEndpoint
@Path("delete")
void deleteProject(MigrationProject migration);

/**
* Look up a project ID by name.
*/
@GET
@Path("id-by-name/{title}")
Long getProjectIdByName(@PathParam("title") String title);



/**
* Adds app count to MigrationProject solely for the purpose of this REST API.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.PathParam;

import org.jboss.windup.util.exception.WindupException;

import org.jboss.windup.web.addons.websupport.WebPathUtil;
import org.jboss.windup.web.furnaceserviceprovider.FromFurnace;
import org.jboss.windup.web.services.model.AnalysisContext;
import org.jboss.windup.web.services.model.MigrationProject;
import org.jboss.windup.web.services.service.AnalysisContextService;
import org.jboss.windup.web.services.service.MigrationProjectService;
Expand Down Expand Up @@ -95,4 +95,12 @@ public void deleteProject(MigrationProject migrationProject)
this.migrationProjectService.deleteProject(project);
}

@Override
public Long getProjectIdByName(String title){
String jql = "SELECT id FROM MigrationProject p WHERE LOWER(p.title) = LOWER(:title)";
List<Long> ids = this.entityManager.createQuery(jql, Long.class)
.setParameter("title", title)
.getResultList();
return ids.isEmpty() ? null : ids.get(0);
}
}
2 changes: 2 additions & 0 deletions ui/src/main/webapp/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import {ExecutionApplicationListComponent} from "./reports/execution-application
import {SourceResolve} from "./reports/source/source.resolve";
import {ExecutionResolve} from "./executions/execution.resolve";
import {CacheService, CacheServiceInstance} from "./shared/cache.service";
import {ProjectNameNotExistsValidator} from "./shared/validators/project-name-not-exists.validator";

/**
* Load all mapping data from the generated files.
Expand All @@ -151,6 +152,7 @@ initializeModelMappingData();
declarations: [
// Directives
InViewport,
ProjectNameNotExistsValidator,

// pages
AppComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ <h1>{{title}}</h1>
<div class="form-group">
<label class="col-md-2 control-label" for="idProjectTitle">Project Title</label>
<div class="col-md-8">
<input #projectTitleControl="ngModel"
name="projectTitle"
[(ngModel)]="model.title"
required
minlength="1"
maxlength="128"
type="text"
id="idProjectTitle"
<input #projectTitleControl="ngModel" name="projectTitle" id="idProjectTitle" [(ngModel)]="model.title"
required wuProjectNameNotUsed minlength="1" maxlength="128" type="text"
class="form-control">
</div>
<span [class.hidden]="!hasError(projectTitleControl)" class="help-block">
<span [class.hidden]="!titleIsDuplicated(projectTitleControl)" class="help-block">
The title must be unique
</span>
<span [class.hidden]="titleIsDuplicated(projectTitleControl) || !hasError(projectTitleControl)" class="help-block">
The title must be greater than 3 characters long and fewer than 128 characters.
</span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {MigrationProjectService} from "./migration-project.service";
import {FormComponent} from "../shared/form.component";
import {Subscription} from "rxjs";
import {RouteFlattenerService} from "../core/routing/route-flattener.service";
import {FormControl} from "@angular/forms";

@Component({
templateUrl: './migration-project-form.component.html',
Expand Down Expand Up @@ -56,6 +57,11 @@ export class MigrationProjectFormComponent extends FormComponent implements OnIn
return Math.min(4 + (this.model.description ? this.model.description.length : 0) / 80, 25)
}

titleIsDuplicated(control:FormControl):boolean {
let touched = control.touched == null ? false : control.touched;
return control.hasError('nameIsTaken') && touched;
}

save() {
if (this.editMode) {
this._migrationProjectService.update(this.model).subscribe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class MigrationProjectService extends AbstractService {
private CREATE_MIGRATION_PROJECT_URL = "/migrationProjects/create";
private UPDATE_MIGRATION_PROJECT_URL = "/migrationProjects/update";
private DELETE_MIGRATION_PROJECT_URL = '/migrationProjects/delete';
private GET_ID_BY_NAME_URL = '/migrationProjects/id-by-name';

private monitoredProjects = new Map<number, MigrationProject>();

Expand Down Expand Up @@ -138,6 +139,12 @@ export class MigrationProjectService extends AbstractService {
stopMonitoringProject(project: MigrationProject) {
this.monitoredProjects.delete(project.id);
}

getIdByName(name: string): Observable<number> | null {
return this._http.get(Constants.REST_BASE + this.GET_ID_BY_NAME_URL + "/" + encodeURIComponent(name))
.map(res => <number> res.json())
.catch(this.handleError);
}
}

export interface HasAppCount{ applicationCount: number; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {Directive, forwardRef} from '@angular/core';
import {FormControl, NG_ASYNC_VALIDATORS, Validator} from "@angular/forms";
import {MigrationProjectService} from "../../project/migration-project.service";

/**
* Fails validation if the name in the given control already exists as a project name.
*/
@Directive({
selector: '[wuProjectNameNotUsed]',
providers: [
{ provide: NG_ASYNC_VALIDATORS, useExisting: forwardRef(() => ProjectNameNotExistsValidator), multi: true }
]
})
export class ProjectNameNotExistsValidator implements Validator {

constructor(private projectService: MigrationProjectService)
{
}

validate(control: FormControl):Promise<{[key: string] : boolean}> {
let projectService = this.projectService;

return new Promise(resolve => {
projectService.getIdByName(control.value).subscribe(result => {
if (result == null) {
resolve(null);
} else {
resolve({['nameIsTaken']: true});
}
});
});
}

}