/// ////////////////////////////////////////// public override CResultAErreur MyEval(CContexteEvaluationExpression ctx, object[] valeursParametres) { CResultAErreur result = CResultAErreur.True; double fVal = 0; if (valeursParametres[0] is CValeurUnite) { result.Data = ((CValeurUnite)valeursParametres[0]).Valeur; return(result); } try { fVal = Convert.ToDouble(valeursParametres[0]); if (double.IsNaN(fVal)) { fVal = 0; } } catch { try { string strTexte = valeursParametres[0].ToString(); fVal = CUtilDouble.DoubleFromString(strTexte); if (double.IsNaN(fVal)) { fVal = 0; } } catch { } } result.Data = fVal; return(result); }
/// ///////////////////////////////////////////////////////// public static object StringToType(ETypeChampBasique type, string strTexte) { if (strTexte.ToUpper() == c_ConstanteNull) { return(null); } switch (type) { case ETypeChampBasique.Bool: return(strTexte.ToString() == "1" || strTexte.ToUpper() == "TRUE"); case ETypeChampBasique.Date: try { return(Convert.ChangeType(strTexte, typeof(DateTime), null)); } catch { //Tente le format sc2i de date chaine try { return(CUtilDate.FromUniversalString(strTexte)); } catch { return(null); } } case ETypeChampBasique.Decimal: try { return(CUtilDouble.DoubleFromString(strTexte)); } catch { return(null); } case ETypeChampBasique.Int: try { return(Convert.ChangeType(strTexte, typeof(int), null)); } catch { return(null); } case ETypeChampBasique.String: return(strTexte); } return(null); }
//------------------------------------------------ /// <summary> /// Convertit la valeur pour qu'elle soit compatible avec le champ /// </summary> /// <param name="valeur"></param> /// <returns></returns> private CResultAErreur ConvertValue(object valeur) { CResultAErreur result = CResultAErreur.True; if (valeur == null || Propriete == null) { return(result); } if (Source.OptionsValeursNulles != null && Source.OptionsValeursNulles.IsValeurNulle(valeur)) { return(result); } Type tpAttendu = Propriete.TypeDonnee.TypeDotNetNatif; if (tpAttendu.IsGenericType && tpAttendu.GetGenericTypeDefinition() == typeof(Nullable <>)) { tpAttendu = tpAttendu.GetGenericArguments()[0]; } if (tpAttendu == typeof(CDateTimeEx)) { tpAttendu = typeof(DateTime); } try { object val = null; if (tpAttendu == typeof(double) && valeur is string) { val = CUtilDouble.DoubleFromString((string)valeur); } else { val = Convert.ChangeType(valeur, tpAttendu); } result.Data = val; } catch { if (Source.OptionsValeursNulles != null && Source.OptionsValeursNulles.NullOnConversionError) { result.Data = null; } else { result.EmpileErreur(I.T("Can not convert value @1 to @2|20092", valeur.ToString(), DynamicClassAttribute.GetNomConvivial(tpAttendu))); } return(result); } return(result); }
//-------------------------------------------------------------------------------- public static List <CValeurUnite> DecomposeChaineFormattée(string strChaine) { List <CValeurUnite> valeurs = new List <CValeurUnite>(); string strVal = ""; string strU = ""; //Si - au début, suppression et note le premier élément en - bool bIsNegatif = false; strChaine = strChaine.Trim(); if (strChaine.Length > 0 && strChaine[0] == '-') { bIsNegatif = true; strChaine = strChaine.Substring(1); } foreach (char c in strChaine) { if ("0123456789,.-+eE".IndexOf(c) >= 0 || c == ' ') { if (strU != "") { if (strVal.Trim().Length > 0) { valeurs.Add(new CValeurUnite(CUtilDouble.DoubleFromString(strVal), strU.Trim())); } strU = ""; strVal = ""; } if (c != ' ') { strVal += c; } } else if (c != ' ') { strU += c; } } if (strVal.Trim().Length > 0) { valeurs.Add(new CValeurUnite(CUtilDouble.DoubleFromString(strVal), strU.Trim())); } if (bIsNegatif && valeurs.Count > 0) { valeurs[0].Valeur = -valeurs[0].Valeur; } return(valeurs); }
//-------------------------------------------------- public CValeursImputées(string strStringSerialization) { string[] strChaines = strStringSerialization.Split('~'); foreach (string strChaine in strChaines) { string[] strParts = strChaine.Split('#'); if (strParts.Length == 2) { string strIdUniversel = strParts[0]; double fVal = 0; try { fVal = CUtilDouble.DoubleFromString(strParts[1]); } catch { } m_dicImputations[strIdUniversel] = fVal; } } }
/// ////////////////////////////////////////// public override CResultAErreur MyEval(CContexteEvaluationExpression ctx, object[] valeursParametres) { CResultAErreur result = CResultAErreur.True; try { double?fVal = null; if (valeursParametres[0] is double) { fVal = (double)valeursParametres[0]; } if (fVal == null) { if (valeursParametres[0] is string) { try { fVal = CUtilDouble.DoubleFromString((string)valeursParametres[0]); } catch { } } } if (fVal == null) { try { fVal = Convert.ToDouble(valeursParametres[0]); } catch { } } if (fVal == null) { result.EmpileErreur("Bad parameter for U, first parameter should be a real number|20112"); return(result); } result.Data = new CValeurUnite(fVal.Value, valeursParametres[1].ToString()); } catch { result.EmpileErreur(I.T("The parameters of the function 'U' are incorrect|20113")); } return(result); }
//--------------------------------------------------------- public void ConvertToTableToMap() { DataTable table = m_grid.DataSource as DataTable; if (table == null) { return; } m_tableToMap.Rows.Clear(); m_tableToMap.AcceptChanges(); foreach (DataRow row in table.Rows) { DataRow newRow = m_tableToMap.NewRow(); string strErrors = ""; foreach (KeyValuePair <DataColumn, string> kv in m_dicMaps) { if (kv.Value != null && table.Columns.Contains(kv.Value)) { string strVal = row[kv.Value] as string; if (strVal == null) { newRow[kv.Key] = DBNull.Value; } else { Type tp = kv.Key.DataType; if (tp == typeof(string)) { newRow[kv.Key] = strVal; } if (tp == typeof(int)) { if (strVal.Trim().Length == 0) { newRow[kv.Key] = DBNull.Value; } else { try { newRow[kv.Key] = Int64.Parse(strVal); } catch { newRow[kv.Key] = DBNull.Value; strErrors += "Can not convert value '" + strVal + "' to integer"; } } } if (tp == typeof(double)) { if (strVal.Trim().Length == 0) { newRow[kv.Key] = DBNull.Value; } else { try { newRow[kv.Key] = CUtilDouble.DoubleFromString(strVal); } catch { newRow[kv.Key] = DBNull.Value; strErrors += "Can not convert value '" + strVal + "' to décimal number"; } } } if (tp == typeof(DateTime)) { if (strVal.Trim().Length == 0) { newRow[kv.Key] = DBNull.Value; } else { try { newRow[kv.Key] = DateTime.Parse(strVal); } catch { newRow[kv.Key] = DBNull.Value; strErrors += "Can not convert value '" + strVal + "' to date time"; } } } if (tp == typeof(bool)) { if (strVal.Trim().Length == 0) { newRow[kv.Key] = DBNull.Value; } else { if (strVal.ToUpper() == "OK" || strVal.ToUpper() == "TRUE" || strVal.ToUpper() == "1") { newRow[kv.Key] = true; } else { newRow[kv.Key] = false; } } } } newRow.SetColumnError(kv.Key, strErrors); } } m_tableToMap.Rows.Add(newRow); } return; }
/// ///////////////////////////////////////////////////////// public static object StringToType(TypeDonnee type, string strTexte, CContexteDonnee contexteDonnee) { if (strTexte.ToUpper() == c_ConstanteNull) { return(null); } switch (type) { case TypeDonnee.tBool: return(strTexte.ToString() == "1" || strTexte.ToUpper() == "TRUE"); case TypeDonnee.tDate: try { return(Convert.ChangeType(strTexte, typeof(DateTime), null)); } catch { //Tente le format sc2i de date chaine try { return(CUtilDate.FromUniversalString(strTexte)); } catch { return(null); } } case TypeDonnee.tDouble: try { return(CUtilDouble.DoubleFromString(strTexte)); } catch { return(null); } case TypeDonnee.tEntier: try { return(Convert.ChangeType(strTexte, typeof(int), null)); } catch { return(null); } case TypeDonnee.tString: return(strTexte); case TypeDonnee.tObjetDonneeAIdNumeriqueAuto: try { if (contexteDonnee == null) { return(null); } //Syntaxe : classe|id int nPos = strTexte.LastIndexOf("|"); if (nPos < 0) { return(null); } string strClasse = strTexte.Substring(0, nPos); int nId = Int32.Parse(strTexte.Substring(nPos + 1)); Type tp = CActivatorSurChaine.GetType(strClasse, true); if (tp != null) { CObjetDonneeAIdNumerique objet = (CObjetDonneeAIdNumerique)Activator.CreateInstance(tp, new object[] { contexteDonnee }); if (objet.ReadIfExists(nId)) { return(objet); } } return(null); } catch { return(null); } } return(null); }
/// <summary> /// Importe un fichier. Le data du result contient un datatable /// </summary> /// <param name="?"></param> /// <returns></returns> public CResultAErreur LectureFichier(string strFichier) { CResultAErreur result = CResultAErreur.True; StreamReader reader = new StreamReader(strFichier, new CEncoding(m_encoding).GetEncoding()); DataTable table = new DataTable(); try { //string strLigne = reader.ReadLine(); string strLigne = GetCSVLine(reader); string[] strDatas = strLigne.Split(m_strSeparateur); int nCol = 1; int nLigne = 1; //Creation des colonnes Dictionary <DataColumn, CColonneCSV> mappageCols = new Dictionary <DataColumn, CColonneCSV>(); foreach (string strData in strDatas) { //Nom Table string strNom = "Column " + nCol.ToString(); CColonneCSV colCSV = (CColonneCSV)m_tableColonnes[nCol - 1]; if (NomChampsSurPremiereLigne) { strNom = strData; } if (colCSV != null && colCSV.Nom != "") { strNom = colCSV.Nom; } int nIndex = 0; while (table.Columns[strNom] != null) { nIndex++; strNom = strNom + "_" + nIndex.ToString(); } Type tp = typeof(string); if (colCSV != null && colCSV.DataType != null) { tp = colCSV.DataType; } DataColumn col = new DataColumn(strNom, tp); mappageCols.Add(col, colCSV); table.Columns.Add(col); col.AllowDBNull = true; nCol++; } if (NomChampsSurPremiereLigne) { //strLigne = reader.ReadLine(); strLigne = GetCSVLine(reader); nLigne++; } //Lecture Table while (strLigne != null) { if (strLigne.Trim().Length > 0) { strDatas = GetDatas(strLigne); nCol = 0; DataRow row = table.NewRow(); foreach (string strData in strDatas) { if (nCol < table.Columns.Count) { DataColumn col = table.Columns[nCol]; CColonneCSV colCSV = mappageCols[col]; try { if (colCSV != null) { if (colCSV.HasNullMapping) { if (colCSV.NullCaseSensitive) { if (strData == colCSV.NullMapping) { row[col] = DBNull.Value; nCol++; continue; } } else if (strData.ToUpper() == colCSV.NullMapping.ToUpper()) { row[col] = DBNull.Value; nCol++; continue; } } } if (col.DataType == typeof(string)) { row[col] = strData; } else if (col.DataType == typeof(double) || col.DataType == typeof(int) || col.DataType == typeof(float)) { double fVal = CUtilDouble.DoubleFromString(strData); if (col.DataType == typeof(int)) { row[col] = (int)fVal; } if (col.DataType == typeof(float)) { row[col] = (float)fVal; } else { row[col] = fVal; } } else if (col.DataType == typeof(DateTime)) { DateTime dt = CUtilDate.DateFromString(strData); row[col] = dt; } else if (col.DataType == typeof(bool)) { string valBool = strData.ToUpper(); bool val = (valBool == "1" || valBool == "O" || valBool == "OUI" || valBool == "Y" || valBool == "YES" || valBool == "TRUE"); row[col] = val; } else { object val = Convert.ChangeType(strData, col.DataType); row[col] = val; } } catch { if (ValeurNullSiErreur) { row[col] = DBNull.Value; } else { result.EmpileErreur(I.T("Conversion error : line @1 column @2 : the value cannot be converted into @3|30003" , nLigne.ToString(), col.ColumnName, DynamicClassAttribute.GetNomConvivial(col.DataType))); return(result); } } } nCol++; } table.Rows.Add(row); } //strLigne = reader.ReadLine(); strLigne = GetCSVLine(reader); nLigne++; } result.Data = table; return(result); } catch (Exception e) { result.EmpileErreur(new CErreurException(e)); return(result); } finally { try { reader.Close(); } catch { } } }
//------------------------------------------------------------------------------------ public CResultAErreur TraiteDataRowFromCsv(string strCsvDataRow) { CResultAErreur result = CResultAErreur.True; // Traite la data row de données CSV string[] listeDatas = strCsvDataRow.Split(';'); string strHostId = listeDatas[0]; if (strHostId == "") { string strErreur = "No host Id for CSV data row : " + strCsvDataRow; C2iEventLog.WriteErreur(strErreur); result.EmpileErreur(strErreur); return(result); } HostId = strHostId; try{ DateTime dt = ParseDateFromString(listeDatas[1]); if (dt != null) { //Vérifie l'existence de cet élément CListeObjetDonneeGenerique <CCamusatQowisioData> lstToCount = new CListeObjetDonneeGenerique <CCamusatQowisioData>(ContexteDonnee); lstToCount.Filtre = new CFiltreData( c_champQwHost_Id + "=@1 and " + c_champQwDateTime + "=@2", strHostId, dt); if (lstToCount.Count > 0) { if (IsNew()) { CancelCreate(); } PointeSurLigne(lstToCount[0].Id); } } } catch (Exception e) { } int?nIdSite = TrouveAssociationIdHost_IdSite(strHostId, ContexteDonnee); if (nIdSite == null) { string strErreur = "Error in TraiteDataRowFromCsv: No associatied Site found for Host Id : " + strHostId; C2iEventLog.WriteErreur(strErreur); result.EmpileErreur(strErreur); return(result); } int nIdTypSite = -1; lock (typeof(CLockerCacheDatasQowisio)) { if (!s_dicCacheSiteId_TypeSiteId.TryGetValue(nIdSite.Value, out nIdTypSite)) { CSite site = new CSite(ContexteDonnee); if (site.ReadIfExists(nIdSite)) { nIdTypSite = site.TypeSite.Id; s_dicCacheSiteId_TypeSiteId[nIdSite.Value] = nIdTypSite; } } } if (nIdTypSite == c_nIdTypeSiteTelecom) { SiteId = nIdSite; } else if (nIdTypSite == c_nIdTypeSitePickup) { PickupId = nIdSite; } else { string strErreur = "Error in Site Type for site Id = " + nIdSite.Value; C2iEventLog.WriteErreur(strErreur); result.EmpileErreur(strErreur); return(result); } int nNbColonnes = listeDatas.Length; for (int i = 1; i < nNbColonnes; i++) { string strDataSource = listeDatas[i]; KeyValuePair <string, Type> defChampData; if (s_dicMappageColonneCsv_ChampData.TryGetValue(i, out defChampData)) { string strNomChamp = defChampData.Key; Type typeDonnee = defChampData.Value; if (typeDonnee == typeof(DateTime)) { try { DateTime date = ParseDateFromString(strDataSource); Row[strNomChamp] = date; continue; } catch { } } if (typeDonnee == typeof(int?)) { if (strDataSource.Trim() == "") { Row[strNomChamp] = DBNull.Value; } else { try { int nVal = Int32.Parse(strDataSource); Row[strNomChamp] = nVal; } catch { Row[strNomChamp] = DBNull.Value; } } } if (typeDonnee == typeof(double)) { try { double valeur = CUtilDouble.DoubleFromString(strDataSource); Row[strNomChamp] = valeur; continue; } catch { } } if (typeDonnee == typeof(string)) { Row[strNomChamp] = strDataSource; continue; } } else { return(result); } } result += TraiteAssociationsFuelProbeTank(); return(result); }