2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.policymodel.handling;
23 import javax.xml.bind.annotation.adapters.XmlAdapter;
24 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
25 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
26 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
29 * This class makes the albums field optional in marshaled Policy Models.
30 * Empty albums are not marshaled to JSON/XML.
31 * When unmarshaled, if no albums value is present then a new empty albums entry is created.
33 * @author John Keeney (john.keeney@ericsson.com)
35 public class EmptyAlbumsAdapter extends XmlAdapter<AxContextAlbums, AxContextAlbums> {
39 * Decide whether to marshall a context albums entry. Non-empty context albums are always marshalled.
40 * Empty albums are filtered.
42 * @param albums the albums entry
43 * @return the albums entry, or null if empty
44 * @throws Exception if there is a problem with the marshalling
45 * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object)
48 public AxContextAlbums marshal(AxContextAlbums albums) throws Exception {
49 if ((albums == null) || (albums.getAlbumsMap() == null) || (albums.getAlbumsMap().isEmpty())) {
57 * Decide whether to unmarshall a context albums entry - Always.
59 * @param val the albums entry
60 * @return the albums entry
61 * @throws Exception if there is a problem with the unmarshalling
62 * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
65 public AxContextAlbums unmarshal(AxContextAlbums val) throws Exception {
70 * After unmarshalling has completed the model's context albums entry may be null/empty or default.
71 * If so the key for the albums entry should updated to a sensible value and additional keyinfo
72 * information should then be added for that key
74 * @param policyModel the policy model containing the possibly empty context albums entry
75 * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
77 public void doAfterUnmarshal(AxPolicyModel policyModel) {
78 AxArtifactKey nullkey = new AxArtifactKey();
79 AxArtifactKey blanknullalbumskey =
80 new AxArtifactKey(nullkey.getKey().getName() + "_Albums", nullkey.getKey().getVersion());
81 AxArtifactKey thisalbumskey = policyModel.getAlbums().getKey();
82 AxArtifactKey thismodelkey = policyModel.getKey();
83 AxContextAlbums thismodelalbums = policyModel.getAlbums();
85 if (nullkey.equals(thisalbumskey) || blanknullalbumskey.equals(thisalbumskey)) {
86 thismodelalbums.setKey(new AxArtifactKey(thismodelkey.getName() + "_Albums", thismodelkey.getVersion()));
87 policyModel.getKeyInformation().generateKeyInfo(thismodelalbums);