Esempio n. 1
0
        public static Boolean CreateMessage(string title, string body, MessageType type, int deviceId)
        {
            var transaction = new Transaction(IsolationLevel.ReadCommitted, "create message");

            try
            {
                var messageEntity = new MessageEntity {
                    MessageType = type, Title = title, Body = body, CreateTime = DateTime.Now, StartTime = DateTime.Now
                };
                transaction.Add(messageEntity);
                messageEntity.Save();

                var deviceMessage = new DeviceMessageEntity {
                    DeviceId = deviceId, MessageId = messageEntity.MessageId
                };
                transaction.Add(deviceMessage);
                deviceMessage.Save();

                transaction.Commit();

                return(true);
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                Loggers.LogException(GlobalSettings.SYSTEM_DAEMON_USER_ID, ex);
                return(false);
            }
            finally
            {
                transaction.Dispose();
            }
        }
Esempio n. 2
0
        public static Boolean DeleteAMessageImpl(long messageId, int organizationId)
        {
            // Make sure customer owns the message, thus preventing customers from possibly
            // deleting other customers messages.
            OrganizationEntity organization = new OrganizationEntity(organizationId);

            foreach (LocationEntity location in organization.Locations)
            {
                DeviceCollection devices = new DeviceCollection();
                devices.GetMulti(new PredicateExpression(DeviceFields.LocationId == location.LocationId));
                foreach (DeviceEntity device in devices)
                {
                    DeviceMessageCollection deviceMessages = new DeviceMessageCollection();
                    PredicateExpression     filter         = new PredicateExpression {
                        DeviceMessageFields.DeviceId == device.DeviceId, DeviceMessageFields.MessageId == messageId
                    };
                    deviceMessages.GetMulti(filter);
                    if (deviceMessages.Count > 0)
                    {
                        DeviceMessageEntity deviceMessage = deviceMessages[0];
                        deviceMessage.DeliveryTime = DateTime.UtcNow;
                        deviceMessage.Save();
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 3
0
        public ActionResult Add(ComposeMessage model)
        {
            if (ModelState.IsValid)
            {
                var transaction = new Transaction(IsolationLevel.ReadCommitted, "add message");
                try
                {
                    // add the message
                    var message = new MessageEntity
                    {
                        MessageType = model.Type,
                        Title       = model.Title,
                        Body        = model.Body,
                        CreateTime  = DateTime.UtcNow,
                        StartTime   = model.StartTime.ToUniversalTime(),
                        EndTime     = model.EndTime.HasValue ? model.EndTime.Value.ToUniversalTime() : new DateTime?()
                    };
                    transaction.Add(message);
                    message.Save();

                    // add the devicemessage entities for all the tos
                    foreach (var device in model.Tos)
                    {
                        var dm = new DeviceMessageEntity
                        {
                            DeviceId  = device.DeviceId,
                            MessageId = message.MessageId
                        };
                        transaction.Add(dm);
                        dm.Save();
                    }

                    transaction.Commit();

                    return(new EmptyResult());
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new HttpException(417, Message.AddError);
                }
                finally
                {
                    transaction.Dispose();
                }
            }

            Response.StatusCode             = 417;
            Response.TrySkipIisCustomErrors = true;

            return(PartialView("Edit", model));
        }
Esempio n. 4
0
        public ActionResult Edit(long messageId, ComposeMessage model)
        {
            var message = new MessageEntity(messageId);

            if (message.IsNew)
            {
                throw new HttpException(404, SharedRes.Error.NotFound_Message);
            }

            if (ModelState.IsValid)
            {
                var transaction = new Transaction(IsolationLevel.ReadCommitted, "edit message");
                try
                {
                    // if the device list has missing devices create a new message and remove the intersecting devices from the old message
                    // only if the message has been delivered, otherwise remove the device message normally below
                    if (message.DeviceMessages.Where(x => x.DeliveryTime != null).Select(x => x.Device).Except(model.Tos).Any())
                    {
                        // remove the devices from the old message that are included in the new model
                        foreach (var device in message.DeviceMessages.Select(x => x.Device).Intersect(model.Tos))
                        {
                            var deviceMessage = new DeviceMessageEntity(device.DeviceId, message.MessageId);
                            transaction.Add(deviceMessage);
                            deviceMessage.Delete();
                        }

                        // create a new message for the model leaving the old message Already Delivered
                        message = new MessageEntity
                        {
                            MessageType = model.Type,
                            Title       = model.Title,
                            Body        = model.Body,
                            CreateTime  = DateTime.UtcNow,
                            StartTime   = model.StartTime.ToUniversalTime(),
                            EndTime     =
                                model.EndTime.HasValue
                                                         ? model.EndTime.Value.ToUniversalTime()
                                                         : new DateTime?()
                        };
                        transaction.Add(message);
                    }
                    else
                    {
                        // devices are only added to the message, none removed, so update the existing message
                        message.MessageType = model.Type;
                        message.Title       = model.Title;
                        message.Body        = model.Body;
                        message.CreateTime  = DateTime.UtcNow;
                        message.StartTime   = model.StartTime.ToUniversalTime();
                        message.EndTime     =
                            model.EndTime.HasValue
                                ? model.EndTime.Value.ToUniversalTime()
                                : new DateTime?();
                        transaction.Add(message);
                    }
                    message.Save();

                    // remove any removed device IDs,
                    //  if messages have already been delivered a new message will be created above and nothing more will be done here
                    foreach (var device in message.DeviceMessages.Select(x => x.Device).Except(model.Tos))
                    {
                        var deviceMessage = new DeviceMessageEntity(device.DeviceId, message.MessageId);
                        transaction.Add(deviceMessage);
                        deviceMessage.Delete();
                    }

                    // add the devicemessage entities for all the tos
                    foreach (var device in model.Tos)
                    {
                        var dm = new DeviceMessageEntity(device.DeviceId, message.MessageId)
                        {
                            DeviceId  = device.DeviceId,
                            MessageId = message.MessageId
                        };
                        transaction.Add(dm);
                        dm.Save();
                    }

                    transaction.Commit();
                    return(new EmptyResult());
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new HttpException(417, Message.AddError);
                }
                finally
                {
                    transaction.Dispose();
                }
            }

            Response.StatusCode             = 417;
            Response.TrySkipIisCustomErrors = true;

            return(PartialView(model));
        }