Example #1
0
        private static void UpdateImpact(string assetType, string activityID, string conditionIndex, string value)
        {
            OMSAssetConditionIndexStore oci = OMS.GetAssetConditionIndex(assetType);
            OMSConditionIndexStore      omsConditionIndex = oci.ConditionIndexes.Find(delegate(OMSConditionIndexStore ci) { return(ci.ConditionCategory == conditionIndex); });

            if (omsConditionIndex != null)
            {
                string attribute = omsConditionIndex.AttributeDE;
                if (string.IsNullOrWhiteSpace(value))//The user has deleted an impact.
                {
                    DeleteScenario.DeleteImpact(activityID, attribute);
                }
                else
                {
                    bool isExists = false;
                    using (SqlConnection connection = new SqlConnection(DB.ConnectionString))
                    {
                        try
                        {
                            connection.Open();
                            string        query = "SELECT CONSEQUENCEID FROM " + DB.TablePrefix + "CONSEQUENCES WHERE TREATMENTID='" + activityID + "' AND ATTRIBUTE_='" + attribute + "'";
                            SqlCommand    cmd   = new SqlCommand(query, connection);
                            SqlDataReader dr    = cmd.ExecuteReader();
                            if (dr.Read())
                            {
                                isExists = true;
                            }
                        }
                        catch (Exception e)
                        {
                            DataAccessExceptionHandler.HandleException(e, false);
                            return;
                        }
                    }

                    if (isExists)
                    {
                        //Update impact
                        UpdateImpact(activityID, attribute, value);
                    }
                    else //Insert impact
                    {
                        InsertImpact(activityID, attribute, value);
                    }
                }
            }
        }
Example #2
0
        public static void AddActivity(OMSActivityStore activity, String simulationID, String asset)
        {
            //Insert activity in treatments
            //Get Identity
            string treatmentID = null;
            int    year        = DateTime.Now.Year + 1;

            using (SqlConnection connection = new SqlConnection(DB.ConnectionString))
            {
                connection.Open();
                string insert = "INSERT INTO " + DB.TablePrefix + "TREATMENTS (SIMULATIONID,TREATMENT,BEFOREANY,BEFORESAME,OMS_REPEAT_START, OMS_REPEAT_INTERVAL, OMS_OID, OMS_IS_SELECTED,OMS_IS_EXCLUSIVE, OMS_IS_REPEAT)"
                                + " VALUES ('" + simulationID + "','" + activity.ActivityName + "','1','1','" + year + "','1','" + activity.ActivityOID + "','0','0','0')";
                try
                {
                    SqlCommand cmd = new SqlCommand(insert, connection);
                    cmd.ExecuteNonQuery();
                    string selectIdentity = "SELECT IDENT_CURRENT ('" + DB.TablePrefix + "TREATMENTS') FROM " + DB.TablePrefix + "TREATMENTS";
                    cmd         = new SqlCommand(selectIdentity, connection);
                    treatmentID = cmd.ExecuteScalar().ToString();
                }
                catch (Exception e)
                {
                    Utility.ExceptionHandling.DataAccessExceptionHandler.HandleException(e, false);
                }

                try
                {
                    double cost = 0;
                    if (activity.OmsCost.CostPerUnit != double.NaN)
                    {
                        cost = activity.OmsCost.CostPerUnit;
                        if (cost <= 0)
                        {
                            cost = 1;
                        }
                    }
                    else
                    {
                        cost = 1;
                    }
                    insert = "INSERT INTO " + DB.TablePrefix + "COSTS (TREATMENTID,COST_) VALUES ('" + treatmentID + "','" + cost.ToString() + "')";
                    SqlCommand cmd = new SqlCommand(insert, connection);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Utility.ExceptionHandling.DataAccessExceptionHandler.HandleException(e, false);
                }

                OMSAssetConditionIndexStore oci = OMS.GetAssetConditionIndex(asset);

                foreach (OMSConsequenceStore consequence in activity.ConsequenceList)
                {
                    try
                    {
                        OMSConditionIndexStore condition = oci.ConditionIndexes.Find(delegate(OMSConditionIndexStore ci) { return(ci.ConditionCategory == consequence.ConditionCategory); });

                        if (condition != null)
                        {
                            insert = "INSERT INTO " + DB.TablePrefix + "CONSEQUENCES (TREATMENTID,ATTRIBUTE_,CHANGE_) VALUES ('" + treatmentID + "','__" + condition.AttributeDE.Replace(" ", "").Replace("/", "") + "','" + consequence.GetDecisionEngineConsequence() + "')";
                            SqlCommand cmd = new SqlCommand(insert, connection);
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception e)
                    {
                        Utility.ExceptionHandling.DataAccessExceptionHandler.HandleException(e, false);
                    }
                }

                try
                {
                    string conditionIndexCriteria = GetConditionIndexCriteria(activity.ConsequenceList);

                    if (String.IsNullOrWhiteSpace(conditionIndexCriteria))
                    {
                        conditionIndexCriteria = "[AssetType]=|" + asset + "|";
                    }
                    else
                    {
                        conditionIndexCriteria = " AND [AssetType]=|" + asset + "|";
                    }


                    insert = "INSERT INTO " + DB.TablePrefix + "FEASIBILITY (TREATMENTID,CRITERIA) VALUES ('" + treatmentID + "','" + conditionIndexCriteria + "')";
                    SqlCommand cmd = new SqlCommand(insert, connection);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Utility.ExceptionHandling.DataAccessExceptionHandler.HandleException(e, false);
                }
            }
        }