2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.gui.editors.apex.rest.handling;
24 import org.onap.policy.apex.model.modelapi.ApexApiResult;
25 import org.onap.policy.apex.model.modelapi.ApexApiResult.Result;
26 import org.onap.policy.apex.model.modelapi.ApexModel;
27 import org.onap.policy.apex.model.modelapi.ApexModelFactory;
30 * This class represents an ongoing editor session in the Apex editor and holds
31 * the information for the session.
34 public class RestSession {
35 // The ID of the session
36 private int sessionId;
38 // The Apex policy model of the session
39 private ApexModel apexModel;
41 // The Apex policy model being edited
42 private ApexModel apexModelEdited;
44 public RestSession(final int sessionId) {
45 this.sessionId = sessionId;
46 this.apexModel = new ApexModelFactory().createApexModel(null, true);
50 * Commence making changes to the Apex model.
52 * @return the result of the edit commencement operation
54 public synchronized ApexApiResult editModel() {
55 if (apexModelEdited != null) {
56 return new ApexApiResult(Result.FAILED, "model is already being edited");
59 apexModelEdited = apexModel.clone();
60 return new ApexApiResult();
64 * Commit the changes to the Apex model.
66 * @return the result of the commit operation
68 public synchronized ApexApiResult commitChanges() {
69 if (apexModelEdited == null) {
70 return new ApexApiResult(Result.FAILED, "model is not being edited");
73 apexModel = apexModelEdited;
74 apexModelEdited = null;
75 return new ApexApiResult();
79 * Discard the changes to the Apex model.
81 * @return the result of the discard operation
83 public synchronized ApexApiResult discardChanges() {
84 if (apexModelEdited == null) {
85 return new ApexApiResult(Result.FAILED, "model is not being edited");
88 apexModelEdited = null;
89 return new ApexApiResult();
93 * Finish a session by committing or discarding the changes.
95 * @param commitFlag if ture, commit changes otherwise discard them
97 public void finishSession(boolean commitFlag) {
106 * Get the session ID of the session.
108 * @return the sessionId
110 public int getSessionId() {
115 * Get the Apex model of the session.
117 * @return the apexModel
119 public ApexModel getApexModel() {
124 * Get the edited Apex model of the session.
126 * @return the apexModel
128 public ApexModel getApexModelEdited() {
129 return apexModelEdited;