Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Antti Väyrynen
/
Moya
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit f028befe
authored
Jun 02, 2014
by
Tuukka Kivilahti
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/more-meta' into 'master'
Metatukea eventusereille Vilkuili läpi
@tkfftk
2 parents
3cb7ca22
72eae1db
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
216 additions
and
96 deletions
code/MoyaUtilities/src/main/java/fi/codecrew/moya/utilities/JsonUtils.java
code/MoyaUtilsTest/.classpath
code/MoyaUtilsTest/.settings/org.eclipse.wst.common.component
code/MoyaUtilsTest/pom.xml
code/MoyaUtilsTest/src/META-INF/MANIFEST.MF
code/MoyaUtilsTest/src/fi/codecrew/moya/utilities/JsonUtilsTest.java → code/MoyaUtilsTest/src/test/java/fi/codecrew/moya/utilities/JsonUtilsTest.java
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/AbstractRestViewV1.java
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/EventUserRestViewV1.java
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/PrintedCardRestViewV1.java
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/UserRestViewV1.java
code/MoyaUtilities/src/main/java/fi/codecrew/moya/utilities/JsonUtils.java
View file @
f028bef
...
...
@@ -7,6 +7,7 @@ import javax.json.Json;
import
javax.json.JsonObject
;
import
javax.json.JsonObjectBuilder
;
import
javax.json.JsonValue
;
import
javax.json.JsonValue.ValueType
;
public
class
JsonUtils
{
...
...
@@ -18,15 +19,22 @@ public class JsonUtils {
* @param path
* @return
*/
public
static
Json
Object
getSubObject
(
JsonObject
jsonObject
,
public
static
Json
Value
getSubObject
(
JsonObject
jsonObject
,
List
<
String
>
path
)
{
JsonObject
sub
=
jsonObject
;
JsonValue
sub
=
jsonObject
;
// Burrow into object hierarchy
for
(
String
s
:
path
)
{
sub
=
sub
.
getJsonObject
(
s
);
if
(
sub
==
null
)
break
;
if
(
sub
.
getValueType
()
==
ValueType
.
OBJECT
)
{
JsonObject
subObject
=
(
JsonObject
)
sub
;
sub
=
subObject
.
get
(
s
);
}
else
{
// Trying to get sub-object of something not an object. Bad.
return
null
;
}
}
// If this thing does not exist, return an empty object.
if
(
sub
==
null
)
{
sub
=
Json
.
createObjectBuilder
().
build
();
}
...
...
code/MoyaUtilsTest/.classpath
View file @
f028bef
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry
kind=
"src"
output=
"target/
classes"
path=
"src
"
>
<classpathentry
kind=
"src"
output=
"target/
test-classes"
path=
"src/test/java
"
>
<attributes>
<attribute
name=
"optional"
value=
"true"
/>
<attribute
name=
"maven.pomderived"
value=
"true"
/>
...
...
@@ -23,5 +23,11 @@
<attribute
name=
"maven.pomderived"
value=
"true"
/>
</attributes>
</classpathentry>
<classpathentry
kind=
"src"
output=
"target/classes"
path=
"src/main/java"
>
<attributes>
<attribute
name=
"optional"
value=
"true"
/>
<attribute
name=
"maven.pomderived"
value=
"true"
/>
</attributes>
</classpathentry>
<classpathentry
kind=
"output"
path=
"target/classes"
/>
</classpath>
code/MoyaUtilsTest/.settings/org.eclipse.wst.common.component
View file @
f028bef
<?xml version="1.0" encoding="UTF-8"?>
<project-modules
id=
"moduleCoreId"
project-version=
"1.5.0"
>
<wb-module
deploy-name=
"MoyaUtilitiesTest"
>
<wb-resource
deploy-path=
"/"
source-path=
"/src"
/>
<wb-resource
deploy-path=
"/"
source-path=
"/src
/test/java
"
/>
</wb-module>
</project-modules>
code/MoyaUtilsTest/pom.xml
View file @
f028bef
...
...
@@ -4,7 +4,6 @@
<artifactId>
moya-utils-test
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<build>
<sourceDirectory>
src
</sourceDirectory>
<plugins>
<plugin>
<artifactId>
maven-compiler-plugin
</artifactId>
...
...
code/MoyaUtilsTest/src/META-INF/MANIFEST.MF
deleted
100644 → 0
View file @
3cb7ca2
Manifest-Version: 1.0
Class-Path:
code/MoyaUtilsTest/src/fi/codecrew/moya/utilities/JsonUtilsTest.java
→
code/MoyaUtilsTest/src/
test/java/
fi/codecrew/moya/utilities/JsonUtilsTest.java
View file @
f028bef
...
...
@@ -6,11 +6,14 @@ import java.util.ArrayList;
import
javax.json.Json
;
import
javax.json.JsonObject
;
import
javax.json.JsonReader
;
import
javax.json.JsonValue
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
fi.codecrew.moya.utilities.JsonUtils
;
public
class
JsonUtilsTest
{
private
JsonObject
jsonObject
(
String
jsonText
)
{
...
...
@@ -27,12 +30,11 @@ public class JsonUtilsTest {
@Test
public
final
void
testGetSubObject
()
{
JsonObject
meta
=
jsonObject
(
"{\"foo\":\"bar\",\"baz\":{\"quuz\":\"plop\"}}"
);
JsonObject
expected
=
jsonObject
(
"{\"quuz\":\"plop\"}"
);
ArrayList
<
String
>
path
=
new
ArrayList
<
String
>();
path
.
add
(
"baz"
);
JsonObject
actual
=
JsonUtils
.
getSubObject
(
meta
,
path
);
JsonValue
expected
=
jsonObject
(
"{\"quuz\":\"plop\"}"
);
JsonValue
actual
=
JsonUtils
.
getSubObject
(
meta
,
path
);
Assert
.
assertEquals
(
expected
.
toString
(),
actual
.
toString
());
}
...
...
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/AbstractRestViewV1.java
0 → 100644
View file @
f028bef
package
fi
.
codecrew
.
moya
.
rest
.
meta
.
v1
;
import
java.io.StringReader
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.json.Json
;
import
javax.json.JsonObject
;
import
javax.json.JsonReader
;
import
javax.json.JsonValue
;
import
javax.ws.rs.WebApplicationException
;
import
javax.ws.rs.core.PathSegment
;
import
javax.ws.rs.core.Response
;
import
fi.codecrew.moya.model.EntityMeta
;
import
fi.codecrew.moya.utilities.JsonUtils
;
public
abstract
class
AbstractRestViewV1
{
/**
* Convert List<PathSegment> to List<String>
*
* @param path
* List<PathSegment>
* @return List<String>
*/
public
List
<
String
>
pathToStrings
(
List
<
PathSegment
>
path
)
{
ArrayList
<
String
>
stringList
=
new
ArrayList
<
String
>(
path
.
size
());
for
(
PathSegment
s
:
path
)
{
String
segmentString
=
s
.
getPath
();
if
(
segmentString
!=
null
&&
!
segmentString
.
isEmpty
())
{
stringList
.
add
(
segmentString
);
}
}
return
stringList
;
}
public
JsonObject
parseJson
(
String
jsonString
)
{
JsonReader
jsonReader
=
Json
.
createReader
(
new
StringReader
(
jsonString
));
JsonObject
newData
=
jsonReader
.
readObject
();
return
newData
;
}
protected
void
setEntityMeta
(
EntityMeta
entity
,
List
<
PathSegment
>
keys
,
String
jsonString
)
{
if
(
entity
==
null
)
{
throw
new
WebApplicationException
(
Response
.
Status
.
NOT_FOUND
);
}
JsonObject
meta
=
entity
.
getMeta
();
if
(
meta
==
null
)
{
meta
=
Json
.
createObjectBuilder
().
build
();
}
JsonObject
alteredMeta
=
JsonUtils
.
alterSubObject
(
meta
,
pathToStrings
(
keys
),
parseJson
(
jsonString
));
if
(
alteredMeta
!=
null
)
{
entity
.
setMeta
(
alteredMeta
);
}
}
protected
String
getEntityMeta
(
EntityMeta
entity
,
List
<
PathSegment
>
keys
)
{
if
(
entity
==
null
)
{
throw
new
WebApplicationException
(
Response
.
Status
.
NOT_FOUND
);
}
JsonObject
meta
=
entity
.
getMeta
();
if
(
meta
==
null
)
{
meta
=
Json
.
createObjectBuilder
().
build
();
}
JsonValue
subValue
=
JsonUtils
.
getSubObject
(
meta
,
pathToStrings
(
keys
));
if
(
subValue
==
null
)
{
subValue
=
Json
.
createObjectBuilder
().
build
();
}
return
subValue
.
toString
()
+
"\n"
;
}
}
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/EventUserRestViewV1.java
0 → 100644
View file @
f028bef
package
fi
.
codecrew
.
moya
.
rest
.
meta
.
v1
;
import
java.util.List
;
import
javax.ejb.EJB
;
import
javax.enterprise.context.RequestScoped
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.PUT
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.PathSegment
;
import
fi.codecrew.moya.beans.UserBeanLocal
;
import
fi.codecrew.moya.model.EventUser
;
@RequestScoped
@Path
(
"/meta/v1/eventuser"
)
@Consumes
({
MediaType
.
APPLICATION_JSON
})
@Produces
({
MediaType
.
APPLICATION_JSON
+
"; charset=UTF-8"
})
public
class
EventUserRestViewV1
extends
AbstractRestViewV1
{
@EJB
UserBeanLocal
userBean
;
@GET
@Path
(
"/{id}/{path:.*}"
)
public
String
getMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"path"
)
List
<
PathSegment
>
path
)
{
EventUser
eventUser
=
userBean
.
findByEventUserId
(
id
);
return
getEntityMeta
(
eventUser
,
path
);
}
@PUT
@Path
(
"/{id}/{path:.*}"
)
public
void
putMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"path"
)
List
<
PathSegment
>
path
,
String
jsonString
)
{
EventUser
eventUser
=
userBean
.
findByEventUserId
(
id
);
setEntityMeta
(
eventUser
,
path
,
jsonString
);
userBean
.
mergeChanges
(
eventUser
);
}
@POST
@Path
(
"/{id}/{path:.*}"
)
public
void
postMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"path"
)
List
<
PathSegment
>
path
,
String
jsonString
)
{
EventUser
eventUser
=
userBean
.
findByEventUserId
(
id
);
setEntityMeta
(
eventUser
,
path
,
jsonString
);
userBean
.
mergeChanges
(
eventUser
);
}
}
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/PrintedCardRestViewV1.java
View file @
f028bef
package
fi
.
codecrew
.
moya
.
rest
.
meta
.
v1
;
import
java.io.StringReader
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.ejb.EJB
;
import
javax.enterprise.context.RequestScoped
;
import
javax.json.Json
;
import
javax.json.JsonObject
;
import
javax.json.JsonReader
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.PUT
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
...
...
@@ -20,101 +16,42 @@ import javax.ws.rs.core.PathSegment;
import
fi.codecrew.moya.beans.CardTemplateBeanLocal
;
import
fi.codecrew.moya.model.PrintedCard
;
import
fi.codecrew.moya.utilities.JsonUtils
;
@RequestScoped
@Path
(
"/meta/v1/printedcard"
)
@Consumes
({
MediaType
.
APPLICATION_JSON
,
MediaType
.
APPLICATION_XML
})
@Consumes
({
MediaType
.
APPLICATION_JSON
})
@Produces
({
MediaType
.
APPLICATION_JSON
+
"; charset=UTF-8"
})
public
class
PrintedCardRestViewV1
{
public
class
PrintedCardRestViewV1
extends
AbstractRestViewV1
{
@EJB
CardTemplateBeanLocal
cardTemplateBeanLocal
;
/**
* Convert List<PathSegment> to List<String>
*
* @param path
* List<PathSegment>
* @return List<String>
*/
public
List
<
String
>
pathToStrings
(
List
<
PathSegment
>
path
)
{
ArrayList
<
String
>
stringList
=
new
ArrayList
<
String
>(
path
.
size
());
for
(
PathSegment
s
:
path
)
{
stringList
.
add
(
s
.
getPath
());
}
return
stringList
;
}
/**
* Metadata for entity.
*
* @param id
* @param keys
* The path segments after card id
* @return
*/
@GET
@Path
(
"/{id}/{keys:.*}"
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
String
getPrintedCardMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"keys"
)
List
<
PathSegment
>
keys
)
{
@Path
(
"/{id}/{path:.*}"
)
public
String
getMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"path"
)
List
<
PathSegment
>
path
)
{
// Get the card's metadata
PrintedCard
printedCard
=
cardTemplateBeanLocal
.
findCard
(
id
);
JsonObject
meta
=
printedCard
.
getMeta
();
// Must be something
if
(
meta
==
null
)
{
meta
=
Json
.
createObjectBuilder
().
build
();
}
// Remove lone empty string
if
(
keys
.
size
()
==
1
&&
keys
.
get
(
0
).
getPath
().
isEmpty
())
{
keys
=
new
ArrayList
<
PathSegment
>();
}
return
getEntityMeta
(
printedCard
,
path
);
}
// Get the subobject
JsonObject
subObject
=
JsonUtils
.
getSubObject
(
meta
,
pathToStrings
(
keys
));
@PUT
@Path
(
"/{id}/{path:.*}"
)
public
void
putMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"path"
)
List
<
PathSegment
>
path
,
String
jsonString
)
{
return
subObject
.
toString
()
+
"\n"
;
PrintedCard
printedCard
=
cardTemplateBeanLocal
.
findCard
(
id
);
setEntityMeta
(
printedCard
,
path
,
jsonString
);
cardTemplateBeanLocal
.
saveCard
(
printedCard
);
}
/**
* Alter metadata in one subproperty of the object.
*
* @param id
* @param keys
* @return
*/
@POST
@Path
(
"/{id}/{keys:.*}"
)
@Consumes
(
MediaType
.
APPLICATION_JSON
)
public
String
setPrintedCardMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"keys"
)
List
<
PathSegment
>
keys
,
String
jsonData
)
{
@Path
(
"/{id}/{path:.*}"
)
public
void
postMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"path"
)
List
<
PathSegment
>
path
,
String
jsonString
)
{
// Get PrintedCard and it's metadata
PrintedCard
printedCard
=
cardTemplateBeanLocal
.
findCard
(
id
);
JsonObject
meta
=
printedCard
.
getMeta
();
// If no metadata yet, make empty metadata object
if
(
meta
==
null
)
{
meta
=
Json
.
createObjectBuilder
().
build
();
}
// Parse the new json data to be inserted
JsonReader
jsonReader
=
Json
.
createReader
(
new
StringReader
(
jsonData
));
JsonObject
newData
=
jsonReader
.
readObject
();
// Merge the new json data into existing metadata object
JsonObject
alteredMeta
=
JsonUtils
.
alterSubObject
(
meta
,
pathToStrings
(
keys
),
newData
);
// Save the changed meta back to database
printedCard
.
setMeta
(
alteredMeta
);
setEntityMeta
(
printedCard
,
path
,
jsonString
);
cardTemplateBeanLocal
.
saveCard
(
printedCard
);
return
null
;
}
}
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/UserRestViewV1.java
0 → 100644
View file @
f028bef
package
fi
.
codecrew
.
moya
.
rest
.
meta
.
v1
;
import
java.util.List
;
import
javax.ejb.EJB
;
import
javax.enterprise.context.RequestScoped
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.GET
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.PathSegment
;
import
fi.codecrew.moya.beans.UserBeanLocal
;
import
fi.codecrew.moya.model.User
;
@RequestScoped
@Path
(
"/meta/v1/user"
)
@Consumes
({
MediaType
.
APPLICATION_JSON
})
@Produces
({
MediaType
.
APPLICATION_JSON
+
"; charset=UTF-8"
})
public
class
UserRestViewV1
extends
AbstractRestViewV1
{
@EJB
UserBeanLocal
userBean
;
@GET
@Path
(
"/{id}/{path:.*}"
)
public
String
getMeta
(
@PathParam
(
"id"
)
Integer
id
,
@PathParam
(
"path"
)
List
<
PathSegment
>
path
)
{
User
user
=
userBean
.
getUser
(
id
);
return
getEntityMeta
(
user
,
path
);
}
// There is no way to merge/save User entity through the UserBean.
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment