001package com.plivo.api.models.compliancedocument; 002 003import com.plivo.api.exceptions.ResourceNotFoundException; 004import com.plivo.api.models.base.Creator; 005import com.plivo.api.util.Utils; 006import okhttp3.MediaType; 007import okhttp3.MultipartBody; 008import okhttp3.RequestBody; 009import retrofit2.Call; 010 011import javax.activation.MimetypesFileTypeMap; 012import java.io.File; 013import java.io.IOException; 014import java.nio.file.Files; 015import java.nio.file.Path; 016import java.nio.file.Paths; 017 018public class ComplianceDocumentCreator extends Creator<ComplianceDocumentCreateResponse> { 019 020 private String endUserId; 021 private String alias; 022 private String documentTypeId; 023 private String file; 024 private String addressLine1; 025 private String addressLine2; 026 private String city; 027 private String country; 028 private String postalCode; 029 private String lastName; 030 private String firstName; 031 private String dateOfBirth; 032 private String uniqueIdentificationNumber; 033 private String businessName; 034 private String authorizedRepresentativeName; 035 private String nationality; 036 private enum typeOfUtility { 037 water,electricity,gas, propertyRental, others 038 }; 039 private String billId; 040 private String typeOfId; 041 private String placeOfBirth; 042 private String dateOfIssue; 043 private String dateOfExpiration; 044 private String billDate; 045 private String supportEmail; 046 private String supportPhoneNumber; 047 private String useCaseDescription; 048 private MultipartBody.Builder body; 049 050 ComplianceDocumentCreator(String endUserId, String documentTypeId, String alias) { 051 if (!Utils.allNotNull(alias, endUserId)) { 052 throw new IllegalArgumentException("alias, endUserId must not be null"); 053 } 054 055 this.alias = alias; 056 this.endUserId = endUserId; 057 this.documentTypeId = documentTypeId; 058 this.body = new MultipartBody.Builder().setType(MultipartBody.FORM); 059 this.body.addFormDataPart("alias", alias); 060 this.body.addFormDataPart("end_user_id", endUserId); 061 this.body.addFormDataPart("document_type_id", documentTypeId); 062 } 063 064 public void setUniqueIdentificationNumber(String uniqueIdentificationNumber) { 065 this.uniqueIdentificationNumber = uniqueIdentificationNumber; 066 this.body.addFormDataPart("unique_identification_number", uniqueIdentificationNumber); 067 } 068 069 public void setTypeOfId(String typeOfId) { 070 this.typeOfId = typeOfId; 071 this.body.addFormDataPart("type_of_id", typeOfId); 072 } 073 074 public void setBusinessName(String businessName) { 075 this.businessName = businessName; 076 this.body.addFormDataPart("business_name", businessName); 077 } 078 079 public void setAuthorizedRepresentativeName(String authorizedRepresentativeName) { 080 this.authorizedRepresentativeName = authorizedRepresentativeName; 081 this.body.addFormDataPart("authorized_representative_name", authorizedRepresentativeName); 082 } 083 084 public void setNationality(String nationality) { 085 this.nationality = nationality; 086 this.body.addFormDataPart("nationality", nationality); 087 } 088 089 public void setBillId(String billId) { 090 this.billId = billId; 091 this.body.addFormDataPart("bill_id", billId); 092 } 093 094 public void setPlaceOfBirth(String placeOfBirth) { 095 this.placeOfBirth = placeOfBirth; 096 this.body.addFormDataPart("place_of_birth", placeOfBirth); 097 } 098 099 public void setDateOfIssue(String dateOfIssue) { 100 this.dateOfIssue = dateOfIssue; 101 this.body.addFormDataPart("date_of_issue", dateOfIssue); 102 } 103 104 public void setDateOfExpiration(String dateOfExpiration) { 105 this.dateOfExpiration = dateOfExpiration; 106 this.body.addFormDataPart("date_of_expiration", dateOfExpiration); 107 } 108 109 public void setBillDate(String billDate) { 110 this.billDate = billDate; 111 this.body.addFormDataPart("bill_date", billDate); 112 } 113 114 public void setSupportEmail(String supportEmail) { 115 this.supportEmail = supportEmail; 116 this.body.addFormDataPart("support_email", supportEmail); 117 } 118 119 public void setSupportPhoneNumber(String supportPhoneNumber) { 120 this.supportPhoneNumber = supportPhoneNumber; 121 this.body.addFormDataPart("support_phone_number", supportPhoneNumber); 122 } 123 124 public void setUseCaseDescription(String useCaseDescription) { 125 this.useCaseDescription = useCaseDescription; 126 this.body.addFormDataPart("use_case_description", useCaseDescription); 127 } 128 129 public void setAddressLine1(String addressLine1) { 130 this.addressLine1 = addressLine1; 131 this.body.addFormDataPart("address_line_1", addressLine1); 132 } 133 134 public void setAddressLine2(String addressLine2) { 135 this.addressLine2 = addressLine2; 136 this.body.addFormDataPart("address_line_2", addressLine2); 137 } 138 139 public void setAlias(String alias) { 140 this.alias = alias; 141 this.body.addFormDataPart("alias", alias); 142 } 143 144 public void setCity(String city) { 145 this.city = city; 146 this.body.addFormDataPart("city", city); 147 } 148 149 public void setCountry(String country) { 150 this.country = country; 151 this.body.addFormDataPart("country", country); 152 } 153 154 public void setDateOfBirth(String dateOfBirth) { 155 this.dateOfBirth = dateOfBirth; 156 this.body.addFormDataPart("date_of_birth", dateOfBirth); 157 } 158 159 public void setDocumentTypeId(String documentTypeId) { 160 this.documentTypeId = documentTypeId; 161 this.body.addFormDataPart("document_type_id", documentTypeId); 162 } 163 164 public void setEndUserId(String endUserId) { 165 this.endUserId = endUserId; 166 this.body.addFormDataPart("end_user_id", endUserId); 167 } 168 169 public void setFile(String file) throws ResourceNotFoundException { 170 this.file = file; 171 File tempFile = new File(file); 172 boolean exists = tempFile.exists(); 173 if (!exists) 174 throw new ResourceNotFoundException("File missing " + file); 175 try { 176 System.out.println(tempFile); 177 System.out.println(tempFile.toPath()); 178 // handle for java 8 179 String content_type = ""; 180 if (Files.probeContentType(tempFile.toPath()) != null) { 181 content_type = Files.probeContentType(tempFile.toPath()); 182 } else { 183 Path source = Paths.get(file); 184 MimetypesFileTypeMap m = new MimetypesFileTypeMap(source.toString()); 185 content_type = m.getContentType(tempFile); 186 } 187 this.body 188 .addFormDataPart("file", file, 189 RequestBody.create(MediaType.parse(content_type), tempFile)); 190 } catch (IOException e) { 191 throw new ResourceNotFoundException("Unable to read file " + file); 192 } 193 } 194 195 196 public void setFirstName(String firstName) { 197 this.firstName = firstName; 198 this.body.addFormDataPart("first_name", firstName); 199 } 200 201 public void setLastName(String lastName) { 202 this.lastName = lastName; 203 this.body.addFormDataPart("last_name", lastName); 204 } 205 206 public void setPostalCode(String postalCode) { 207 this.postalCode = postalCode; 208 this.body.addFormDataPart("postal_code", postalCode); 209 } 210 211 212 @Override 213 protected Call<ComplianceDocumentCreateResponse> obtainCall() { 214 return client().getApiService().complianceDocumentCreate( client().getAuthId(), this.body.build()); 215 } 216}