Java tutorial
package com.its.web.services; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.math.RandomUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.its.web.db.mappings.License; import com.its.web.db.mappings.Product; import com.its.web.orm.Database; @Service public class LicensesService { private Logger log = Logger.getLogger(this.getClass()); @Autowired Database database; @Autowired EncryptionService encryptionService; @Autowired ProductsService productsService; public boolean deleteAllByHddSerialMacProduct(String hddSerial, String mac, Long productId) { Map<String, Object> fieldValues = new HashMap<String, Object>(); fieldValues.put("product_id", productId); fieldValues.put("hdd_serial", hddSerial); fieldValues.put("mac", mac); try { List<License> licenses = database.getLicenseDao().queryForFieldValues(fieldValues); for (License license : licenses) { remove(license.getId()); } } catch (SQLException e) { return false; } return true; } public boolean saveOrUpdate(License license) { try { if (license.getId() == null) { database.getLicenseDao().create(license); } else { database.getLicenseDao().update(license); } } catch (SQLException e) { e.printStackTrace(); return false; } return true; } public License getById(Long id) { License license = null; try { license = database.getLicenseDao().queryForId(id); } catch (SQLException e1) { } return license; } public List<License> findByLicenseTypeId(Long licenseTypeId) { Map<String, Object> fieldValues = new HashMap<String, Object>(); fieldValues.put("license_type_id", licenseTypeId); try { return database.getLicenseDao().queryForFieldValues(fieldValues); } catch (SQLException e) { e.printStackTrace(); return null; } } public License getByActivationCode(String activationCode) { License l = null; try { l = database.getLicenseDao().queryForEq("activation_code", activationCode).get(0); } catch (Exception e1) { } return l; } public List<License> findByCpuMacProductName(String cpu, String mac, Long parentId) { Map<String, Object> fieldValues = new HashMap<String, Object>(); fieldValues.put("cpu", cpu); fieldValues.put("mac", mac); fieldValues.put("parent_id", parentId); try { return database.getLicenseDao().queryForFieldValues(fieldValues); } catch (SQLException e) { e.printStackTrace(); return null; } } public License getLicenseIfExist(String hddSerial, String mac, Long productId) { Map<String, Object> fieldValues = new HashMap<String, Object>(); fieldValues.put("hdd_serial", hddSerial); fieldValues.put("mac", mac); fieldValues.put("product_id", productId); try { List<License> licenses = database.getLicenseDao().queryForFieldValues(fieldValues); return licenses.get(0); } catch (Exception e) { return null; } } public List<License> readAll() { try { return database.getLicenseDao().queryForAll(); } catch (SQLException e) { e.printStackTrace(); return null; } } public boolean remove(Long id) { try { database.getLicenseDao().deleteIds(Arrays.asList(id)); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } //------------------------------------------------------------------------------------------------------------------------------------------ public String generateCode() { String res = null; do { res = activationCodeAuto(); try { List<License> list = database.getLicenseDao().queryForEq("activation_code", res); if (list == null || list.size() == 0) break; } catch (SQLException e) { break; } } while (true); return res; } public String generatePassword() { StringBuffer password = new StringBuffer(20); int next = RandomUtils.nextInt(13) + 8; password.append(RandomStringUtils.randomAlphanumeric(next)); return password.toString(); } public String activationCodeAuto() { String res = ""; for (int i = 0; i < 5; i++) { res += generateFiveCart(); if (i != 4) res += "-"; } return res; } private String generateFiveCart() { String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // Tu supprimes les lettres dont tu ne veux pas String pass = ""; for (int x = 0; x < 5; x++) { int i = (int) Math.floor(Math.random() * 62); pass += chars.charAt(i); } return pass; } //------------------------------------------------------------------------------------------------------------------------------------------ public InputStream generateLicenseFile(License license) { //String crypted=generateLicenseString(license); //crypted=license.getEncryptionKey() +"@#####@"+crypted; String crypted = license.getActivationCode(); InputStream stream = new ByteArrayInputStream(crypted.getBytes(StandardCharsets.UTF_8)); return stream; } public String generateLicenseString(License license) { String licenseStr = ""; ObjectMapper mapper = new ObjectMapper(); try { licenseStr = mapper.writeValueAsString(license); } catch (Exception e) { } String crypted = encryptionService.cryptString(licenseStr, license.getEncryptionKey()); return crypted; } }