11package controllers
22
33import controllers .base ._
4+ import controllers .helper .ControllerUtils .parseIntegerSeq
45import formats .json .GalleryFormats ._
56import formats .json .LabelFormats
67import models .auth .DefaultEnv
78import models .gallery .{GalleryTaskEnvironment , GalleryTaskEnvironmentTable , GalleryTaskInteraction , GalleryTaskInteractionTable }
9+ import models .label .LabelTypeEnum
810import models .utils .MyPostgresProfile
11+ import play .api .Configuration
912import play .api .db .slick .{DatabaseConfigProvider , HasDatabaseConfigProvider }
13+ import play .api .i18n .Messages
1014import play .api .libs .json .{JsError , JsObject , JsValue , Json }
11- import play .api .mvc .{Action , Result }
15+ import play .api .mvc .{Action , AnyContent , Result }
1216import play .silhouette .api .Silhouette
13- import service .{GsvDataService , LabelService }
14-
17+ import service .{ConfigService , GsvDataService , LabelService , RegionService }
1518import javax .inject .{Inject , Singleton }
1619import scala .concurrent .{ExecutionContext , Future }
1720
1821@ Singleton
1922class GalleryController @ Inject () (
2023 cc : CustomControllerComponents ,
2124 val silhouette : Silhouette [DefaultEnv ],
25+ val config : Configuration ,
2226 protected val dbConfigProvider : DatabaseConfigProvider ,
2327 implicit val ec : ExecutionContext ,
28+ configService : ConfigService ,
2429 labelService : LabelService ,
2530 gsvDataService : GsvDataService ,
2631 galleryTaskInteractionTable : GalleryTaskInteractionTable ,
27- galleryTaskEnvironmentTable : GalleryTaskEnvironmentTable
28- ) extends CustomBaseController (cc)
32+ galleryTaskEnvironmentTable : GalleryTaskEnvironmentTable ,
33+ regionService : RegionService
34+ )(implicit assets : AssetsFinder )
35+ extends CustomBaseController (cc)
2936 with HasDatabaseConfigProvider [MyPostgresProfile ] {
37+ implicit val implicitConfig : Configuration = config
38+
39+ /**
40+ * Returns the Gallery page.
41+ */
42+ def gallery (
43+ labelType : String ,
44+ neighborhoods : String ,
45+ severities : String ,
46+ tags : String ,
47+ validationOptions : String ,
48+ aiValidationOptions : String
49+ ): Action [AnyContent ] =
50+ cc.securityService.SecuredAction { implicit request =>
51+ val labelTypes : Seq [(String , String )] = Seq (
52+ (" Assorted" , Messages (" gallery.all" )),
53+ (LabelTypeEnum .CurbRamp .name, Messages (" curb.ramp" )),
54+ (LabelTypeEnum .NoCurbRamp .name, Messages (" missing.ramp" )),
55+ (LabelTypeEnum .Obstacle .name, Messages (" obstacle" )),
56+ (LabelTypeEnum .SurfaceProblem .name, Messages (" surface.problem" )),
57+ (LabelTypeEnum .Occlusion .name, Messages (" occlusion" )),
58+ (LabelTypeEnum .NoSidewalk .name, Messages (" no.sidewalk" )),
59+ (LabelTypeEnum .Crosswalk .name, Messages (" crosswalk" )),
60+ (LabelTypeEnum .Signal .name, Messages (" signal" )),
61+ (LabelTypeEnum .Other .name, Messages (" other" ))
62+ )
63+ val labType : String = if (labelTypes.exists(x => { x._1 == labelType })) labelType else " Assorted"
64+
65+ for {
66+ possibleRegions : Seq [Int ] <- regionService.getAllRegions.map(_.map(_.regionId))
67+ possibleTags : Seq [String ] <- {
68+ if (labType != " Assorted" ) db.run(labelService.selectTagsByLabelType(labelType).map(_.map(_.tag)))
69+ else Future .successful(Seq ())
70+ }
71+ commonData <- configService.getCommonPageData(request2Messages.lang)
72+ } yield {
73+ // Make sure that list of region IDs, severities, and validation options are formatted correctly.
74+ val regionIdsList : Seq [Int ] = parseIntegerSeq(neighborhoods).filter(possibleRegions.contains)
75+ val severityList : Seq [Int ] = parseIntegerSeq(severities).filter(s => s > 0 && s < 4 )
76+ val tagList : List [String ] = tags.split(" ," ).filter(possibleTags.contains).toList
77+ val valOptions : Seq [String ] =
78+ validationOptions.split(" ," ).filter(Seq (" correct" , " incorrect" , " unsure" , " unvalidated" ).contains(_)).toSeq
79+ val aiValOptions : Seq [String ] =
80+ aiValidationOptions.split(" ," ).filter(Seq (" correct" , " incorrect" , " unsure" , " unvalidated" ).contains(_)).toSeq
81+
82+ // Log visit to Gallery async.
83+ val activityStr : String =
84+ s " Visit_Gallery_LabelType= ${labType}_RegionIDs= ${regionIdsList}_Severity= ${severityList}_Tags= ${tagList}_Validations= $valOptions"
85+ cc.loggingService.insert(request.identity.userId, request.ipAddress, activityStr)
86+
87+ Ok (
88+ views.html.apps.gallery(commonData, " Sidewalk - Gallery" , request.identity, labType, labelTypes,
89+ regionIdsList, severityList, tagList, valOptions, aiValOptions)
90+ )
91+ }
92+ }
3093
3194 /**
3295 * Returns labels of specified type, severities, and tags.
@@ -36,18 +99,19 @@ class GalleryController @Inject() (
3699 submission.fold(
37100 errors => { Future .successful(BadRequest (Json .obj(" status" -> " Error" , " message" -> JsError .toJson(errors)))) },
38101 submission => {
39- val n : Int = submission.n
40- val labelTypeId : Option [Int ] = submission.labelTypeId
41- val loadedLabelIds : Set [Int ] = submission.loadedLabels.toSet
42- val valOptions : Set [String ] = submission.validationOptions.getOrElse(Seq ()).toSet
43- val regionIds : Set [Int ] = submission.regionIds.getOrElse(Seq ()).toSet
44- val severities : Set [Int ] = submission.severities.getOrElse(Seq ()).toSet
45- val tags : Set [String ] = submission.tags.getOrElse(Seq ()).toSet
46- val userId : String = request.identity.userId
102+ val n : Int = submission.n
103+ val labelType : Option [LabelTypeEnum .Base ] = submission.labelTypeId.flatMap(l => LabelTypeEnum .byId.get(l))
104+ val loadedLabels : Set [Int ] = submission.loadedLabels.toSet
105+ val valOptions : Set [String ] = submission.validationOptions.getOrElse(Seq ()).toSet
106+ val regionIds : Set [Int ] = submission.regionIds.getOrElse(Seq ()).toSet
107+ val severities : Set [Int ] = submission.severities.getOrElse(Seq ()).toSet
108+ val tags : Set [String ] = submission.tags.getOrElse(Seq ()).toSet
109+ val aiValOptions : Set [String ] = submission.aiValidationOptions.getOrElse(Seq ()).toSet
110+ val userId : String = request.identity.userId
47111
48112 // Get labels from LabelTable.
49113 labelService
50- .getGalleryLabels(n, labelTypeId, loadedLabelIds , valOptions, regionIds, severities, tags, userId)
114+ .getGalleryLabels(n, labelType, loadedLabels , valOptions, regionIds, severities, tags, aiValOptions , userId)
51115 .map { labels =>
52116 val jsonList : Seq [JsObject ] = labels.map(l =>
53117 Json .obj(
0 commit comments