KeyPairGenerator : generated public key parameters - public-key

I generated a public key and I printed the parameters, p,q,g and y of the public key. I executed the program a few times and realized that only the y value changes but not the values for p,q,g.
Why is it so?
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
public class Key {
public static void main (String args[]) {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA","SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();
System.out.println("Public Key: " + pubKey);
} catch (Exception e) {
}
}
}
Output
Public Key: Sun DSA Public Key
Parameters:
p:
fd7f5381 1d751229 52df4a9c 2eece4e7 f611b752 3cef4400 c31e3f80 b6512669
455d4022 51fb593d 8d58fabf c5f5ba30 f6cb9b55 6cd7813b 801d346f f26660b7
6b9950a5 a49f9fe8 047b1022 c24fbba9 d7feb7c6 1bf83b57 e7c6a8a6 150f04fb
83f6d3c5 1ec30235 54135a16 9132f675 f3ae2b61 d72aeff2 2203199d d14801c7
q:
9760508f 15230bcc b292b982 a2eb840b f0581cf5
g:
f7e1a085 d69b3dde cbbcab5c 36b857b9 7994afbb fa3aea82 f9574c0b 3d078267
5159578e bad4594f e6710710 8180b449 167123e8 4c281613 b7cf0932 8cc8a6e1
3c167a8b 547c8d28 e0a3ae1e 2bb3a675 916ea37f 0bfa2135 62f1fb62 7a01243b
cca4f1be a8519089 a883dfe1 5ae59f06 928b665e 807b5525 64014c3b fecf492a
y:
ddd3c52e 670efbe0 6755e3cd 13b28019 06565e33 fcab6d9a b2ab548c e361f971
2a771a8b 0bd7d58e 7c09d310 d8004d2a de8833fd b1a60d59 4441d60d 3d051ceb
0f9d3a08 ba71b804 4a42a901 98acedf7 10feba49 f2bb5274 934b63c1 1561bd81
1f3bd998 d6475a72 2df9c963 7bcdc53c 86078a8a fa74c903 4d184bc9 20c67aaa

As the Wikipedia Article on DSA states:
The algorithm parameters (p, q, g) may be shared between different users of the system.
I checked and I get the exact same p, q and g on my system as well.
Also the Oracle docs state the parameters as well.

Related

Read and store game state as CSV

Thanks to the great help from Tenfour04, I've got wonderful code for handling CSV files.
However, I am in trouble like followings.
How to call these functions?
How to initialize 2-dimensional array variables?
Below is the code that finally worked.
MainActivity.kt
package com.surlofia.csv_tenfour04_1
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import java.io.File
import java.io.IOException
import com.surlofia.csv_tenfour04_1.databinding.ActivityMainBinding
var chk_Q_Num: MutableList<Int> = mutableListOf (
0,
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
)
var chk_Q_State: MutableList<String> = mutableListOf (
"z",
"a", "b", "c", "d", "e",
"f", "g", "h", "i", "j"
)
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// Load saved data at game startup. It will be invalid if performed by other activities.
val filePath = filesDir.path + "/chk_Q.csv"
val file = File(filePath)
binding.fileExists.text = isFileExists(file).toString()
if (isFileExists(file)) {
val csvIN = file.readAsCSV()
for (i in 0 .. 10) {
chk_Q_Num[i] = csvIN[i][0].toInt()
chk_Q_State[i] = csvIN[i][1]
}
}
// Game Program Run
val csvOUT = mutableListOf(
mutableListOf("0","OK"),
mutableListOf("1","OK"),
mutableListOf("2","OK"),
mutableListOf("3","Not yet"),
mutableListOf("4","Not yet"),
mutableListOf("5","Not yet"),
mutableListOf("6","Not yet"),
mutableListOf("7","Not yet"),
mutableListOf("8","Not yet"),
mutableListOf("9","Not yet"),
mutableListOf("10","Not yet")
)
var tempString = ""
for (i in 0 .. 10) {
csvOUT[i][0] = chk_Q_Num[i].toString()
csvOUT[i][1] = "OK"
tempString = tempString + csvOUT[i][0] + "-->" + csvOUT[i][1] + "\n"
}
binding.readFile.text = tempString
// and save Data
file.writeAsCSV(csvOUT)
}
// https://www.techiedelight.com/ja/check-if-a-file-exists-in-kotlin/
private fun isFileExists(file: File): Boolean {
return file.exists() && !file.isDirectory
}
#Throws(IOException::class)
fun File.readAsCSV(): List<List<String>> {
val splitLines = mutableListOf<List<String>>()
forEachLine {
splitLines += it.split(", ")
}
return splitLines
}
#Throws(IOException::class)
fun File.writeAsCSV(values: List<List<String>>) {
val csv = values.joinToString("\n") { line -> line.joinToString(", ") }
writeText(csv)
}
}
chk_Q.csv
0,0
1,OK
2,OK
3,Not yet
4,Not yet
5,Not yet
6,Not yet
7,Not yet
8,Not yet
9,Not yet
10,Not yet
1. How to call these functions?
The code below seems work well.
Did I call these funtions in right way?
Or are there better ways to achieve this?
read
if (isFileExists(file)) {
val csvIN = file.readAsCSV()
for (i in 0 .. 10) {
chk_Q_Num[i] = csvIN[i][0].toInt()
chk_Q_State[i] = csvIN[i][1]
}
}
write
file.writeAsCSV(csvOUT)
2. How to initialize 2-dimensional array variables?
val csvOUT = mutableListOf(
mutableListOf("0","OK"),
mutableListOf("1","OK"),
mutableListOf("2","OK"),
mutableListOf("3","Not yet"),
mutableListOf("4","Not yet"),
mutableListOf("5","Not yet"),
mutableListOf("6","Not yet"),
mutableListOf("7","Not yet"),
mutableListOf("8","Not yet"),
mutableListOf("9","Not yet"),
mutableListOf("10","Not yet")
)
I would like to know the clever way to use a for loop instead of writing specific values one by one.
For example, something like bellow.
val csvOUT = mutableListOf(mutableListOf())
for (i in 0 .. 10) {
csvOUT[i][0] = i
csvOUT[i][1] = "OK"
}
But this gave me the following error message:
Not enough information to infer type variable T
It would be great if you could provide an example of how to execute this for beginners.
----- Added on June 15, 2022. -----
[Question 1]
Regarding initialization, I got an error "keep stopping" when I executed the following code.
The application is forced to terminate.
Why is this?
val csvOUT: MutableList<MutableList<String>> = mutableListOf(mutableListOf())
for (i in 0 .. 10) {
csvOUT[i][0] = "$i"
csvOUT[i][1] = "OK"
}
[Error Message]
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.surlofia.csv_endzeit_01/com.surlofia.csv_endzeit_01.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
In my opinion there are basically two parts to your question. First you need an understanding of the Kotlin type system including generics. Secondly you want some knowledge about approaches to the problem at hand.
type-system and generics
The function mutableListOf you're using is generic and thus needs a single type parameter T, as can be seen by definition its taken from the documentation:
fun <T> mutableListOf(): MutableList<T>
Most of the time the Kotlin compiler is quite good at type-inference, that is guessing the type used based on the context. For example, I do not need to provide a type explicitly in the following example, because the Kotlin compiler can infer the type from the usage context.
val listWithInts = mutableListOf(3, 7)
The infered type is MutableList<Int>.
However, sometimes this might not be what one desires. For example, I might want to allow null values in my list above. To achieve this, I have to tell the compiler that it should not only allow Int values to the list but also null values, widening the type from Int to Int?. I can achieve this in at least two ways.
providing a generic type parameter
val listWithNullableInts = mutableListOf<Int?>(3, 7)
defining the expected return type explicitly
val listWithNullableInts: MutableList<Int?> = mutableListOf(3, 7)
In your case the compiler does NOT have enough information to infer the type from the usage context. Thus you either have to provide it that context, e.g. by passing values of a specific type to the function or using one of the two options named above.
initialization of multidimensional arrays
There are questions and answers on creating multi-dimensional arrays in Kotlin on StackOverflow already.
One solution to your problem at hand might be the following.
val csvOUT: MutableList<MutableList<String>> = mutableListOf(mutableListOf())
for (i in 0 .. 10) {
csvOUT[i][0] = "$i"
csvOUT[i][1] = "OK"
}
You help the Kotlin compiler by defining the expected return type explicitly and then add the values as Strings to your 2D list.
If the dimensions are fixed, you might want to use fixed-size Arrays instead.
val csvArray = Array(11) { index -> arrayOf("$index", "OK") }
In both solutions you convert the Int index to a String however.
If the only information you want to store for each level is a String, you might as well use a simple List<String and use the index of each entry as the level number, e.g.:
val csvOut = List(11) { "OK" }
val levelThree = csvOut[2] // first index of List is 0
This would also work with more complicated data structures instead of Strings. You simply would have to adjust your fun File.writeAsCSV(values: List<List<String>>) to accept a different type as the values parameter.
Assume a simple data class you might end up with something along the lines of:
data class LevelState(val state: String, val timeBeaten: Instant?)
val levelState = List(11) { LevelState("OK", Instant.now()) }
fun File.writeAsCSV(values: List<LevelState>) {
val csvString = values
.mapIndexed { index, levelState -> "$index, ${levelState.state}, ${levelState.timeBeaten}" }
.joinToString("\n")
writeText(csvString)
}
If you prefer a more "classical" imperative approach, you can populate your 2-dimensional Array / List using a loop like for in.
val list: MutableList<MutableList<String>> = mutableListOf() // list is now []
for (i in 0..10) {
val innerList: MutableList<String> = mutableListOf()
innerList.add("$i")
innerList.add("OK")
innerList.add("${Instant.now()}")
list.add(innerList)
// list is after first iteration [ ["0", "OK", "2022-06-15T07:03:14.315Z"] ]
}
The syntax listName[index] = value is just syntactic sugar for the operator overload of the set operator, see the documentation on MutableList for example.
You cannot access an index, that has not been populated before, e.g. during the List's initialization or by using add; or else you're greeted with a IndexOutOfBoundsException.
If you want to use the set operator, one option is to use a pre-populated Array as such:
val array: Array<Array<String>>> = Array(11) {
Array(3) { "default" }
} // array is [ ["default, "default", "default"], ...]
array[1][2] = "myValue"
However, I wouldn't recommend this approach, as it might lead to left over, potentially invalid initial data, in case one misses to replace a value.

how to evaluate String to identify duplicate keys

I have an input string in groovy which is not strictly JSON.
String str = "['OS_Node':['eth0':'1310','eth0':'1312']]"
My issue is to identify the duplicate "eth0" . I tried to convert this into map using Eval.me(), but it automatically removes the duplicate key "eth0" and gives me a Map.
What is the best way for me to identify the presence of duplicate key ?
Note: there could be multiple OS_Node1\2\3\ entries.. need to identify duplicates in each of them ?
Is there any JSON api that can be used? or need to use logic based on substring() ?
One way to solve this could be to cheat a little and replace colons with commas which would transform the maps into lists and then do a recursive search for duplicates:
def str = "['OS_Node':['eth0':'1310','eth0':'1312'], 'OS_Node':['eth1':'1310','eth1':'1312']]"
def tree = Eval.me(str.replaceAll(":", ","))
def dupes = findDuplicates(tree)
dupes.each { println it }
def findDuplicates(t, path=[], dupes=[]) {
def seen = [] as Set
t.collate(2).each { k, v ->
if (k in seen) dupes << [path: path + k]
seen << k
if (v instanceof List) findDuplicates(v, path+k, dupes)
}
dupes
}
when run, prints:
─➤ groovy solution.groovy
[path:[OS_Node, eth0]]
[path:[OS_Node]]
[path:[OS_Node, eth1]]
i.e. the method finds all paths to duplicated keys where "path" is defined as the key sequence required to navigate to the duplicate key.
The function returns a list of maps which you can then do whatever you wish with. Should be noted that the "OS_Node" key is with this logic treated as a duplicate but you could easily filter that out as a step after this function call.
First of all, the string you have there is not JSON - not only due to
the duplicate keys, but also by the use of [] for maps. This looks
a lot more like a groovy map literal. So if this is your custom format
and you can not do anything against it, I'd write a small parser for this,
because sooner or later edge cases or quoting problems come around the
corner.
#Grab("com.github.petitparser:petitparser-core:2.3.1")
import org.petitparser.tools.GrammarDefinition
import org.petitparser.tools.GrammarParser
import org.petitparser.parser.primitive.CharacterParser as CP
import org.petitparser.parser.primitive.StringParser as SP
import org.petitparser.utils.Functions as F
class MappishGrammerDefinition extends GrammarDefinition {
MappishGrammerDefinition() {
define("start", ref("map"))
define("map",
CP.of("[" as Character)
.seq(ref("kv-pairs"))
.seq(CP.of("]" as Character))
.map{ it[1] })
define("kv-pairs",
ref("kv-pair")
.plus()
.separatedBy(CP.of("," as Character))
.map{ it.collate(2)*.first()*.first() })
define("kv-pair",
ref('key')9
.seq(CP.of(":" as Character))
.seq(ref('val'))
.map{ [it[0], it[2]] })
define("key",
ref("quoted"))
define("val",
ref("quoted")
.or(ref("map")))
define("quoted",
CP.anyOf("'")
.seq(SP.of("\\''").or(CP.pattern("^'")).star().flatten())
.seq(CP.anyOf("'"))
.map{ it[1].replace("\\'", "'") })
}
// Helper for `def`, which is a keyword in groovy
void define(s, p) { super.def(s,p) }
}
println(new GrammarParser(new MappishGrammerDefinition()).parse("['OS_Node':['eth0':'1310','eth0':'1312'],'OS_Node':['eth0':'42']]").get())
// → [[OS_Node, [[eth0, 1310], [eth0, 1312]]], [OS_Node, [[eth0, 42]]]]

Managing JSON Array format in MQL4 script

I would need to get data from inside a JSON Array.
I use a call to WebRequest() function to get an order list from my db, thru a nodejs(api) and then I got the following JSON-data format e.g:
[{"orderid": 123556,
"ordertype": 0,
"ordercurrency": "EURUSD",
"orderdt": "2016-12-03 03:00:00"
},
{"orderid": 123457,
"ordertype": 0,
"ordercurrency": "GBPUSD",
"orderdt": "2016-12-03 03:15:00"
}
]
Any idea how to transform it to a normal array in order to manage data?
Thank you.
/Koul
JSON Serialization and Deserialization library works pretty well.
You can include it and finish your task within a minute or get an inspiration from its code.
https://www.mql5.com/en/code/13663
https://www.mql5.com/en/forum/65320
Let's assume you have the JSON data you pasted in a string variable named data.
#include <JAson.mqh>
// Object
CJAVal json;
// Load in and deserialize the data
json.Deserialize(data);
// Try to access the data elements
Alert(json["orderid"].ToInt());
Alert(json["ordercurrency"].ToStr());
How?Easy:
JSON-format is a "string-ified" representation of data-elements, put into a common container, be it a uchar[] or a string on the MQL4 side.
So, let's create a JSON-parser, coherent with the subset of the standard JSON-format syntax rules.
1) Search for all db-output "row"-s ( encapsulated in {...} )
2) Decode all db-output "column"-s ( encoded in ( known ) "KEY":VALUE pairs )
3) Store decoded values into any kind of target representation,
int orderIdNoARRAY[];
int orderTypeARRAY[];
string orderCurrARRAY[];
string orderDateARRAY[];
int anArrayStackPTR = 0
...
orderIdNoARRAY[anArrayStackPTR] = aDecodedOrderID; // 123556
orderTypeARRAY[anArrayStackPTR] = aDecodedOrderTYPE; // 0
orderCurrARRAY[anArrayStackPTR] = aDecodedOrderCURR; // "EURUSD"
orderDateARRAY[anArrayStackPTR] = aDecodedOrderDATE; // "2016-12-03 03:00:00"
or
#define oID 0
#define oTYPE 1
#define oCCY 2
#define oDATE 3
string stringDataFromJSON[10000,4];
int anArrayStackPTR = 0
...
stringDataFromJSON[anArrayStackPTR,oID] = (string)aDecodedOrderID; // 123556
stringDataFromJSON[anArrayStackPTR,oTYPE] = (string)aDecodedOrderTYPE; // 0
stringDataFromJSON[anArrayStackPTR,oCCY] = (string)aDecodedOrderCURR; // "EURUSD"
stringDataFromJSON[anArrayStackPTR,oDATE] = (string)aDecodedOrderDATE; // "2016-12-03 03:00:00"
or
struct aDB_RECORD{
int aDB_oID;
int aDB_oTYPE;
string aDB_oCCY;
string aDB_oDATE;
};
aDB_RECORD anArrayOfSTRUCTs[];
int anArrayStackPTR = 0
...
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oID = aDecodedOrderID; // 123556
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oTYPE = aDecodedOrderTYPE; // 0
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oCCY = aDecodedOrderCCY; // "EURUSD"
anArrayOfSTRUCTs[anArrayStackPTR].aDB_oDATE = aDecodedOrderDATE; // "2016-12-03 03:00:00"
Yes, it is that easy!
Yes, JAson - JSON Serialization and Deserialization library works is good popular library:
https://www.mql5.com/en/code/13663
https://www.mql5.com/en/forum/65320
I wrote documentation with more details (different from https://www.mql5.com/en/code/13663) and added unit tests:
https://github.com/vivazzi/JAson.
It may be useful for you.
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include <JAson.mqh>
int OnInit()
{
CJAVal data;
string aCookieHOLDER = NULL,
aHttpHEADERs;
char postBYTEs[],
replBYTEs[];
int aRetCODE;
string aTargetURL ="https://apitester.ir/api/Categories";
/* to enable access to the URL-> pointed server,
you should append "https://api.myjson.com/bins/56z28"
to the list of allowed URLs in
( Main Menu -> Tools -> Options, tab "Expert Advisors" ):
*/
ResetLastError();
int aTIMEOUT = 5000;
aRetCODE = WebRequest("GET",
aTargetURL,
aCookieHOLDER,
NULL,
aTIMEOUT,
postBYTEs,
0,
replBYTEs,
aHttpHEADERs
);
if ( aRetCODE == NULL )
{ Print( "Error in WebRequest(). Error code = ", GetLastError() );
}
else
{
string k=data.GetStr(replBYTEs,0,replBYTEs.Size());
data.Deserialize(k,0);
Comment(data.m_e[0].m_e[1].m_sv);
}
return(INIT_SUCCEEDED);
}
//after Deserialize you access any of row in json with m_e[0 to total size of objects] and access to any items of object with m_e[0 to count of keys]

Getting method return value from many to many in grails

I have a problem with getting one of the object via grails here is my code:
Grails domain:
class Circle
{
String description
int howMany
static hasMany [rects: Rectangle]
constraints =
{
answer: blank false, nullable: false
}
Rectangle getRect(int x, int y)
{
Rectangle rect = Rectangle.findWhere(x: x, y: y);
rect
}
}
class Rectangle
{
int x
int y
String description
Circle circle
constraints =
{
circle blank: false, nullable: false
description blank: false, nullable: false
}
}
I pass my circle object via controller to the view, of course object isn't null and have correct values.
grails controller:
...
return [figure: circle, howManyX: xCount, howManyY: yCount]
...
xCount and yCount tells me how many times I have to render my template in the view:
view:
...
<g:render template="figureTemplate" model="[bean: figure, posX: x, posY: y]"/> // x and y are a variables in grails foreach loop
...
and my template:
<input type="text" name="text_${poY}_${posX}" text="${fieldValue(bean: bean, field: getRect(posX, posY).name)}
I know my code isn't correct because fieldValue is not a method to get a value from object method, but from object variable. So it's possible to get somehow value returning via method? Or I should use something different?
I add two dimensional array which contains Rectangles on specific indexes, and return it from controller to view, and in the view I get values from this array and this works perfectly for me:
controller:
...
return [figure: circle, howManyX: xCount, howManyY: yCount, arr: doubleArray]
...
view:
<g:render template="figureTemplate" model="[bean: figure, posX: x, posY: y, rect: arr[x][y]"/>
template:
<input type="text" name="text_${poY}_${posX}" text="${rect.description}/>
It looks like you are not referencing the objects by the name assigned in the controller.
If you return the following map in your controller:
return [figure: circle, howManyX: xCount, howManyY: yCount]
then you must reference the objects by the map KEY.
So if you want to reference the "circle" object, you need to use ${figure}
in your view code and to references "xCount" , ${howManyX}, and so forth.

Play framework - save data to database by parameters in URL

I'm new to the Play Framework, and Scala language. I want to save some data to database only by running URL with specified parameters.
For example I want to run url like:
/DeviceData?device_ID=1&insertDate=2013-01-01&windDirection=50&device_ID=1&insertDate=2013-01-02&windDirection=5
and after that in the database two new records would be inserted (with Device_ID, insertDate and windDirection).
Right now I'm trying to save only one record at once (I don't know how to read list of elements and save them) but event that it's not working. There is no error, it's just not inserted.
DeviceData model
case class DeviceData(data_ID: Long, device_ID: Long, insertDate: String, windDirection: Double)
object DeviceData{
var deviceDataList = new HashMap[Long, DeviceData]
var data_ID = 0L
def nextId(): Long = { data_ID += 1; data_ID}
def createDeviceData(device_ID: Long, insertDate: String, windDirection: Double) :Unit = {
DB.withConnection { implicit connection =>
SQL(
"""
INSERT INTO devicedata(device_ID, insertDate, windDirection)
VALUES ({device_ID}, {insertDate}, {windDirection})
"""
).
on("device_ID" -> device_ID, "insertDate" -> insertDate, "windDirection" -> windDirection).
executeInsert()
}
}
def list(): List[DeviceData] = { deviceDataList.values.toList }
}
DeviceDatas controller
object DeviceDatas extends Controller {
val deviceDataForm = Form(
tuple(
"device_ID" -> of[Long],
"insertDate" -> nonEmptyText,
"windDirection" -> of[Double]
)
)
def listDeviceData() = Action {
Ok(views.html.deviceData(DeviceData.list(), deviceDataForm))
}
def createDeviceData(device_ID: Long, insertDate: String, windDirection: Double) = Action { implicit request =>
deviceDataForm.bindFromRequest.fold(
errors => BadRequest(views.html.deviceData(DeviceData.list(), errors)),
{ case (device_ID, insertDate, windDirection) => {
DeviceData.createDeviceData(device_ID, insertDate, windDirection)
Redirect(routes.DeviceDatas.listDeviceData)
}
}
)
}
}
deviceData.scala.html - it's simple one, just to check if there is any new inserted record.
#(deviceDatas: List[DeviceData], deviceDataForm: Form[(Long, String, Double)])
#import helper._
#main("DeviceDatas"){
<h3>#deviceDatas.size DeviceData(s)</h3>
}
routes file for /deviceDatas
GET /deviceDatas controllers.DeviceDatas.listDeviceData
POST /deviceDatas controllers.DeviceDatas.createDeviceData(device_ID: Long, insertDate: String, windDirection: Double)
Could You help me with that how to insert the data into database, and if there is any possibility to put list of elements with few records to insert. Also what's the best way to insert DateTime (yyyy-MM-dd hh:mm:ss) into URL parameters in Play Framework? I'm stuck and I don't know how to do it.
UPDATED
Thanks Zim-Zam O'Pootertoot for the answer. Unfortunately I need to use parameters, because I'm sending the data through the router. But anyway one more thanks to You because I'll use json in the future.
I decided to not use List of parameter as I said before, but for one new record I'm sending one request (for example: to add 6 new records to the database I need to run 6 times URL on the router:
/DeviceData?device_ID=1&insertDate=2013-01-01&windDirection=50
And my problem was solved by changing the route file to:
GET /deviceDatas controllers.DeviceDatas.listDeviceData
GET /deviceDatas controllers.DeviceDatas.createDeviceData(device_ID: Long, insertDate: String, windDirection: Double)
To pass in data for multiple records, and also to pass in DateTime data, send the data in the request's json body instead of as url params
http://www.playframework.com/documentation/2.2.x/ScalaBodyParsers
http://www.playframework.com/documentation/2.2.x/ScalaJson
Action(parse.json) { implicit request =>
(request.body \ "records") match {
case arr: JsArray => arr.value.foreach(json => {
val deviceId = (json \ "device_ID").as[Long]
val date = (json \ "insertDate").as[String]
val windDirection = (json \ "windDirection").as[Double]
// insert data in database
})
case _ => throw new IllegalArgumentException("Invalid Json: records must be a JsArray")
}}
The json for your records might look something like
{"records" : [
{"device_ID" : 123, "insertDate" : "2014-03-01 12:00:00", "windDirection" : 123.45},
{"device_ID" : 456, "insertDate" : "2014-03-02 12:00:00", "windDirection" : 54.321}]}