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 2210a671
authored
Jun 03, 2014
by
Tuukka Kivilahti
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into vectoinfo
2 parents
1aa69f6c
6ba9b1c9
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
525 additions
and
97 deletions
code/MoyaBeans/ejbModule/fi/codecrew/moya/beans/NetworkAssociationBean.java
code/MoyaBeans/ejbModule/fi/codecrew/moya/facade/NetworkAssociationFacade.java
code/MoyaBeansClient/ejbModule/fi/codecrew/moya/beans/NetworkAssociationBeanLocal.java
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/WebContent/networkassociation/index.xhtml
code/MoyaWeb/src/fi/codecrew/moya/rest/NetworkAssociationRestView.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/MoyaWeb/src/fi/codecrew/moya/rest/pojo/NetworkAssociationInfoPojo.java
code/MoyaWeb/src/fi/codecrew/moya/rest/pojo/NetworkAssociationInfoResponseRoot.java
code/MoyaWeb/src/fi/codecrew/moya/rest/pojo/NetworkAssociationInfolistResponseRoot.java
code/MoyaBeans/ejbModule/fi/codecrew/moya/beans/NetworkAssociationBean.java
View file @
2210a67
...
...
@@ -19,6 +19,7 @@ import fi.codecrew.moya.model.EventUser;
import
fi.codecrew.moya.model.GroupMembership
;
import
fi.codecrew.moya.model.NetworkAssociation
;
import
fi.codecrew.moya.model.Place
;
import
fi.codecrew.moya.model.PrintedCard
;
import
fi.codecrew.moya.model.Role
;
/**
...
...
@@ -44,6 +45,9 @@ public class NetworkAssociationBean implements NetworkAssociationBeanLocal {
private
EventBean
eventBean
;
@EJB
private
CardTemplateBean
cardBean
;
@EJB
private
PermissionBean
permissionBean
;
public
NetworkAssociationBean
()
{}
...
...
@@ -83,6 +87,21 @@ public class NetworkAssociationBean implements NetworkAssociationBeanLocal {
if
(
authUser
==
null
)
throw
new
Exception
(
"INVALID_USER_OR_PASSWORD"
);
return
tryAssociateInternal
(
authUser
,
ip
,
mac
,
code
,
codeRequired
);
}
@Override
@RolesAllowed
(
NetworkAssociationPermission
.
S_CAN_ADMINISTER_ASSOCIATIONS
)
public
NetworkAssociation
tryAssociate
(
String
usercode
,
String
ip
,
String
mac
,
String
code
,
Boolean
codeRequired
)
throws
Exception
{
EventUser
authUser
=
userBean
.
getUser
(
usercode
);
if
(
authUser
==
null
)
throw
new
Exception
(
"INVALID_USERCODE"
);
return
tryAssociateInternal
(
authUser
,
ip
,
mac
,
code
,
codeRequired
);
}
private
NetworkAssociation
tryAssociateInternal
(
EventUser
authUser
,
String
ip
,
String
mac
,
String
code
,
boolean
codeRequired
)
throws
Exception
{
NetworkAssociation
association
;
HashSet
<
IAppPermission
>
userPerms
=
buildPermsFor
(
authUser
);
...
...
@@ -249,5 +268,36 @@ public class NetworkAssociationBean implements NetworkAssociationBeanLocal {
private
Place
resolvePlaceFromCode
(
String
code
)
{
if
(
code
==
null
)
return
null
;
return
barcodeBean
.
getPlaceFromTextCode
(
code
);
}
}
@Override
@RolesAllowed
(
NetworkAssociationPermission
.
S_CAN_ADMINISTER_ASSOCIATIONS
)
public
NetworkAssociation
getActiveAssociationByIP
(
String
ipAddress
)
{
List
<
NetworkAssociation
>
na
=
this
.
networkAssociationFacade
.
findActiveAssociationsByIP
(
eventBean
.
getCurrentEvent
(),
ipAddress
);
if
(
na
.
size
()
>
0
)
return
na
.
get
(
0
);
else
return
null
;
}
@Override
@RolesAllowed
(
NetworkAssociationPermission
.
S_CAN_ADMINISTER_ASSOCIATIONS
)
public
List
<
NetworkAssociation
>
getActiveAssociationsByHorizon
(
Boolean
activate
,
String
horizon
)
{
if
(
activate
)
activatePendingAssociationsByHorizon
(
horizon
);
return
networkAssociationFacade
.
findByStatusAndHorizon
(
eventBean
.
getCurrentEvent
(),
NetworkAssociationStatus
.
ACTIVE
,
horizon
);
}
private
void
activatePendingAssociationsByHorizon
(
String
horizon
)
{
List
<
NetworkAssociation
>
netAssocs
=
networkAssociationFacade
.
findByStatusAndHorizon
(
eventBean
.
getCurrentEvent
(),
NetworkAssociationStatus
.
PENDING
,
horizon
);
for
(
NetworkAssociation
na
:
netAssocs
)
{
na
.
setStatus
(
NetworkAssociationStatus
.
ACTIVE
);
na
.
setModifyTime
(
Calendar
.
getInstance
());
}
}
}
code/MoyaBeans/ejbModule/fi/codecrew/moya/facade/NetworkAssociationFacade.java
View file @
2210a67
...
...
@@ -100,4 +100,19 @@ public class NetworkAssociationFacade extends IntegerPkGenericFacade<NetworkAsso
return
getEm
().
createQuery
(
cq
).
getResultList
();
}
public
List
<
NetworkAssociation
>
findByStatusAndHorizon
(
LanEvent
event
,
NetworkAssociationStatus
status
,
String
horizon
)
{
CriteriaBuilder
cb
=
getEm
().
getCriteriaBuilder
();
CriteriaQuery
<
NetworkAssociation
>
cq
=
cb
.
createQuery
(
NetworkAssociation
.
class
);
Root
<
NetworkAssociation
>
root
=
cq
.
from
(
NetworkAssociation
.
class
);
cq
.
where
(
cb
.
and
(
cb
.
equal
(
root
.
get
(
NetworkAssociation_
.
event
),
event
),
cb
.
equal
(
root
.
get
(
NetworkAssociation_
.
status
),
status
),
cb
.
like
(
root
.
get
(
NetworkAssociation_
.
ip
),
horizon
+
"%"
)
)
);
return
getEm
().
createQuery
(
cq
).
getResultList
();
}
}
code/MoyaBeansClient/ejbModule/fi/codecrew/moya/beans/NetworkAssociationBeanLocal.java
View file @
2210a67
...
...
@@ -23,4 +23,12 @@ public interface NetworkAssociationBeanLocal {
void
dropAssociationById
(
Integer
associd
);
NetworkAssociation
getActiveAssociationByIP
(
String
ipAddress
);
NetworkAssociation
tryAssociate
(
String
usercode
,
String
ip
,
String
mac
,
String
code
,
Boolean
codeRequired
)
throws
Exception
;
List
<
NetworkAssociation
>
getActiveAssociationsByHorizon
(
Boolean
activate
,
String
horizon
);
}
code/MoyaUtilities/src/main/java/fi/codecrew/moya/utilities/JsonUtils.java
View file @
2210a67
...
...
@@ -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 @
2210a67
<?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 @
2210a67
<?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 @
2210a67
...
...
@@ -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 @
1aa69f6
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 @
2210a67
...
...
@@ -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/WebContent/networkassociation/index.xhtml
View file @
2210a67
...
...
@@ -12,6 +12,7 @@
</f:metadata>
<ui:define
name=
"content"
>
<h:form>
<p:commandButton
style=
"width: 0px; height: 0px;"
value=
"#{i18n['networkassociation.create_association']}"
action=
"#{networkAssociationView.createAssociation}"
update=
"@form"
/>
<p:messages
autoUpdate=
"true"
redisplay=
"false"
></p:messages>
<h1>
#{i18n['networkassociation.current_associations']}
</h1>
<p:dataTable
var=
"activeAssoc"
value=
"#{networkAssociationView.activeAssociations}"
>
...
...
code/MoyaWeb/src/fi/codecrew/moya/rest/NetworkAssociationRestView.java
View file @
2210a67
...
...
@@ -16,6 +16,9 @@ import fi.codecrew.moya.beans.NetworkAssociationBeanLocal;
import
fi.codecrew.moya.enums.NetworkAssociationStatus
;
import
fi.codecrew.moya.model.NetworkAssociation
;
import
fi.codecrew.moya.rest.pojo.NetworkAssociationActionPojo
;
import
fi.codecrew.moya.rest.pojo.NetworkAssociationInfoPojo
;
import
fi.codecrew.moya.rest.pojo.NetworkAssociationInfoResponseRoot
;
import
fi.codecrew.moya.rest.pojo.NetworkAssociationInfolistResponseRoot
;
import
fi.codecrew.moya.rest.pojo.NetworkAssociationResponseRoot
;
@RequestScoped
...
...
@@ -57,6 +60,67 @@ public class NetworkAssociationRestView {
return
resp
;
}
@POST
@Path
(
"/codeauth"
)
@Produces
({
MediaType
.
APPLICATION_JSON
})
public
NetworkAssociationResponseRoot
codeAuth
(
@FormParam
(
"usercode"
)
String
usercode
,
@FormParam
(
"ip"
)
String
ip
,
@FormParam
(
"mac"
)
String
mac
,
@FormParam
(
"code"
)
String
code
,
@FormParam
(
"coderequired"
)
Boolean
codeRequired
)
{
NetworkAssociationResponseRoot
resp
=
new
NetworkAssociationResponseRoot
();
try
{
NetworkAssociation
na
=
networkAssociationBean
.
tryAssociate
(
usercode
,
ip
,
mac
,
code
,
codeRequired
);
if
(
na
.
getStatus
().
equals
(
NetworkAssociationStatus
.
ACTIVE
))
resp
.
getAdditions
().
add
(
new
NetworkAssociationActionPojo
(
na
.
getIP
(),
na
.
getMAC
()));
else
resp
.
getPendings
().
add
(
new
NetworkAssociationActionPojo
(
na
.
getIP
(),
na
.
getMAC
()));
}
catch
(
Exception
e
)
{
resp
.
getResult
().
setResultCode
(
0
);
if
(
e
.
getMessage
()
!=
null
&&
e
.
getMessage
()
!=
""
)
{
resp
.
getResult
().
setMessage
(
e
.
getMessage
());
}
else
{
resp
.
getResult
().
setMessage
(
"UNKNOWN_ERROR"
);
}
}
return
resp
;
}
@GET
@Path
(
"/get_association_infos"
)
@Produces
({
MediaType
.
APPLICATION_JSON
})
public
NetworkAssociationInfolistResponseRoot
getAssociationInfos
()
{
NetworkAssociationInfolistResponseRoot
nairr
=
new
NetworkAssociationInfolistResponseRoot
();
for
(
NetworkAssociation
na
:
networkAssociationBean
.
getActiveAssociations
(
false
))
{
nairr
.
getAssociations
().
add
(
new
NetworkAssociationInfoPojo
(
na
));
}
return
nairr
;
}
@GET
@Path
(
"/get_association_info_by_ip/{ipAddress}"
)
@Produces
({
MediaType
.
APPLICATION_JSON
})
public
NetworkAssociationInfoResponseRoot
getAssociationInfos
(
@PathParam
(
"ipAddress"
)
String
ipAddress
)
{
NetworkAssociation
na
=
networkAssociationBean
.
getActiveAssociationByIP
(
ipAddress
);
NetworkAssociationInfoResponseRoot
nairr
;
if
(
na
!=
null
)
{
nairr
=
new
NetworkAssociationInfoResponseRoot
(
new
NetworkAssociationInfoPojo
(
na
)
);
}
else
{
nairr
=
new
NetworkAssociationInfoResponseRoot
();
}
return
nairr
;
}
@GET
@Path
(
"/get_all/{activate}"
)
@Produces
({
MediaType
.
APPLICATION_JSON
})
...
...
@@ -65,6 +129,7 @@ public class NetworkAssociationRestView {
)
{
NetworkAssociationResponseRoot
resp
=
new
NetworkAssociationResponseRoot
();
try
{
if
(
activate
==
null
)
throw
new
Exception
(
"INVALID_PARAMETER_FOR_ACTIVATE"
);
...
...
@@ -82,6 +147,33 @@ public class NetworkAssociationRestView {
}
}
@GET
@Path
(
"/get_all/{horizon}/{activate}"
)
@Produces
({
MediaType
.
APPLICATION_JSON
})
public
NetworkAssociationResponseRoot
getAllByHorizon
(
@PathParam
(
"activate"
)
Boolean
activate
,
@PathParam
(
"horizon"
)
String
horizon
)
{
NetworkAssociationResponseRoot
resp
=
new
NetworkAssociationResponseRoot
();
try
{
if
(
activate
==
null
)
throw
new
Exception
(
"INVALID_PARAMETER_FOR_ACTIVATE"
);
List
<
NetworkAssociation
>
activeAssociations
=
networkAssociationBean
.
getActiveAssociationsByHorizon
(
activate
,
horizon
);
for
(
NetworkAssociation
na
:
activeAssociations
)
{
resp
.
getAdditions
().
add
(
new
NetworkAssociationActionPojo
(
na
.
getIP
(),
na
.
getMAC
()));
}
return
resp
;
}
catch
(
Exception
e
)
{
resp
.
getResult
().
setResultCode
(
0
);
resp
.
getResult
().
setMessage
(
e
.
getMessage
());
return
resp
;
}
}
@POST
@Path
(
"/get_status_by_ip_mac"
)
@Produces
({
MediaType
.
APPLICATION_JSON
})
...
...
code/MoyaWeb/src/fi/codecrew/moya/rest/meta/v1/AbstractRestViewV1.java
0 → 100644
View file @
2210a67
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 @
2210a67
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 @
2210a67
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 @
2210a67
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.
}
code/MoyaWeb/src/fi/codecrew/moya/rest/pojo/NetworkAssociationInfoPojo.java
0 → 100644
View file @
2210a67
package
fi
.
codecrew
.
moya
.
rest
.
pojo
;
import
java.util.Calendar
;
import
javax.xml.bind.annotation.XmlElement
;
import
fi.codecrew.moya.model.NetworkAssociation
;
public
class
NetworkAssociationInfoPojo
{
private
String
createTime
;
private
String
modifyTime
;
private
String
ipAddress
;
private
String
macAddress
;
private
Integer
placeId
;
private
Integer
eventuserId
;
public
NetworkAssociationInfoPojo
(
NetworkAssociation
na
)
{
this
.
createTime
=
na
.
getCreateTime
().
getTime
().
toString
();
this
.
modifyTime
=
na
.
getModifyTime
().
getTime
().
toString
();
this
.
ipAddress
=
na
.
getIP
();
this
.
macAddress
=
na
.
getMAC
();
if
(
na
.
getPlace
()
!=
null
)
this
.
placeId
=
na
.
getPlace
().
getId
();
else
this
.
placeId
=
null
;
this
.
eventuserId
=
na
.
getEventUser
().
getId
();
}
public
NetworkAssociationInfoPojo
()
{
this
.
createTime
=
null
;
this
.
modifyTime
=
null
;
this
.
ipAddress
=
null
;
this
.
macAddress
=
null
;
this
.
placeId
=
null
;
this
.
eventuserId
=
null
;
}
@XmlElement
(
name
=
"createTime"
)
public
String
getCreateTime
()
{
return
createTime
;
}
public
void
setCreateTime
(
String
createTime
)
{
this
.
createTime
=
createTime
;
}
@XmlElement
(
name
=
"modifyTime"
)
public
String
getModifyTime
()
{
return
modifyTime
;
}
public
void
setModifyTime
(
String
modifyTime
)
{
this
.
modifyTime
=
modifyTime
;
}
@XmlElement
(
name
=
"ipAddress"
)
public
String
getIpAddress
()
{
return
ipAddress
;
}
public
void
setIpAddress
(
String
ipAddress
)
{
this
.
ipAddress
=
ipAddress
;
}
@XmlElement
(
name
=
"macAddress"
)
public
String
getMacAddress
()
{
return
macAddress
;
}
public
void
setMacAddress
(
String
macAddress
)
{
this
.
macAddress
=
macAddress
;
}
@XmlElement
(
name
=
"placeId"
)
public
Integer
getPlaceId
()
{
return
placeId
;
}
public
void
setPlaceId
(
Integer
placeId
)
{
this
.
placeId
=
placeId
;
}
@XmlElement
(
name
=
"eventuserId"
)
public
Integer
getEventuserId
()
{
return
eventuserId
;
}
public
void
setEventuserId
(
Integer
eventuserId
)
{
this
.
eventuserId
=
eventuserId
;
}
}
code/MoyaWeb/src/fi/codecrew/moya/rest/pojo/NetworkAssociationInfoResponseRoot.java
0 → 100644
View file @
2210a67
package
fi
.
codecrew
.
moya
.
rest
.
pojo
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.xml.bind.annotation.XmlRootElement
;
@XmlRootElement
public
class
NetworkAssociationInfoResponseRoot
{
private
NetworkAssociationInfoPojo
association
;
public
NetworkAssociationInfoResponseRoot
(
NetworkAssociationInfoPojo
naip
)
{
this
.
association
=
naip
;
}
public
NetworkAssociationInfoResponseRoot
()
{
this
.
association
=
null
;
}
public
NetworkAssociationInfoPojo
getAssociation
()
{
return
association
;
}
public
void
setAssociation
(
NetworkAssociationInfoPojo
association
)
{
this
.
association
=
association
;
}
}
code/MoyaWeb/src/fi/codecrew/moya/rest/pojo/NetworkAssociationInfolistResponseRoot.java
0 → 100644
View file @
2210a67
package
fi
.
codecrew
.
moya
.
rest
.
pojo
;
import
java.util.ArrayList
;
import
java.util.List
;
import
javax.xml.bind.annotation.XmlRootElement
;
@XmlRootElement
public
class
NetworkAssociationInfolistResponseRoot
{
private
List
<
NetworkAssociationInfoPojo
>
associations
;
public
NetworkAssociationInfolistResponseRoot
()
{
this
.
associations
=
new
ArrayList
<>();
}
public
List
<
NetworkAssociationInfoPojo
>
getAssociations
()
{
return
associations
;
}
public
void
setAssociations
(
List
<
NetworkAssociationInfoPojo
>
associations
)
{
this
.
associations
=
associations
;
}
}
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