5d1af17de80726797bed0ffb163598a0feb0e717
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 import java.util.Date;
21 import java.util.Calendar;
22 import java.util.TimeZone;
23 import java.text.SimpleDateFormat;
24
25 logger.info("Task Execution: '"+subject.id+"'. Input Fields: '"+inFields+"'");
26
27 outFields.put("amount"      , inFields.get("amount"));
28 outFields.put("assistant_ID", inFields.get("assistant_ID"));
29 outFields.put("notes"       , inFields.get("notes"));
30 outFields.put("quantity"    , inFields.get("quantity"));
31 outFields.put("branch_ID"   , inFields.get("branch_ID"));
32 outFields.put("item_ID"     , inFields.get("item_ID"));
33 outFields.put("time"        , inFields.get("time"));
34 outFields.put("sale_ID"     , inFields.get("sale_ID"));
35
36 item_id = inFields.get("item_ID");
37
38 //The events used later to test this task use GMT timezone!
39 gmt = TimeZone.getTimeZone("GMT");
40 timenow = Calendar.getInstance(gmt);
41 df = new SimpleDateFormat("HH:mm:ss z");
42 df.setTimeZone(gmt);
43 timenow.setTimeInMillis(inFields.get("time"));
44
45 midnight = timenow.clone();
46 midnight.set(
47     timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
48     timenow.get(Calendar.DATE),0,0,0);
49 eleven30 = timenow.clone();
50 eleven30.set(
51     timenow.get(Calendar.YEAR),timenow.get(Calendar.MONTH),
52     timenow.get(Calendar.DATE),11,30,0);
53
54 itemisalcohol = false;
55 if(item_id != null && item_id >=1000 && item_id < 2000)
56     itemisalcohol = true;
57
58 if( itemisalcohol
59     && timenow.after(midnight) && timenow.before(eleven30)){
60   outFields.put("authorised", false);
61   outFields.put("message", "Sale not authorised by policy task "+subject.taskName+
62     " for time "+df.format(timenow.getTime())+
63     ". Alcohol can not be sold between "+df.format(midnight.getTime())+
64     " and "+df.format(eleven30.getTime()));
65   return true;
66 }
67 else{
68   outFields.put("authorised", true);
69   outFields.put("message", "Sale authorised by policy task "+subject.taskName+
70     " for time "+df.format(timenow.getTime()));
71   return true;
72 }
73
74 /*
75 This task checks if a sale request is for an item that is an alcoholic drink.
76 If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
77 authorised. Otherwise the sale is authorised. 
78 In this implementation we assume that items with item_ID value between 1000 and 
79 2000 are all alcoholic drinks :-)
80 */