Fix sonar issues in dcaegen2/services/mapper
[dcaegen2/services/mapper.git] / snmpmapper / src / main / java / org / onap / dcaegen2 / services / mapper / snmpmapper / service / MappingFileServiceImpl.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DCAE
4 * ================================================================================
5 * Copyright 2018 TechMahindra
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.dcaegen2.services.mapper.snmpmapper.service;
21
22 import java.io.IOException;
23 import java.sql.SQLException;
24
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.onap.dcaegen2.services.mapper.snmpmapper.DAO.MappingFileDAO;
28 import org.onap.dcaegen2.services.mapper.snmpmapper.entity.MappingFile;
29 import org.onap.dcaegen2.services.mapper.snmpmapper.exception.SnmpMapperException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34 import org.springframework.web.multipart.MultipartFile;
35
36 @Service
37 public class MappingFileServiceImpl implements MappingFileService {
38         private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
39
40         static String enterpriseid;
41     static MappingFile mapping;
42     
43     @Autowired
44     MappingFileDAO mappingFileDAO;
45         
46         @Override
47         public String saveUploadedFileInDatabase(HttpServletRequest request, MultipartFile[] mappingfile) {
48                 
49                 // Reading File Upload Form Input Parameters        
50         enterpriseid = request.getParameter("eid");
51         
52         LOGGER.debug("EnterPrise ID recieved:{}",enterpriseid);
53          
54         
55         if ((mappingfile != null) && (mappingfile.length > 0)) {
56             for (MultipartFile aFile : mappingfile) {
57                 if(aFile.isEmpty()) {
58                     continue;
59                 } else {
60                         LOGGER.debug("MappingFile Name = {} with enterprise id:{}", aFile.getOriginalFilename(),enterpriseid);
61                     if (!aFile.getOriginalFilename().equals("")) {
62                         try {
63                         mapping = new MappingFile();
64                         mapping.setEnterprise_ID(enterpriseid);
65                         
66                                                         mapping.setMapping_File(aFile.getBytes());
67                                                 
68                         mapping.setMimetype(aFile.getContentType());
69                         mapping.setName(aFile.getOriginalFilename());
70                         
71                         
72                        
73                         mappingFileDAO.uploadMappingFile(aFile,enterpriseid);
74                         } catch (SnmpMapperException snmpMapperException) {
75                                 LOGGER.error(snmpMapperException.getMessage());
76                                                 } catch (IOException e) {
77                                                         LOGGER.error("IOException occured:{}",e.getCause());
78                                                         return "failed";
79                                                 } catch (SQLException e) {
80                                                         LOGGER.error("SQLException occured:{}",e.getCause());
81                                                         return "failed";
82                                                 }
83                
84                     }
85                 }
86                 LOGGER.debug("File Is Successfully Uploaded & Saved In The Database\n");
87             }
88         } else {
89                 return "failed";
90         }
91                 return "success";
92         }
93
94         
95         
96 }