8ec15a9927263d5abd71fa780538b01845968d0c
[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
21 var returnValueType = Java.type("java.lang.Boolean");
22 var returnValue = new returnValueType(true);
23
24 // Load compatibility script for imports etc 
25 load("nashorn:mozilla_compat.js");
26 importPackage(java.text);
27 importClass(java.text.SimpleDateFormat);
28
29 executor.logger.info("Task Execution: '"+executor.subject.id+"'. Input Fields: '"+executor.inFields+"'");
30
31 executor.outFields.put("amount"      , executor.inFields.get("amount"));
32 executor.outFields.put("assistant_ID", executor.inFields.get("assistant_ID"));
33 executor.outFields.put("notes"       , executor.inFields.get("notes"));
34 executor.outFields.put("quantity"    , executor.inFields.get("quantity"));
35 executor.outFields.put("branch_ID"   , executor.inFields.get("branch_ID"));
36 executor.outFields.put("item_ID"     , executor.inFields.get("item_ID"));
37 executor.outFields.put("time"        , executor.inFields.get("time"));
38 executor.outFields.put("sale_ID"     , executor.inFields.get("sale_ID"));
39
40 item_id = executor.inFields.get("item_ID");
41
42 //All times in this script are in GMT/UTC since the policy and events assume time is in GMT. 
43 var timenow_gmt =  new Date(Number(executor.inFields.get("time")));
44
45 var midnight_gmt = new Date(Number(executor.inFields.get("time")));
46 midnight_gmt.setUTCHours(0,0,0,0);
47
48 var eleven30_gmt = new Date(Number(executor.inFields.get("time")));
49 eleven30_gmt.setUTCHours(11,30,0,0);
50
51 var timeformatter = new java.text.SimpleDateFormat("HH:mm:ss z");
52
53 var itemisalcohol = false;
54 if(item_id != null && item_id >=1000 && item_id < 2000)
55     itemisalcohol = true;
56     
57 if( itemisalcohol
58     && timenow_gmt.getTime() >= midnight_gmt.getTime()
59     && timenow_gmt.getTime() <  eleven30_gmt.getTime()) {
60
61   executor.outFields.put("authorised", false);
62   executor.outFields.put("message", "Sale not authorised by policy task " +
63     executor.subject.taskName+ " for time " + timeformatter.format(timenow_gmt.getTime()) +
64     ". Alcohol can not be sold between " + timeformatter.format(midnight_gmt.getTime()) +
65     " and " + timeformatter.format(eleven30_gmt.getTime()));
66 }
67 else{
68   executor.outFields.put("authorised", true);
69   executor.outFields.put("message", "Sale authorised by policy task " + 
70     executor.subject.taskName + " for time "+timeformatter.format(timenow_gmt.getTime()));
71 }
72
73 /*
74 This task checks if a sale request is for an item that is an alcoholic drink.
75 If the local time is between 00:00:00 GMT and 11:30:00 GMT then the sale is not
76 authorised. Otherwise the sale is authorised. 
77 In this implementation we assume that items with item_ID value between 1000 and 
78 2000 are all alcoholic drinks :-)
79 */