Skip to content

Commit

Permalink
fixes-#39 (#46)
Browse files Browse the repository at this point in the history
* added initial user data

* updating user requests

* automatially bootstraps users

* used more formal parsing

* admins bootstrapped and updated

* updated

* added proper fields

* updated

* added proper private versus public list calls

* added is public

* added user

* added shared user

* monior cleanup
  • Loading branch information
nathandunn authored Feb 27, 2021
1 parent 745c94a commit 106ad77
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 11 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ dependencies {
compile "org.grails.plugins:views-json"
compile "org.grails.plugins:views-json-templates"
compileOnly "io.micronaut:micronaut-inject-groovy"
console "org.grails:grails-console"
implementation group: 'com.nimbusds', name: 'nimbus-jose-jwt', version: '9.7'
implementation group: 'com.google.api-client', name: 'google-api-client', version: '1.31.2'
implementation group: 'com.google.api-client', name: 'google-api-client-jackson2', version: '1.20.0'
profile "org.grails.profiles:rest-api"
runtime "org.glassfish.web:el-impl:2.1.2-b03"
runtime "com.h2database:h2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.xena.analysis

import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.NOT_FOUND
import static org.springframework.http.HttpStatus.NO_CONTENT
import static org.springframework.http.HttpStatus.OK
import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY

import grails.gorm.transactions.ReadOnly
import grails.gorm.transactions.Transactional

@ReadOnly
class AuthenticatedUserController {

AuthenticatedUserService authenticatedUserService

static responseFormats = ['json', 'xml']
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond authenticatedUserService.list(params), model:[authenticatedUserCount: authenticatedUserService.count()]
}

def show(Long id) {
respond authenticatedUserService.get(id)
}

@Transactional
def save(AuthenticatedUser authenticatedUser) {
if (authenticatedUser == null) {
render status: NOT_FOUND
return
}
if (authenticatedUser.hasErrors()) {
transactionStatus.setRollbackOnly()
respond authenticatedUser.errors
return
}

try {
authenticatedUserService.save(authenticatedUser)
} catch (ValidationException e) {
respond authenticatedUser.errors
return
}

respond authenticatedUser, [status: CREATED, view:"show"]
}

@Transactional
def update(AuthenticatedUser authenticatedUser) {
if (authenticatedUser == null) {
render status: NOT_FOUND
return
}
if (authenticatedUser.hasErrors()) {
transactionStatus.setRollbackOnly()
respond authenticatedUser.errors
return
}

try {
authenticatedUserService.save(authenticatedUser)
} catch (ValidationException e) {
respond authenticatedUser.errors
return
}

respond authenticatedUser, [status: OK, view:"show"]
}

@Transactional
def delete(Long id) {
if (id == null || authenticatedUserService.delete(id) == null) {
render status: NOT_FOUND
return
}

render status: NO_CONTENT
}
}
48 changes: 39 additions & 9 deletions grails-app/controllers/org/xena/analysis/GmtController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class GmtController {
GmtService gmtService
AnalysisService analysisService
TpmAnalysisService tpmAnalysisService
UserService userService

static responseFormats = ['json', 'xml']
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE",deleteByMethodAndName: "DELETE", analyzeGmt: "POST"]
Expand Down Expand Up @@ -88,21 +89,43 @@ class GmtController {

}

@Transactional
def names(String method) {
println "method: ${method}"
def gmtList = Gmt.executeQuery(" select g.name,g.geneSetCount,g.availableTpmCount,count(r) from Gmt g left outer join g.results r group by g")
println "gmtlist ${gmtList}"
// println "req: ${request.getHeader('Authorization')}"
def publicGmtList = Gmt.executeQuery(" select g.name,g.geneSetCount,g.availableTpmCount,g.isPublic,count(r) from Gmt g left outer join g.results r where g.isPublic = 't' group by g")
println "gmt list: ${publicGmtList}"

if(request.getHeader('Authorization')){
AuthenticatedUser user = userService.getUserFromRequest(request)
if(user){
def privateList = []
if(user.role == RoleEnum.ADMIN){
privateList = Gmt.executeQuery(" select g.name,g.geneSetCount,g.availableTpmCount,g.isPublic,count(r),u from Gmt g left outer join g.results r join g.authenticatedUser u where g.isPublic != 't' group by g, u")
}
else
if(user.role == RoleEnum.USER){
privateList = Gmt.executeQuery(" select g.name,g.geneSetCount,g.availableTpmCount,g.isPublic,count(r),u from Gmt g left outer join g.results r join g.authenticatedUser u where g.authenticatedUser=:user group by g, u",[user:user])
}
publicGmtList = publicGmtList + privateList
}
}


// println "gmtlist ${publicGmtList as JSON}"
JSONArray jsonArray = new JSONArray()
gmtList.sort{ a,b -> a[0].toString().compareTo(b[0].toString())} .each { def gmtEntry ->
publicGmtList.sort{ a,b -> a[0].toString().compareTo(b[0].toString())} .each { def gmtEntry ->
def obj = new JSONObject()
obj.name = gmtEntry[0]
obj.geneCount = gmtEntry[1]
// obj.hash = it.hash
// obj.id = it.id
obj.method = method
obj.availableCount = gmtEntry[2]
obj.readyCount = gmtEntry[3]
obj.public = gmtEntry[3]
obj.readyCount = gmtEntry[4]
obj.ready = obj.availableCount == obj.readyCount
if(gmtEntry.size()>5){
obj.user = gmtEntry[5].firstName + " " + gmtEntry[5].lastName
}
jsonArray.add(obj)
}
render jsonArray as JSON
Expand All @@ -115,22 +138,29 @@ class GmtController {

@Transactional
def store() {

AuthenticatedUser user = userService.getUserFromRequest(request)
if(!user){
throw new RuntimeException("Not authorized")
}


def json = request.JSON
String method = json.method
String gmtname = json.gmtname
String gmtDataHash = json.gmtdata.md5()
def geneCount = json.gmtdata.split("\n").findAll{it.split("\t").size()>2 }.size()


println "stroring with method '${method}' and gmt name '${gmtname}' '${gmtDataHash}"
// println "stroring with method '${method}' and gmt name '${gmtname}' '${gmtDataHash}"
Gmt gmt = Gmt.findByName(gmtname)
if (gmt == null) {
def sameDataGmt = Gmt.findByHashAndMethod(gmtDataHash,method)
if(sameDataGmt){
gmt = new Gmt(name: gmtname, hash: gmtDataHash, data: sameDataGmt.data, method: method, geneSetCount: geneCount)
gmt = new Gmt(name: gmtname, hash: gmtDataHash, data: sameDataGmt.data, method: method, geneSetCount: geneCount,authenticatedUser:user,isPublic: false)
}
else{
gmt = new Gmt(name: gmtname, hash: gmtDataHash, data: json.gmtdata, method: method, geneSetCount: geneCount)
gmt = new Gmt(name: gmtname, hash: gmtDataHash, data: json.gmtdata, method: method, geneSetCount: geneCount,authenticatedUser:user,isPublic: false)
}
gmt.save(failOnError: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class UrlMappings {
}
}

"/user"(resources:'authenticatedUser')


// "/result/test"(controller: 'result', action: 'test')
// "/result/analyze"(controller: 'result', action: 'analyze')
"/"(controller: 'application', action: 'index')
Expand Down
21 changes: 21 additions & 0 deletions grails-app/domain/org/xena/analysis/AuthenticatedUser.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.xena.analysis

class AuthenticatedUser {

static constraints = {
email email: true,nullable: false,blank: false,unique: true
firstName nullable: true
lastName nullable: true
role blank: false,nullable: false
}

String firstName
String lastName
String email
RoleEnum role //

static hasMany = [
gmts:Gmt
]

}
4 changes: 4 additions & 0 deletions grails-app/domain/org/xena/analysis/Gmt.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ class Gmt {
String data
int geneSetCount
int availableTpmCount // count from defaultGeneSet on initial load
AuthenticatedUser authenticatedUser
Boolean isPublic

// use gene names as key
String stats // { 'ABC':{mean: 0.11212, std: 0.272 }, 'DEF': { mean:0.17, std:0.3 } }

static constraints = {
name blank: false, unique: true
stats nullable: true
authenticatedUser nullable: true
isPublic nullable: true
}

static mapping = {
Expand Down
19 changes: 19 additions & 0 deletions grails-app/domain/org/xena/analysis/RoleEnum.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.xena.analysis

enum RoleEnum {

USER("user",10),
BANNED("banned",1),
INACTIVE("inactive",5),
ADMIN("admin",100);

private String display; // pertains to the 1.0 value
private Integer rank;

RoleEnum(String display , int rank) {
this.display = display;
this.rank = rank;
}


}
6 changes: 5 additions & 1 deletion grails-app/init/xena/analysis/grails/BootStrap.groovy
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package xena.analysis.grails

import org.xena.analysis.AnalysisService
import org.xena.analysis.AuthenticatedUser
import org.xena.analysis.CohortService
import org.xena.analysis.RoleEnum
import org.xena.analysis.UserService

class BootStrap {

CohortService cohortService
UserService userService

def init = { servletContext ->
cohortService.validateCohorts()
userService.createAdmins()
}
def destroy = {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.xena.analysis

import grails.gorm.services.Service

@Service(AuthenticatedUser)
interface AuthenticatedUserService {

AuthenticatedUser get(Serializable id)

List<AuthenticatedUser> list(Map args)

Long count()

AuthenticatedUser delete(Serializable id)

AuthenticatedUser save(AuthenticatedUser authenticatedUser)

}
Loading

0 comments on commit 106ad77

Please sign in to comment.