Update the license for 2017-2018 license
[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
32 public class FormatFactory {
33
34         private final Loader loader;
35         private final DBSerializer serializer;
36         private final UrlBuilder urlBuilder;
37         private final QueryParamInjector injector;
38         public FormatFactory (Loader loader, DBSerializer serializer) throws AAIException {
39                 this.loader = loader;
40                 this.serializer = serializer;
41                 this.urlBuilder = new UrlBuilder(loader.getVersion(), serializer);
42                 this.injector = QueryParamInjector.getInstance();
43         }
44         
45         public Formatter get(Format format) throws AAIException {
46                 return get(format, new MultivaluedHashMap<String, String>());
47         }
48         
49         public Formatter get(Format format, MultivaluedMap<String, String> params) throws AAIException {
50                 
51                 Formatter formatter = null;
52
53                 switch (format) {
54                         case graphson :
55                                 formatter = new Formatter(inject(new GraphSON(), params));
56                                 break;
57                         case pathed :
58                                 formatter = new Formatter(inject(new PathedURL(loader, urlBuilder), params));
59                                 break;
60                         case id :
61                                 formatter = new Formatter(inject(new IdURL(loader, urlBuilder), params));
62                                 break;
63                         case resource :
64                                 formatter = new Formatter(inject(new Resource.Builder(loader, serializer, urlBuilder), params).build());
65                                 break;
66                         case resource_and_url :
67                                 formatter = new Formatter(inject(new Resource.Builder(loader, serializer, urlBuilder).includeUrl(), params).build());
68                                 break;
69                         case raw :
70                                 formatter = new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder), params).build());
71                                 break;
72                         case simple :
73                                 formatter = new Formatter(inject(new RawFormat.Builder(loader, serializer, urlBuilder).depth(0).modelDriven(), params).build());
74                                 break;
75                         case console :
76                                 formatter = new Formatter(inject(new Console(), params));
77                                 break;
78                         case count :
79                                 formatter = new Formatter(inject(new Count(), params));
80                                 break;
81                         default :
82                                 break;
83
84                 }
85                 
86                 return formatter;
87         }
88         
89         private <T> T inject (T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException {
90                 
91                 injector.injectParams(obj, params);
92                 return obj;
93         }
94         
95 }