f36941f8f128fc9905c82d3c868006f3bafef7e4
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / FormatFactory.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.serialization.queryformats;
21
22 import javax.ws.rs.core.MultivaluedHashMap;
23 import javax.ws.rs.core.MultivaluedMap;
24
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.introspection.Loader;
27 import org.onap.aai.serialization.db.DBSerializer;
28 import org.onap.aai.serialization.queryformats.exceptions.QueryParamInjectionException;
29 import org.onap.aai.serialization.queryformats.utils.QueryParamInjector;
30 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
31 import org.onap.aai.setup.SchemaVersions;
32
33 public class FormatFactory {
34
35         private final Loader loader;
36         private final DBSerializer serializer;
37         private final UrlBuilder urlBuilder;
38         private final QueryParamInjector injector;
39
40         public FormatFactory (Loader loader, DBSerializer serializer, SchemaVersions schemaVersions, String basePath) throws AAIException {
41                 this.loader = loader;
42                 this.serializer = serializer;
43                 this.urlBuilder = new UrlBuilder(loader.getVersion(), serializer, schemaVersions, basePath);
44                 this.injector = QueryParamInjector.getInstance();
45         }
46         
47         public Formatter get(Format format) throws AAIException {
48                 return get(format, new MultivaluedHashMap<String, String>());
49         }
50         
51         public Formatter get(Format format, MultivaluedMap<String, String> params) throws AAIException {
52                 
53                 Formatter formatter = null;
54
55                 switch (format) {
56                         case graphson :
57                                 formatter = new Formatter(inject(new GraphSON(), params));
58                                 break;
59                         case pathed :
60                                 formatter = new Formatter(inject(new PathedURL(loader, urlBuilder), params));
61                                 break;
62             case pathed_resourceversion :
63                 formatter = new Formatter(inject(new PathedURL(loader, urlBuilder).includeUrl(), params));
64                 break;
65                         case id :
66                                 formatter = new Formatter(inject(new IdURL(loader, urlBuilder), params));
67                                 break;
68                         case resource :
69                                 formatter = new Formatter(inject(new Resource.Builder(loader, serializer, urlBuilder), params).build());
70                                 break;
71                         case resource_and_url :
72                                 formatter = new Formatter(inject(new Resource.Builder(loader, serializer, urlBuilder).includeUrl(), params).build());
73                                 break;
74                         case raw :
75                                 formatter = new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder), params).build());
76                                 break;
77                         case simple :
78                                 formatter = new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder).depth(0).modelDriven(), params).build());
79                                 break;
80                         case console :
81                                 formatter = new Formatter(inject(new Console(), params));
82                                 break;
83                         case count :
84                                 formatter = new Formatter(inject(new Count(), params));
85                                 break;
86             case resource_with_sot :
87                 formatter = new Formatter(inject(new ResourceWithSoT.Builder(loader, serializer, urlBuilder), params).build());
88                 break;
89                         default :
90                                 break;
91
92                 }
93                 
94                 return formatter;
95         }
96         
97         private <T> T inject (T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException {
98                 
99                 injector.injectParams(obj, params);
100                 return obj;
101         }
102         
103 }