Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Register
Sign in
Toggle navigation
Menu
UPSMF
knowledge-platform
Commits
b8349fb3
Commit
b8349fb3
authored
2 years ago
by
lakshmikommalapati
Browse files
Options
Download
Patches
Plain Diff
POC code for bulk upload
parent
89e1db09
bulk-upload-test_excel
Gcloud_fix
Remove_unwantedCode_Gcloud_fix
bulk-upload-comptenecy-mapping
bulk-upload-excelsheet
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
assessment-api/assessment-service/app/controllers/v4/QuestionController.scala
+208
-201
...sment-service/app/controllers/v4/QuestionController.scala
assessment-api/assessment-service/app/handlers/CompetencyExcelParser.scala
+137
-82
...sessment-service/app/handlers/CompetencyExcelParser.scala
with
345 additions
and
283 deletions
+345
-283
assessment-api/assessment-service/app/controllers/v4/QuestionController.scala
+
208
−
201
View file @
b8349fb3
package
controllers.v4
import
akka.actor.
{
ActorRef
,
ActorSystem
}
import
com.twitter.util.Config.intoList
import
controllers.BaseController
import
handlers.
{
CompetencyExcelParser
,
QuestionExcelParser
}
import
org.sunbird.common.dto.
{
Request
,
Response
,
ResponseHandler
}
...
...
@@ -16,207 +15,215 @@ import scala.concurrent.duration.Duration
import
scala.concurrent.
{
Await
,
ExecutionContext
,
Future
}
import
org.slf4j.
{
Logger
,
LoggerFactory
}
import
org.sunbird.cache.impl.RedisCache
import
java.util
class
QuestionController
@Inject
()(
@Named
(
ActorNames
.
QUESTION_ACTOR
)
questionActor
:
ActorRef
,
cc
:
ControllerComponents
,
actorSystem
:
ActorSystem
)(
implicit
exec
:
ExecutionContext
)
extends
BaseController
(
cc
)
{
val
objectType
=
"Question"
val
schemaName
:
String
=
"question"
val
version
=
"1.0"
private
val
logger
:
Logger
=
LoggerFactory
.
getLogger
(
RedisCache
.
getClass
.
getCanonicalName
)
def
create
()
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
AnyRef
]]
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
createQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
CREATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
read
(
identifier
:
String
,
mode
:
Option
[
String
],
fields
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
question
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Object
]]
question
.
putAll
(
headers
)
question
.
putAll
(
Map
(
"identifier"
->
identifier
,
"fields"
->
fields
.
getOrElse
(
""
),
"mode"
->
mode
.
getOrElse
(
"read"
)).
asJava
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
readQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
READ_QUESTION
,
questionActor
,
questionRequest
)
}
def
privateRead
(
identifier
:
String
,
mode
:
Option
[
String
],
fields
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
question
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Object
]]
question
.
putAll
(
headers
)
question
.
putAll
(
Map
(
"identifier"
->
identifier
,
"fields"
->
fields
.
getOrElse
(
""
),
"mode"
->
mode
.
getOrElse
(
"read"
)).
asJava
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
readPrivateQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
READ_PRIVATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
update
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
updateQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
UPDATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
review
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
reviewQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
REVIEW_QUESTION
,
questionActor
,
questionRequest
)
}
def
publish
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
publishQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
PUBLISH_QUESTION
,
questionActor
,
questionRequest
)
}
def
retire
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
question
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Object
]]
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
retireQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
RETIRE_QUESTION
,
questionActor
,
questionRequest
)
}
def
importQuestion
()
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
body
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
body
,
headers
,
QuestionOperations
.
importQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
IMPORT_QUESTION
,
questionActor
,
questionRequest
)
}
def
systemUpdate
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
content
=
body
.
getOrDefault
(
schemaName
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
content
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
content
,
headers
,
QuestionOperations
.
systemUpdateQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
);
getResult
(
ApiId
.
SYSTEM_UPDATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
list
(
fields
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"search"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
question
.
put
(
"fields"
,
fields
.
getOrElse
(
""
))
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
listQuestions
.
toString
)
questionRequest
.
put
(
"identifiers"
,
questionRequest
.
get
(
"identifier"
))
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
LIST_QUESTIONS
,
questionActor
,
questionRequest
)
}
def
reject
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
schemaName
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
rejectQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
REJECT_QUESTION
,
questionActor
,
questionRequest
)
}
def
copy
(
identifier
:
String
,
mode
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
question
.
putAll
(
Map
(
"identifier"
->
identifier
,
"mode"
->
mode
.
getOrElse
(
""
),
"copyType"
->
AssessmentConstants
.
COPY_TYPE_DEEP
).
asJava
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
copyQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
COPY_QUESTION
,
questionActor
,
questionRequest
)
}
//Create question by uploading excel file
def
uploadExcel
()
=
Action
(
parse
.
multipartFormData
)
{
implicit
request
=>
logger
.
info
(
"Inside upload excel"
)
val
competency
=
request
.
body
.
file
(
"file"
)
.
map
{
filePart
=>
val
absolutePath
=
filePart
.
ref
.
path
.
toAbsolutePath
CompetencyExcelParser
.
getCompetency
(
absolutePath
.
toFile
)
}
val
questions
=
request
.
body
.
file
(
"file"
)
.
map
{
filePart
=>
val
absolutePath
=
filePart
.
ref
.
path
.
toAbsolutePath
QuestionExcelParser
.
getQuestions
(
absolutePath
.
toFile
)
}
logger
.
info
(
"questions after parsing "
+
questions
)
val
futures
=
questions
.
get
.
map
(
question
=>
{
val
headers
=
commonHeaders
(
request
.
headers
)
question
.
putAll
(
headers
)
logger
.
info
(
"put headers "
+
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
createQuestion
.
toString
)
logger
.
info
(
"After the questionRequest"
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
logger
.
info
(
"After the setRequestContext"
)
getResponse
(
ApiId
.
CREATE_QUESTION
,
questionActor
,
questionRequest
)
}
)
logger
.
info
(
"After the getResponse"
)
val
f
=
Future
.
sequence
(
futures
).
map
(
results
=>
results
.
map
(
_
.
asInstanceOf
[
Response
]).
groupBy
(
_
.
getResponseCode
.
toString
).
mapValues
(
listResult
=>
{
listResult
.
map
(
result
=>
{
setResponseEnvelope
(
result
)
JavaJsonUtils
.
serialize
(
result
.
getResult
)
})
})).
map
(
f
=>
Ok
(
Json
.
stringify
(
Json
.
toJson
(
f
))).
as
(
"application/json"
))
logger
.
info
(
"in Future sequence"
)
Await
.
result
(
f
,
Duration
.
apply
(
"30s"
))
}
def
createFrameworkMappingData
()
=
Action
(
parse
.
multipartFormData
)
{
implicit
request
=>
val
competency
=
request
.
body
.
file
(
"file"
)
.
map
{
filePart
=>
val
absolutePath
=
filePart
.
ref
.
path
.
toAbsolutePath
CompetencyExcelParser
.
getCompetency
(
absolutePath
.
toFile
)
}
val
futures
=
competency
.
get
.
map
(
competncy
=>
{
val
headers
=
commonHeaders
(
request
.
headers
)
competncy
.
putAll
(
headers
)
logger
.
info
(
"put headers "
+
headers
)
val
questionRequest
=
getRequest
(
competncy
,
headers
,
QuestionOperations
.
createQuestion
.
toString
)
logger
.
info
(
"After the questionRequest"
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
logger
.
info
(
"After the setRequestContext"
)
getResponse
(
ApiId
.
CREATE_QUESTION
,
questionActor
,
questionRequest
)
}
)
val
headers
=
commonHeaders
(
request
.
headers
)
System
.
out
.
println
(
"Headers is "
+
headers
)
val
body
=
requestBody
()
System
.
out
.
println
(
"body is "
+
body
)
val
question
=
body
.
getOrDefault
(
"competency"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
AnyRef
]]
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
bulkUploadFrameworkMapping
.
toString
)
setRequestContext
(
questionRequest
,
version
,
"competency"
,
"competency"
)
getResult
(
ApiId
.
FRAMEWORK_COMPETENCY_QUESTION
,
questionActor
,
questionRequest
)
//[request.type]
}
val
objectType
=
"Question"
val
schemaName
:
String
=
"question"
val
version
=
"1.0"
private
val
logger
:
Logger
=
LoggerFactory
.
getLogger
(
RedisCache
.
getClass
.
getCanonicalName
)
def
create
()
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
AnyRef
]]
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
createQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
CREATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
read
(
identifier
:
String
,
mode
:
Option
[
String
],
fields
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
question
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Object
]]
question
.
putAll
(
headers
)
question
.
putAll
(
Map
(
"identifier"
->
identifier
,
"fields"
->
fields
.
getOrElse
(
""
),
"mode"
->
mode
.
getOrElse
(
"read"
)).
asJava
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
readQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
READ_QUESTION
,
questionActor
,
questionRequest
)
}
def
privateRead
(
identifier
:
String
,
mode
:
Option
[
String
],
fields
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
question
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Object
]]
question
.
putAll
(
headers
)
question
.
putAll
(
Map
(
"identifier"
->
identifier
,
"fields"
->
fields
.
getOrElse
(
""
),
"mode"
->
mode
.
getOrElse
(
"read"
)).
asJava
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
readPrivateQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
READ_PRIVATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
update
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
updateQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
UPDATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
review
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
reviewQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
REVIEW_QUESTION
,
questionActor
,
questionRequest
)
}
def
publish
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
publishQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
PUBLISH_QUESTION
,
questionActor
,
questionRequest
)
}
def
retire
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
question
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Object
]]
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
retireQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
RETIRE_QUESTION
,
questionActor
,
questionRequest
)
}
def
importQuestion
()
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
body
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
body
,
headers
,
QuestionOperations
.
importQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
IMPORT_QUESTION
,
questionActor
,
questionRequest
)
}
def
systemUpdate
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
content
=
body
.
getOrDefault
(
schemaName
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
content
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
content
,
headers
,
QuestionOperations
.
systemUpdateQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
);
getResult
(
ApiId
.
SYSTEM_UPDATE_QUESTION
,
questionActor
,
questionRequest
)
}
def
list
(
fields
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"search"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
question
.
put
(
"fields"
,
fields
.
getOrElse
(
""
))
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
listQuestions
.
toString
)
questionRequest
.
put
(
"identifiers"
,
questionRequest
.
get
(
"identifier"
))
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
LIST_QUESTIONS
,
questionActor
,
questionRequest
)
}
def
reject
(
identifier
:
String
)
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
schemaName
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
rejectQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
questionRequest
.
getContext
.
put
(
"identifier"
,
identifier
)
getResult
(
ApiId
.
REJECT_QUESTION
,
questionActor
,
questionRequest
)
}
def
copy
(
identifier
:
String
,
mode
:
Option
[
String
])
=
Action
.
async
{
implicit
request
=>
val
headers
=
commonHeaders
()
val
body
=
requestBody
()
val
question
=
body
.
getOrDefault
(
"question"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
Object
]];
question
.
putAll
(
headers
)
question
.
putAll
(
Map
(
"identifier"
->
identifier
,
"mode"
->
mode
.
getOrElse
(
""
),
"copyType"
->
AssessmentConstants
.
COPY_TYPE_DEEP
).
asJava
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
copyQuestion
.
toString
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
getResult
(
ApiId
.
COPY_QUESTION
,
questionActor
,
questionRequest
)
}
//Create question by uploading excel file
def
uploadExcel
()
=
Action
(
parse
.
multipartFormData
)
{
implicit
request
=>
logger
.
info
(
"Inside upload excel"
)
val
questions
=
request
.
body
.
file
(
"file"
)
.
map
{
filePart
=>
val
absolutePath
=
filePart
.
ref
.
path
.
toAbsolutePath
QuestionExcelParser
.
getQuestions
(
absolutePath
.
toFile
)
}
logger
.
info
(
"questions after parsing "
+
questions
)
val
futures
=
questions
.
get
.
map
(
question
=>
{
val
headers
=
commonHeaders
(
request
.
headers
)
question
.
putAll
(
headers
)
logger
.
info
(
"put headers "
+
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
createQuestion
.
toString
)
logger
.
info
(
"After the questionRequest"
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
logger
.
info
(
"After the setRequestContext"
)
getResponse
(
ApiId
.
CREATE_QUESTION
,
questionActor
,
questionRequest
)
}
)
logger
.
info
(
"After the getResponse"
)
val
f
=
Future
.
sequence
(
futures
).
map
(
results
=>
results
.
map
(
_
.
asInstanceOf
[
Response
]).
groupBy
(
_
.
getResponseCode
.
toString
).
mapValues
(
listResult
=>
{
listResult
.
map
(
result
=>
{
setResponseEnvelope
(
result
)
JavaJsonUtils
.
serialize
(
result
.
getResult
)
})
})).
map
(
f
=>
Ok
(
Json
.
stringify
(
Json
.
toJson
(
f
))).
as
(
"application/json"
))
logger
.
info
(
"in Future sequence"
)
Await
.
result
(
f
,
Duration
.
apply
(
"30s"
))
}
def
createFrameworkMappingData
()
=
Action
(
parse
.
multipartFormData
)
{
implicit
request
=>
val
competency
=
request
.
body
.
file
(
"file"
)
.
map
{
filePart
=>
val
absolutePath
=
filePart
.
ref
.
path
.
toAbsolutePath
println
(
"createFrameworkMappingData:= "
+
absolutePath
)
// CompetencyExcelParser.getExcelData(absolutePath.toFile)
CompetencyExcelParser
.
getCompetency
(
absolutePath
.
toFile
)
}
val
futures
=
competency
.
get
.
map
(
competncy
=>
{
val
headers
=
commonHeaders
(
request
.
headers
)
logger
.
info
(
"put headers "
+
headers
)
val
questionRequest
=
getRequest
(
competncy
,
headers
,
QuestionOperations
.
createQuestion
.
toString
)
logger
.
info
(
"After the questionRequest"
)
setRequestContext
(
questionRequest
,
version
,
objectType
,
schemaName
)
logger
.
info
(
"After the setRequestContext"
)
getResponse
(
ApiId
.
CREATE_QUESTION
,
questionActor
,
questionRequest
)
}
)
val
headers
=
commonHeaders
(
request
.
headers
)
System
.
out
.
println
(
"Headers is "
+
headers
)
val
body
=
competencyRequestBody
()
//val body = requestBody()
System
.
out
.
println
(
"body is "
+
body
)
val
question
=
body
.
getOrDefault
(
"competency"
,
new
java
.
util
.
HashMap
()).
asInstanceOf
[
java.util.Map
[
String
,
AnyRef
]]
question
.
putAll
(
headers
)
val
questionRequest
=
getRequest
(
question
,
headers
,
QuestionOperations
.
bulkUploadFrameworkMapping
.
toString
)
setRequestContext
(
questionRequest
,
version
,
"competency"
,
"competency"
)
getResult
(
ApiId
.
FRAMEWORK_COMPETENCY_QUESTION
,
questionActor
,
questionRequest
)
logger
.
info
(
"After the getResponse"
)
val
f
=
Future
.
sequence
(
futures
).
map
(
results
=>
results
.
map
(
_
.
asInstanceOf
[
Response
]).
groupBy
(
_
.
getResponseCode
.
toString
).
mapValues
(
listResult
=>
{
listResult
.
map
(
result
=>
{
setResponseEnvelope
(
result
)
JavaJsonUtils
.
serialize
(
result
.
getResult
)
})
})).
map
(
f
=>
Ok
(
Json
.
stringify
(
Json
.
toJson
(
f
))).
as
(
"application/json"
))
logger
.
info
(
"in Future sequence"
)
Await
.
result
(
f
,
Duration
.
apply
(
"30s"
))
}
}
This diff is collapsed.
Click to expand it.
assessment-api/assessment-service/app/handlers/CompetencyExcelParser.scala
+
137
−
82
View file @
b8349fb3
package
handlers
import
org.apache.poi.ss.usermodel.DataFormatter
import
org.apache.poi.xssf.usermodel.
{
XSSFRow
,
XSSFSheet
,
XSSFWorkbook
}
import
org.slf4j.
{
Logger
,
LoggerFactory
}
import
java.io.
{
File
,
FileInputStream
}
import
java.util
import
scala.collection.JavaConverters.
{
asScalaIteratorConverter
,
iterableAsScalaIterableConverter
,
mapAsScalaMapConverter
}
import
scala.collection.mutable
import
scala.collection.mutable.ListBuffer
case
class
Activity
(
code
:
String
,
label
:
String
)
...
...
@@ -12,104 +16,155 @@ object CompetencyExcelParser {
private
val
logger
:
Logger
=
LoggerFactory
.
getLogger
(
getClass
.
getName
)
private
var
getData
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
ListBuffer
[
List
[
java.util.Map
[
String
,
Activity
]]]]
private
var
yearData
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
List
[
java.util.Map
[
String
,
Activity
]]]
private
var
getData
:
List
[
util.Map
[
String
,
AnyRef
]]
=
List
.
empty
def
parseCompetencyData
(
xssFRow
:
XSSFRow
)
=
{
val
competency
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Activity
]]
val
data
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
AnyRef
]]
val
listData
=
new
util
.
ArrayList
[
AnyRef
]()
val
rowContent
=
(
0
until
xssFRow
.
getPhysicalNumberOfCells
)
.
map
(
colId
=>
Option
(
xssFRow
.
getCell
(
colId
)).
getOrElse
(
""
).
toString
).
toList
val
activityCode
=
rowContent
.
apply
(
0
).
trim
val
activityLabel
=
rowContent
.
apply
(
1
).
trim
val
questionCode
=
rowContent
.
apply
(
2
).
trim
val
activityDetails
=
Activity
(
activityCode
,
activityLabel
)
competency
.
put
(
questionCode
,
activityDetails
)
competency
val
function
=
rowContent
.
apply
(
0
).
trim
val
year
=
rowContent
.
apply
(
1
).
trim
val
roleId
=
rowContent
.
apply
(
2
).
trim
val
roleLabel
=
rowContent
.
apply
(
3
).
trim
val
competencyMapping
=
rowContent
.
apply
(
4
).
trim
val
activityId
=
rowContent
.
apply
(
5
).
trim
val
activityLabel
=
rowContent
.
apply
(
6
).
trim
val
competencyId
=
rowContent
.
apply
(
7
).
trim
val
competency
=
rowContent
.
apply
(
8
).
trim
val
competencyLevelId
=
rowContent
.
apply
(
9
).
trim
listData
.
add
(
function
)
listData
.
add
(
year
)
listData
.
add
(
roleId
)
listData
.
add
(
roleLabel
)
listData
.
add
(
activityId
)
listData
.
add
(
activityLabel
)
listData
.
add
(
competencyId
)
listData
.
add
(
competency
)
listData
.
add
(
competencyLevelId
)
//val competencyLevel = rowContent.apply(2).trim
data
.
put
(
competencyMapping
,
listData
)
data
}
def
getCompetenciesData
(
sheet
:
XSSFSheet
,
yearWiseEntries
:
ListBuffer
[
List
[
java.util.Map
[
String
,
Activity
]]])
=
{
logger
.
info
(
"Inside the getCompetenciesData"
)
val
sheetName
=
sheet
.
getSheetName
.
toLowerCase
()
if
(
sheetName
.
startsWith
(
"competencies"
))
{
logger
.
info
(
"Inside the competencies sheet"
)
getData
=
{
(
1
until
sheet
.
getPhysicalNumberOfRows
)
.
filter
(
rowNum
=>
{
val
oRow
=
Option
(
sheet
.
getRow
(
rowNum
))
oRow
match
{
case
Some
(
x
)
=>
{
val
competencyBoolean
=
sheet
.
getRow
(
rowNum
).
getCell
(
4
)
boolean2Boolean
(
"CDM"
.
equals
(
competencyBoolean
.
toString
))
}
case
None
=>
false
}
}).
map
(
rowNum
=>
parseCompetencyData
(
sheet
.
getRow
(
rowNum
))).
toList
/*def getCompetenciesData1(sheet: XSSFSheet): List[util.Map[String, AnyRef]] = {
val column = sheet.asScala.drop(1).map(row =>
if (sheet.getWorkbook.getSheetIndex(sheet) == 1 || sheet.getWorkbook.getSheetIndex(sheet) == 8)
row.getCell(4)
else
row.getCell(5)
)
val formatter = new DataFormatter()
val uniqueCompetencies = column.map(cell => formatter.formatCellValue(cell)).toList
val rows = sheet.asScala.drop(1)
getData = rows.flatMap(row => {
val cell = if (sheet.getWorkbook.getSheetIndex(sheet) == 1 || sheet.getWorkbook.getSheetIndex(sheet) == 8)
row.getCell(4)
else
row.getCell(5)
if (cell != null && uniqueCompetencies.contains(cell.getStringCellValue)) {
//if(row.getCell(1)!=null||row.getCell(2)!=null||row.getCell(2)!=null||row.getCell(4)!=null||row.getCell(5)!=null)
Option(sheet.getRow(row.getRowNum)).map(parseCompetencyData)
} else {
None
}
}
}
).toList
getData
}
}*/
def
getCompetency
(
file
:
File
)
:
List
[
java.util.Map
[
String
,
AnyRef
]]
=
{
logger
.
info
(
"enter into the getCompetency method"
)
val
finalData
:
mutable.Map
[
String
,
List
[
Map
[
String
,
AnyRef
]]]
=
mutable
.
Map
.
empty
try
{
val
workbook
=
new
XSSFWorkbook
(
new
FileInputStream
(
file
))
(
1
until
workbook
.
getNumberOfSheets
)
.
foreach
(
index
=>
{
// if (index ==1) {
getData
=
getCompetenciesData
(
workbook
.
getSheetAt
(
index
))
/*}else {
getData=getCompetenciesDataFromSheet(workbook.getSheetAt(index))
}*/
val
convertedData
=
getData
.
map
(
_
.
asScala
.
toMap
)
finalData
+=
(
workbook
.
getSheetName
(
index
)
->
convertedData
)
getData
=
finalData
.
toList
.
flatMap
{
case
(
_
,
maps
)
=>
maps
.
map
(
convertMap
)
}
})
getData
}
catch
{
case
e
:
Exception
=>
throw
new
Exception
(
"Invalid File"
)
}
}
def
getRowNum
(
sheet
:
XSSFSheet
)
=
{
(
1
until
sheet
.
getPhysicalNumberOfRows
)
.
filter
(
rowNum
=>
{
val
oRow
=
Option
(
sheet
.
getRow
(
rowNum
))
oRow
match
{
case
Some
(
x
)
=>
{
val
competencyBoolean
=
sheet
.
getRow
(
rowNum
).
getCell
(
4
)
boolean2Boolean
(
"CDM"
.
equals
(
competencyBoolean
.
toString
))
}
case
None
=>
false
}
}).
map
(
rowNum
=>
parseCompetency
(
sheet
.
getRow
(
rowNum
))).
toList
}
/*def getCompetenciesData2(sheet: XSSFSheet): List[util.Map[String, AnyRef]] = {
val columnIdx = if (sheet.getWorkbook.getSheetIndex(sheet) == 1 || sheet.getWorkbook.getSheetIndex(sheet) == 8) 4 else 5
val rows = sheet.asScala.drop(1)
val formatter = new DataFormatter()
def
getYearWiseCompetencyMappingData
(
sheet
:
XSSFSheet
)
=
{
val
sheetName
=
sheet
.
getSheetName
.
toLowerCase
()
if
(
sheetName
.
startsWith
(
"first"
))
{
logger
.
info
(
"Inside the first sheet"
)
yearData
=
getRowNum
(
sheet
)
}
else
if
(
sheetName
.
startsWith
(
"second"
))
{
logger
.
info
(
"Inside the second sheet"
)
yearData
=
getRowNum
(
sheet
)
}
else
if
(
sheetName
.
startsWith
(
"third"
))
{
logger
.
info
(
"Inside the third sheet"
)
yearData
=
getRowNum
(
sheet
)
def getCell(row: Row, colIdx: Int): Option[Cell] = {
val cell = row.getCell(colIdx)
Option(cell).filter(c => Option(formatter.formatCellValue(c)).exists(_.nonEmpty))
}
yearData
}
def
getCompetency
(
file
:
File
)
:
List
[
java.util.Map
[
String
,
Activity
]]
=
{
try
{
val
workbook
=
new
XSSFWorkbook
(
new
FileInputStream
(
file
))
(
workbook
.
getNumberOfSheets
-
1
until
-
1
)
.
foreach
(
index
=>
{
if
(
index
==
0
)
{
getData
=
getCompetenciesData
(
workbook
.
getSheetAt
(
index
),
getData
)
}
else
{
getData
=
ListBuffer
.
apply
(
getYearWiseCompetencyMappingData
(
workbook
.
getSheetAt
(
index
)))
}
})
yearData
}
catch
{
case
e
:
Exception
=>
throw
new
Exception
(
"Invalid File"
)
}
}
val uniqueCompetencies = rows.flatMap(row => getCell(row, columnIdx)).map(cell => formatter.formatCellValue(cell)).toList
def
parseCompetency
(
xssFRow
:
XSSFRow
)
=
{
val
competency
=
new
java
.
util
.
HashMap
().
asInstanceOf
[
java.util.Map
[
String
,
Activity
]]
rows.flatMap(row => {
getCell(row, columnIdx).filter(cell => uniqueCompetencies.contains(formatter.formatCellValue(cell))).map(row => Option(sheet.getRow(row.getRowNum)).map(parseCompetencyData)).flatten
}).toList
}*/
val
rowContent
=
(
0
until
xssFRow
.
getPhysicalNumberOfCells
)
.
map
(
colId
=>
Option
(
xssFRow
.
getCell
(
colId
)).
getOrElse
(
""
).
toString
).
toList
def
convertMap
(
map
:
Map
[
String
,
AnyRef
])
:
util.Map
[
String
,
AnyRef
]
=
{
val
javaMap
=
new
util
.
HashMap
[
String
,
AnyRef
]()
map
.
foreach
{
case
(
k
,
v
)
=>
javaMap
.
put
(
k
,
v
)
}
javaMap
}
val
activityCode
=
rowContent
.
apply
(
0
).
trim
val
activityLabel
=
rowContent
.
apply
(
1
).
trim
val
questionCode
=
rowContent
.
apply
(
2
).
trim
val
activityDetails
=
Activity
(
activityCode
,
activityLabel
)
competency
.
put
(
questionCode
,
activityDetails
)
competency
def
getCompetenciesData
(
sheet
:
XSSFSheet
)
:
List
[
util.Map
[
String
,
AnyRef
]]
=
{
/* val column = sheet.asScala.drop(1).map(row =>
if (sheet.getWorkbook.getSheetIndex(sheet) == 1 || sheet.getWorkbook.getSheetIndex(sheet) == 8)
row.getCell(4)
else
row.getCell(5)
).toList*/
/*val formatter = new DataFormatter()
val uniqueCompetencies = column.map(cell => formatter.formatCellValue(cell)).toList*/
val
rows
=
sheet
.
asScala
.
drop
(
1
)
getData
=
rows
.
flatMap
(
row
=>
{
val
rowValue
=
{
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
1
||
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
8
)
{
if
(!
row
.
getCell
(
4
).
getStringCellValue
.
isEmpty
)
row
.
getCell
(
4
)
}
else
if
(!
row
.
getCell
(
5
).
getStringCellValue
.
isEmpty
)
row
.
getCell
(
5
)
}
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
1
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
2
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
3
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
4
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
5
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
6
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
7
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
8
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
if
(
sheet
.
getWorkbook
.
getSheetIndex
(
sheet
)
==
9
&&
rowValue
!=
null
)
Option
(
sheet
.
getRow
(
row
.
getRowNum
)).
map
(
parseCompetencyData
)
else
{
None
}
}).
toList
getData
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets