Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Codecrew
/
Moya
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
30
Merge Requests
2
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 0256214c
authored
Jul 29, 2016
by
Tuomas Riihimäki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add rest api for viewing and searching vip-list entries
1 parent
beea1b5e
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
271 additions
and
6 deletions
code/moya-beans/ejbModule/fi/codecrew/moya/beans/VipBean.java
code/moya-beans/ejbModule/fi/codecrew/moya/facade/VipFacade.java
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/VipRestView.java
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/pojo/VipProductDeliveryPojo.java
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/pojo/VipProductPojo.java
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/pojo/VipRestPojo.java
code/moya-beans/ejbModule/fi/codecrew/moya/beans/VipBean.java
View file @
0256214
...
@@ -7,6 +7,7 @@ import java.util.List;
...
@@ -7,6 +7,7 @@ import java.util.List;
import
javax.annotation.security.DeclareRoles
;
import
javax.annotation.security.DeclareRoles
;
import
javax.annotation.security.RolesAllowed
;
import
javax.annotation.security.RolesAllowed
;
import
javax.ejb.EJB
;
import
javax.ejb.EJB
;
import
javax.ejb.EJBAccessException
;
import
javax.ejb.LocalBean
;
import
javax.ejb.LocalBean
;
import
javax.ejb.Stateless
;
import
javax.ejb.Stateless
;
...
@@ -17,6 +18,7 @@ import fi.codecrew.moya.enums.apps.VipPermission;
...
@@ -17,6 +18,7 @@ import fi.codecrew.moya.enums.apps.VipPermission;
import
fi.codecrew.moya.facade.VipFacade
;
import
fi.codecrew.moya.facade.VipFacade
;
import
fi.codecrew.moya.facade.VipProductDeliveryFacade
;
import
fi.codecrew.moya.facade.VipProductDeliveryFacade
;
import
fi.codecrew.moya.facade.VipProductFacade
;
import
fi.codecrew.moya.facade.VipProductFacade
;
import
fi.codecrew.moya.model.LanEvent
;
import
fi.codecrew.moya.model.Vip
;
import
fi.codecrew.moya.model.Vip
;
import
fi.codecrew.moya.model.VipProduct
;
import
fi.codecrew.moya.model.VipProduct
;
import
fi.codecrew.moya.model.VipProductDelivery
;
import
fi.codecrew.moya.model.VipProductDelivery
;
...
@@ -72,6 +74,8 @@ public class VipBean implements VipBeanLocal {
...
@@ -72,6 +74,8 @@ public class VipBean implements VipBeanLocal {
if
(
vip
.
getHost
()
==
null
)
{
if
(
vip
.
getHost
()
==
null
)
{
vip
.
setHost
(
vip
.
getCreator
());
vip
.
setHost
(
vip
.
getCreator
());
}
}
checkEvent
(
vip
);
vipFacade
.
create
(
vip
);
vipFacade
.
create
(
vip
);
}
}
...
@@ -79,14 +83,25 @@ public class VipBean implements VipBeanLocal {
...
@@ -79,14 +83,25 @@ public class VipBean implements VipBeanLocal {
@Override
@Override
@RolesAllowed
({
VipPermission
.
S_EDIT
})
@RolesAllowed
({
VipPermission
.
S_EDIT
})
public
void
delete
(
Vip
vip
)
{
public
void
delete
(
Vip
vip
)
{
vip
=
vipFacade
.
merge
(
vip
);
vip
=
vipFacade
.
reload
(
vip
);
checkEvent
(
vip
);
vipFacade
.
remove
(
vip
);
vipFacade
.
remove
(
vip
);
}
}
private
void
checkEvent
(
Vip
vip
)
{
if
(!
eventbean
.
getCurrentEvent
().
equals
(
vip
.
getEvent
())){
throw
new
EJBAccessException
(
"VIP entry from wrong event!"
);
}
}
@Override
@Override
@RolesAllowed
({
VipPermission
.
S_VIEW
})
@RolesAllowed
({
VipPermission
.
S_VIEW
})
public
Vip
find
(
Integer
id
)
{
public
Vip
find
(
Integer
id
)
{
return
vipFacade
.
find
(
id
);
Vip
ret
=
vipFacade
.
find
(
id
);
checkEvent
(
ret
);
return
ret
;
}
}
// @Override
// @Override
...
@@ -99,6 +114,7 @@ public class VipBean implements VipBeanLocal {
...
@@ -99,6 +114,7 @@ public class VipBean implements VipBeanLocal {
@RolesAllowed
({
VipPermission
.
S_EDIT
})
@RolesAllowed
({
VipPermission
.
S_EDIT
})
public
Vip
createProduct
(
VipProduct
l
)
{
public
Vip
createProduct
(
VipProduct
l
)
{
Vip
vip
=
vipFacade
.
reload
(
l
.
getVip
());
Vip
vip
=
vipFacade
.
reload
(
l
.
getVip
());
checkEvent
(
vip
);
l
.
setVip
(
vip
);
l
.
setVip
(
vip
);
if
(!
vip
.
getProducts
().
contains
(
l
))
if
(!
vip
.
getProducts
().
contains
(
l
))
vip
.
getProducts
().
add
(
0
,
l
);
vip
.
getProducts
().
add
(
0
,
l
);
...
@@ -111,20 +127,23 @@ public class VipBean implements VipBeanLocal {
...
@@ -111,20 +127,23 @@ public class VipBean implements VipBeanLocal {
@Override
@Override
@RolesAllowed
({
VipPermission
.
S_VIEW
})
@RolesAllowed
({
VipPermission
.
S_VIEW
})
public
SearchResult
<
Vip
>
search
(
SearchQuery
sq
)
{
public
SearchResult
<
Vip
>
search
(
SearchQuery
sq
)
{
// TODO Auto-generated method stub
return
vipFacade
.
search
(
sq
);
return
null
;
}
}
@Override
@Override
@RolesAllowed
({
VipPermission
.
S_EDIT
})
@RolesAllowed
({
VipPermission
.
S_EDIT
})
public
Vip
save
(
Vip
vip
)
{
public
Vip
save
(
Vip
vip
)
{
return
vipFacade
.
merge
(
vip
);
Vip
ret
=
vipFacade
.
merge
(
vip
);
checkEvent
(
ret
);
return
ret
;
}
}
@Override
@Override
@RolesAllowed
({
VipPermission
.
S_EDIT
})
@RolesAllowed
({
VipPermission
.
S_EDIT
})
public
Vip
saveProduct
(
VipProduct
prod
)
{
public
Vip
saveProduct
(
VipProduct
prod
)
{
VipProduct
ret
=
vipProductFacade
.
merge
(
prod
);
VipProduct
ret
=
vipProductFacade
.
merge
(
prod
);
checkEvent
(
ret
.
getVip
());
return
ret
.
getVip
();
return
ret
.
getVip
();
}
}
...
@@ -133,6 +152,7 @@ public class VipBean implements VipBeanLocal {
...
@@ -133,6 +152,7 @@ public class VipBean implements VipBeanLocal {
public
Vip
deleteProduct
(
VipProduct
l
)
{
public
Vip
deleteProduct
(
VipProduct
l
)
{
l
=
vipProductFacade
.
reload
(
l
);
l
=
vipProductFacade
.
reload
(
l
);
Vip
vip
=
l
.
getVip
();
Vip
vip
=
l
.
getVip
();
checkEvent
(
vip
);
vip
.
getProducts
().
remove
(
l
);
vip
.
getProducts
().
remove
(
l
);
vipProductFacade
.
remove
(
l
);
vipProductFacade
.
remove
(
l
);
return
vip
;
return
vip
;
...
@@ -142,7 +162,7 @@ public class VipBean implements VipBeanLocal {
...
@@ -142,7 +162,7 @@ public class VipBean implements VipBeanLocal {
@RolesAllowed
({
VipPermission
.
S_USAGE
})
@RolesAllowed
({
VipPermission
.
S_USAGE
})
public
VipProductDelivery
createDelivery
(
VipProduct
prod
,
BigDecimal
quantity
,
String
notes
)
{
public
VipProductDelivery
createDelivery
(
VipProduct
prod
,
BigDecimal
quantity
,
String
notes
)
{
prod
=
vipProductFacade
.
reload
(
prod
);
prod
=
vipProductFacade
.
reload
(
prod
);
checkEvent
(
prod
.
getVip
());
VipProductDelivery
vpd
=
new
VipProductDelivery
();
VipProductDelivery
vpd
=
new
VipProductDelivery
();
vpd
.
setVipProduct
(
prod
);
vpd
.
setVipProduct
(
prod
);
...
...
code/moya-beans/ejbModule/fi/codecrew/moya/facade/VipFacade.java
View file @
0256214
package
fi
.
codecrew
.
moya
.
facade
;
package
fi
.
codecrew
.
moya
.
facade
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
javax.ejb.EJB
;
import
javax.ejb.EJB
;
...
@@ -7,17 +9,45 @@ import javax.ejb.LocalBean;
...
@@ -7,17 +9,45 @@ import javax.ejb.LocalBean;
import
javax.ejb.Stateless
;
import
javax.ejb.Stateless
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.Path
;
import
javax.persistence.criteria.Predicate
;
import
javax.persistence.criteria.Root
;
import
javax.persistence.criteria.Root
;
import
javax.persistence.metamodel.SingularAttribute
;
import
fi.codecrew.moya.beans.EventBeanLocal
;
import
fi.codecrew.moya.beans.EventBeanLocal
;
import
fi.codecrew.moya.facade.callbacks.EventLimiter
;
import
fi.codecrew.moya.facade.callbacks.OrderCallback
;
import
fi.codecrew.moya.facade.callbacks.StringSearchPredicateCreator
;
import
fi.codecrew.moya.model.EventUser
;
import
fi.codecrew.moya.model.EventUser_
;
import
fi.codecrew.moya.model.LanEvent
;
import
fi.codecrew.moya.model.LanEvent
;
import
fi.codecrew.moya.model.User
;
import
fi.codecrew.moya.model.User_
;
import
fi.codecrew.moya.model.Vip
;
import
fi.codecrew.moya.model.Vip
;
import
fi.codecrew.moya.model.Vip_
;
import
fi.codecrew.moya.model.Vip_
;
import
fi.codecrew.moya.utilities.SearchQuery
;
import
fi.codecrew.moya.utilities.SearchResult
;
import
fi.codecrew.moya.utilities.SearchQuery.QuerySortOrder
;
import
fi.codecrew.moya.utilities.jpa.FacadeCallback
;
@Stateless
@Stateless
@LocalBean
@LocalBean
public
class
VipFacade
extends
IntegerPkGenericFacade
<
Vip
>
{
public
class
VipFacade
extends
IntegerPkGenericFacade
<
Vip
>
{
private
static
List
<
SingularAttribute
<
Vip
,
String
>>
SEARCHATTRS
;
public
static
List
<
SingularAttribute
<
Vip
,
String
>>
getAttrlist
()
{
if
(
SEARCHATTRS
==
null
)
{
ArrayList
<
SingularAttribute
<
Vip
,
String
>>
buildAttrs
=
new
ArrayList
<>();
buildAttrs
.
add
(
Vip_
.
description
);
buildAttrs
.
add
(
Vip_
.
shortdescr
);
SEARCHATTRS
=
Collections
.
unmodifiableList
(
buildAttrs
);
}
return
SEARCHATTRS
;
}
public
VipFacade
()
{
public
VipFacade
()
{
super
(
Vip
.
class
);
super
(
Vip
.
class
);
}
}
...
@@ -35,4 +65,41 @@ public class VipFacade extends IntegerPkGenericFacade<Vip> {
...
@@ -35,4 +65,41 @@ public class VipFacade extends IntegerPkGenericFacade<Vip> {
return
getEm
().
createQuery
(
cq
).
getResultList
();
return
getEm
().
createQuery
(
cq
).
getResultList
();
}
}
public
SearchResult
<
Vip
>
search
(
SearchQuery
search
)
{
List
<
FacadeCallback
<
Vip
>>
callbacks
=
new
ArrayList
<>();
// Ascending if null or not descending..
// boolean asc = search.getSortDirection() == null ||
// !QuerySortOrder.DESCENDING.equals(search.getSortDirection());
// SingularAttribute<? super User, ?> sortfield =
// getUserField(search.getSort());
// callbacks.add(new OrderCallback<Vip>(asc, sortfield));
if
(
search
.
getSearch
()
!=
null
&&
!
search
.
getSearch
().
isEmpty
())
callbacks
.
add
(
new
StringSearchPredicateCreator
<
Vip
>(
search
.
getSearch
(),
getAttrlist
()));
LanEvent
event
=
eventBean
.
getCurrentEvent
();
callbacks
.
add
(
new
VipEventLimiter
(
event
));
return
super
.
searcher
(
search
,
callbacks
);
// return this.search(page, pagesize, query, NAMEFIELDS, sort);
}
private
static
class
VipEventLimiter
implements
FacadeCallback
<
Vip
>
{
private
LanEvent
ev
;
public
VipEventLimiter
(
LanEvent
event
)
{
this
.
ev
=
event
;
}
@Override
public
void
exec
(
CriteriaBuilder
cb
,
CriteriaQuery
<?>
cq
,
Path
<
Vip
>
root
,
List
<
Predicate
>
predicates
,
boolean
isFullQuery
)
{
predicates
.
add
(
cb
.
equal
(
root
.
get
(
Vip_
.
event
),
ev
));
}
}
}
}
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/VipRestView.java
0 → 100644
View file @
0256214
package
fi
.
codecrew
.
moya
.
rest
.
v2
;
import
java.util.ArrayList
;
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.QueryParam
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.Response
;
import
com.wordnik.swagger.annotations.Api
;
import
fi.codecrew.moya.beans.ProductBeanLocal
;
import
fi.codecrew.moya.beans.VipBeanLocal
;
import
fi.codecrew.moya.model.Vip
;
import
fi.codecrew.moya.rest.v2.pojo.ProductPojo
;
import
fi.codecrew.moya.rest.v2.pojo.VipRestPojo
;
import
fi.codecrew.moya.utilities.SearchQuery
;
import
fi.codecrew.moya.utilities.SearchResult
;
@RequestScoped
@Path
(
"/v2/vip"
)
@Api
(
value
=
"/v2/vip"
,
description
=
"Vip list operations"
)
@Produces
({
MediaType
.
APPLICATION_JSON
+
"; charset=UTF-8"
})
@Consumes
({
MediaType
.
APPLICATION_JSON
})
public
class
VipRestView
{
@EJB
private
VipBeanLocal
vipbean
;
@GET
@Path
(
"/all"
)
public
Response
getAllVips
()
{
ArrayList
<
VipRestPojo
>
ret
=
VipRestPojo
.
create
(
vipbean
.
getAvailableVips
());
return
Response
.
ok
(
ret
).
build
();
}
@GET
@Path
(
"/search/{searchQuery}"
)
public
Response
getAllVips
(
@PathParam
(
value
=
"searchQuery"
)
String
search
)
{
SearchQuery
sq
=
new
SearchQuery
();
sq
.
setSearch
(
search
);
sq
.
setPagesize
(
0
);
SearchResult
<
Vip
>
result
=
vipbean
.
search
(
sq
);
ArrayList
<
VipRestPojo
>
ret
=
VipRestPojo
.
create
(
result
.
getResults
());
return
Response
.
ok
(
ret
).
build
();
}
}
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/pojo/VipProductDeliveryPojo.java
0 → 100644
View file @
0256214
package
fi
.
codecrew
.
moya
.
rest
.
v2
.
pojo
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
fi.codecrew.moya.model.VipProductDelivery
;
public
class
VipProductDeliveryPojo
{
public
Integer
id
;
public
Integer
delivererId
;
public
BigDecimal
quantity
;
public
Date
deliveryTime
;
public
String
notes
;
public
static
List
<
VipProductDeliveryPojo
>
create
(
List
<
VipProductDelivery
>
deliveries
)
{
List
<
VipProductDeliveryPojo
>
ret
=
new
ArrayList
<>();
if
(
deliveries
!=
null
)
{
for
(
VipProductDelivery
d
:
deliveries
)
{
VipProductDeliveryPojo
delivery
=
new
VipProductDeliveryPojo
();
ret
.
add
(
delivery
);
if
(
d
.
getDeliverer
()
!=
null
)
{
delivery
.
delivererId
=
d
.
getDeliverer
().
getId
();
}
delivery
.
id
=
d
.
getId
();
delivery
.
deliveryTime
=
d
.
getDeliveryTime
();
delivery
.
quantity
=
d
.
getQuantity
();
delivery
.
notes
=
d
.
getNotes
();
}
}
return
ret
;
}
}
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/pojo/VipProductPojo.java
0 → 100644
View file @
0256214
package
fi
.
codecrew
.
moya
.
rest
.
v2
.
pojo
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.List
;
import
fi.codecrew.moya.model.VipProduct
;
public
class
VipProductPojo
{
public
Integer
id
;
public
String
name
;
public
Integer
productId
;
public
String
notes
;
public
BigDecimal
quantity
;
public
BigDecimal
delivered
;
public
List
<
VipProductDeliveryPojo
>
deliveries
=
new
ArrayList
<>();
public
static
VipProductPojo
create
(
VipProduct
prod
)
{
VipProductPojo
ret
=
new
VipProductPojo
();
ret
.
id
=
prod
.
getId
();
ret
.
name
=
prod
.
getProductName
();
if
(
prod
.
getProduct
()
!=
null
)
{
ret
.
productId
=
prod
.
getProduct
().
getId
();
}
ret
.
notes
=
prod
.
getNotes
();
ret
.
quantity
=
prod
.
getQuantity
();
ret
.
delivered
=
prod
.
getDelivered
();
ret
.
deliveries
=
VipProductDeliveryPojo
.
create
(
prod
.
getDeliveries
());
return
ret
;
}
}
code/moya-web/src/main/java/fi/codecrew/moya/rest/v2/pojo/VipRestPojo.java
0 → 100644
View file @
0256214
package
fi
.
codecrew
.
moya
.
rest
.
v2
.
pojo
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
javax.xml.bind.annotation.XmlRootElement
;
import
com.wordnik.swagger.annotations.ApiModel
;
import
fi.codecrew.moya.model.Vip
;
import
fi.codecrew.moya.model.VipProduct
;
@XmlRootElement
()
@ApiModel
(
description
=
"Product"
)
public
class
VipRestPojo
{
public
Integer
id
;
public
String
description
;
public
String
shortdescr
;
public
Date
created
;
public
Integer
eventuserId
;
public
Integer
creatorId
;
public
Integer
hostId
;
public
List
<
VipProductPojo
>
products
=
new
ArrayList
<>();
public
static
ArrayList
<
VipRestPojo
>
create
(
List
<
Vip
>
vips
)
{
ArrayList
<
VipRestPojo
>
ret
=
new
ArrayList
<>();
for
(
Vip
v
:
vips
)
{
VipRestPojo
r
=
new
VipRestPojo
();
ret
.
add
(
r
);
r
.
id
=
v
.
getId
();
r
.
description
=
v
.
getDescription
();
r
.
shortdescr
=
v
.
getShortdescr
();
r
.
created
=
v
.
getCreated
();
if
(
v
.
getEventUser
()
!=
null
)
{
r
.
eventuserId
=
v
.
getEventUser
().
getId
();
}
if
(
v
.
getCreator
()
!=
null
)
{
r
.
creatorId
=
v
.
getCreator
().
getId
();
}
if
(
v
.
getHost
()
!=
null
)
{
r
.
hostId
=
v
.
getHost
().
getId
();
}
for
(
VipProduct
prod
:
v
.
getProducts
())
{
r
.
products
.
add
(
VipProductPojo
.
create
(
prod
));
}
}
return
ret
;
}
}
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