3e616ef3ac3cd56f80e7b381093f6e32841f4138
[policy/apex-pdp.git] / client / client-editor / src / main / java / org / onap / policy / apex / client / editor / rest / handling / RestSession.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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 package org.onap.policy.apex.client.editor.rest.handling;
22
23 import org.onap.policy.apex.model.modelapi.ApexApiResult;
24 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
25 import org.onap.policy.apex.model.modelapi.ApexModel;
26 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
27
28 /**
29  * This class represents an ongoing editor session in the Apex editor and holds the information for the session.
30  *
31  */
32 public class RestSession {
33     // The ID of the session
34     private int sessionId;
35     
36     // The Apex policy model of the session
37     private ApexModel apexModel;
38
39     // The Apex policy model being edited
40     private ApexModel apexModelEdited;
41
42     public RestSession(final int sessionId) {
43         this.sessionId = sessionId;
44         this.apexModel = new ApexModelFactory().createApexModel(null, true);
45     }
46
47     /**
48      * Commence making changes to the Apex model.
49      * @return the result of the edit commencement operation
50      */
51     public synchronized ApexApiResult editModel() {
52         if (apexModelEdited != null) {
53             return new ApexApiResult(Result.FAILED, "model is already being edited");
54         }
55         
56         apexModelEdited = apexModel.clone();
57         return new ApexApiResult();
58     }
59     
60     /**
61      * Commit the changes to the Apex model.
62      * @return the result of the commit operation
63      */
64     public synchronized ApexApiResult commitChanges() {
65         if (apexModelEdited == null) {
66             return new ApexApiResult(Result.FAILED, "model is not being edited");
67         }
68         
69         apexModel = apexModelEdited;
70         apexModelEdited = null;
71         return new ApexApiResult();
72     }
73     
74     /**
75      * Discard the changes to the Apex model.
76      * @return the result of the discard operation
77      */
78     public synchronized ApexApiResult discardChanges() {
79         if (apexModelEdited == null) {
80             return new ApexApiResult(Result.FAILED, "model is not being edited");
81         }
82         
83         apexModelEdited = null;
84         return new ApexApiResult();
85     }
86     
87
88     /**
89      * Finish a session by committing or discarding the changes.
90      * 
91      * @param commitFlag if ture, commit changes otherwise discard them
92      */
93     public void finishSession(boolean commitFlag) {
94         if (commitFlag) {
95             commitChanges();
96         }
97         else {
98             discardChanges();
99         }
100     }
101
102     /**
103      * Get the session ID of the session.
104      * @return the sessionId
105      */
106     public int getSessionId() {
107         return sessionId;
108     }
109
110     /**
111      * Get the Apex model of the session.
112      * @return the apexModel
113      */
114     public ApexModel getApexModel() {
115         return apexModel;
116     }
117
118     /**
119      * Get the edited Apex model of the session.
120      * @return the apexModel
121      */
122     public ApexModel getApexModelEdited() {
123         return apexModelEdited;
124     }
125 }