package it.acxent.face; import it.acxent.db.ApplParmFull; import it.acxent.db.DBAdapter; import it.acxent.db.ResParm; import it.acxent.db.WcString; import it.acxent.face.api.FaceRecognitionApi; import it.acxent.face.api.FaceRecognitionApiResult; import it.acxent.util.StringTokenizer; import it.acxent.util.Vectumerator; import java.io.File; import java.io.FileOutputStream; import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Base64; import org.json.JSONObject; public class FaceRemota extends DBAdapter implements Serializable, FaceLabel { private static final long serialVersionUID = 1705956570841L; private long id_faceRemota; private long label; private String labelMd5; private long labelCount; public FaceRemota(ApplParmFull newApplParmFull) { super(newApplParmFull); } public FaceRemota() {} public void setId_faceRemota(long newId_faceRemota) { this.id_faceRemota = newId_faceRemota; } public void setLabelMd5(String newMd5) { this.labelMd5 = newMd5; } public long getId_faceRemota() { return this.id_faceRemota; } public String getLabelMd5() { return (this.labelMd5 == null) ? "" : this.labelMd5.trim(); } protected ResParm checkDeleteCascade() { return new ResParm(true); } protected void deleteCascade() {} public Vectumerator findByCR(FaceRemotaCR CR, int pageNumber, int pageRows) { String s_Sql_Find = "select A.* from FACE_REMOTA AS A"; String s_Sql_Order = ""; WcString wc = new WcString(); if (!CR.getSearchTxt().trim().isEmpty()) { StringTokenizer st = new StringTokenizer(CR.getSearchTxt().trim(), " "); StringBuffer txt = new StringBuffer("("); while (st.hasMoreTokens()) { String token = st.nextToken(); txt.append("(A.Cognome like '%" + token + "%' or A.Nome like '%" + token + "%')"); if (st.hasMoreTokens()) txt.append(" and "); } txt.append(")"); wc.addWc(txt.toString()); } try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt, pageNumber, pageRows); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public long getLabel() { return this.label; } public void setLabel(long label) { this.label = label; } public String getFullFaceImgPath() { return getFullFileName(); } public void findByLabel(long l_label) { String s_Sql_Find = "select A.* from FACE_REMOTA AS A"; String s_Sql_Order = ""; WcString wc = new WcString(); wc.addWc("A.label=" + l_label); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); findFirstRecord(stmt); } catch (SQLException e) { removeCPConnection(); handleDebug(e); } if (getId_faceRemota() > 0L) return; ResParm rp = new ResParm(); FaceRecognitionApi fra = new FaceRecognitionApi(getApFull()); FaceRecognitionApiResult frar = fra.__reqImage(l_label, ""); if (!frar.isOk()) { rp.setStatus(false); rp.setMsg("Errore!. Label non trovata: " + frar.getMsg()); } else { try { JSONObject jResponse = (JSONObject)frar.getResult(); String l_md5 = jResponse.getString("labelMd5"); setLabel(l_label); setLabelMd5(l_md5); rp = superSave(); if (rp.getStatus()) { StringBuilder err = new StringBuilder(); String imageContent = jResponse.getJSONObject("image") .getString("content"); File path = new File(getPathFaceImg()); if (!path.exists()) path.mkdirs(); byte[] decodedBytes = Base64.getDecoder().decode(imageContent); File file = new File(getFullFileName()); if (!file.exists()) { FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(decodedBytes); } catch (Exception e) { e.printStackTrace(); err.append("Impossibile salvare l'immagine md5 " + getLabelMd5() + ". " + e.getMessage()); err.append("\n"); } finally { if (fos != null) fos.close(); } } if (err.length() > 0) handleDebug(err.toString()); } else { handleDebug(rp.getErrMsg()); } } catch (Exception e) { handleDebug(e); } } } public long getNumberOfLabelsByLabel(long l_label) { return 0L; } public void setLabelCount(long l_labelCount) { this.labelCount = l_labelCount; } public long getLabelCount() { return this.labelCount; } public ResParm superSave() { return save(); } public String getPettorale() { return ""; } public void findByMd5(String l_md5) { String s_Sql_Find = "select A.* from FACE_REMOTA AS A"; String s_Sql_Order = ""; WcString wc = new WcString(); wc.addWc("A.labelMd5='" + l_md5 + "'"); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); findFirstRecord(stmt); } catch (SQLException e) { removeCPConnection(); handleDebug(e); } if (getId_faceRemota() > 0L) return; ResParm rp = new ResParm(); FaceRecognitionApi fra = new FaceRecognitionApi(getApFull()); FaceRecognitionApiResult frar = fra.__reqImage(0L, l_md5); if (!frar.isOk()) { rp.setStatus(false); rp.setMsg("Errore!. Label non trovata: " + frar.getMsg()); } else { try { JSONObject jResponse = (JSONObject)frar.getResult(); long l_label = jResponse.getLong("label"); setLabel(l_label); setLabelMd5(l_md5); rp = superSave(); if (rp.getStatus()) { StringBuilder err = new StringBuilder(); String imageContent = jResponse.getJSONObject("image") .getString("content"); File path = new File(getPathFaceImg()); if (!path.exists()) path.mkdirs(); byte[] decodedBytes = Base64.getDecoder().decode(imageContent); File file = new File(getFullFileName()); if (!file.exists()) { FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(decodedBytes); } catch (Exception e) { e.printStackTrace(); err.append("Impossibile salvare l'immagine md5 " + getLabelMd5() + ". " + e.getMessage()); err.append("\n"); } finally { if (fos != null) fos.close(); } } if (err.length() > 0) handleDebug(err.toString()); } else { handleDebug(rp.getErrMsg()); } } catch (Exception e) { handleDebug(e); } } } public String getFullFileName() { return getPathFaceImg() + getPathFaceImg() + ".jpg"; } public String getPathFaceImg() { return getParm("PATHFACES_REMOTO").getTesto() + getParm("PATHFACES_REMOTO").getTesto(); } public String getPathIdStepDir() { if (getPathIdStep() <= 0L) return ""; if (getId_faceRemota() == 0L) System.out.println("getPathIdStepDir pio"); return "_" + getPathIdStep() * (getId_faceRemota() / getPathIdStep() + 1L) + "/"; } }