Esempio n. 1
0
        public Announcement Add(int announcementId, AnnouncementTypeEnum type, int attachmentId)
        {
            var annDetails = ServiceLocator.GetAnnouncementService(type).GetAnnouncementDetails(announcementId);

            Trace.Assert(Context.PersonId.HasValue);
            Trace.Assert(Context.SchoolLocalId.HasValue);
            if (!CanAttach(annDetails))
            {
                throw new ChalkableSecurityException();
            }

            using (var u = Update())
            {
                var attDa = new AttachmentDataAccess(u);
                var att   = attDa.GetById(attachmentId);
                if (att.PersonRef != Context.PersonId)
                {
                    throw new ChalkableSecurityException();
                }

                var annAtt = new AnnouncementAttachment
                {
                    AnnouncementRef = annDetails.Id,
                    AttachedDate    = Context.NowSchoolTime,
                    Order           = ServiceLocator.GetAnnouncementService(type).GetNewAnnouncementItemOrder(annDetails),
                    AttachmentRef   = attachmentId
                };
                new AnnouncementAttachmentDataAccess(u).Insert(annAtt);
                att.LastAttachedDate = annAtt.AttachedDate;
                attDa.Update(att);
                u.Commit();
                NotifyUsers(annDetails, type);
            }
            return(annDetails);
        }
Esempio n. 2
0
        /// <summary>
        /// 将上传文件加到某个批次
        /// </summary>
        /// <param name="curr_user"></param>
        /// <param name="postedFile"></param>
        /// <param name="batch_id"></param>
        /// <returns></returns>
        public static SysAttachment InsertAttachment(SysUserInfo curr_user, System.Web.HttpPostedFile postedFile, string batch_id)
        {
            string originalFilePath = postedFile.FileName;
            // save file
            // get user's site
            SysSiteList site_entity = AttachmentDataAccess.GetSysSiteList(curr_user.SiteSerial);

            if (site_entity == null)
            {
                throw new ApplicationException(string.Format("site [{0}] does not exists.", curr_user.SiteSerial));
            }

            // get site's upload path
            SysDocPath doc_path_entity = AttachmentDataAccess.GetSysDocPath(site_entity.CurrentUploadPathId);

            if (doc_path_entity == null)
            {
                throw new ApplicationException(string.Format("path_id [{0}]does not configed in db.", site_entity.CurrentUploadPathId));
            }
            string baseDir = doc_path_entity.DocPath;

            // get child dir
            DateTime dt_now  = DateTime.Now;
            string   subDir  = dt_now.ToString("yyyyMM");
            string   fileDir = Path.Combine(baseDir, subDir);

            if (!System.IO.Directory.Exists(fileDir))
            {
                System.IO.Directory.CreateDirectory(fileDir);
            }

            string attachmentId    = System.Guid.NewGuid().ToString("D");
            string currentFileName = attachmentId + Path.GetExtension(originalFilePath);
            string currentFilePath = Path.Combine(fileDir, currentFileName);

            postedFile.SaveAs(currentFilePath);

            SysAttachment item = new SysAttachment();

            item.AttachmentId    = attachmentId;
            item.CurrentFileName = currentFileName;
            item.CurrentFileDir  = subDir;
            item.FileSize        = postedFile.ContentLength;
            // postedFile.
            item.OriginalFileName = Path.GetFileName(originalFilePath);
            item.UploadTime       = dt_now;
            item.UploadUser       = curr_user.UserId;
            item.ContentType      = postedFile.ContentType;
            item.PathId           = site_entity.CurrentUploadPathId;
            item.FileExtension    = Path.GetExtension(originalFilePath);

            SysBatchUpload batch_entity = new SysBatchUpload(batch_id, attachmentId);

            AttachmentDataAccess.InsertAttachment(item, batch_entity);

            return(item);
        }
Esempio n. 3
0
        public override void Submit(int announcementId)
        {
            Trace.Assert(Context.SchoolLocalId.HasValue);
            var res = GetAnnouncementDetails(announcementId);

            if (res.ClassAnnouncementData.Title == null || Exists(res.ClassAnnouncementData.Title, res.ClassAnnouncementData.ClassRef, res.ClassAnnouncementData.Expires, res.ClassAnnouncementData.Id))
            {
                throw new ChalkableException("Invalid Class Announcement Title");
            }

            using (var uow = Update())
            {
                var da = CreateClassAnnouncementDataAccess(uow);
                AnnouncementSecurity.EnsureInModifyAccess(res, Context);
                if (res.ClassAnnouncementData.IsDraft)
                {
                    ServiceLocator.AnnouncementAssignedAttributeService.ValidateAttributes(res.AnnouncementAttributes);
                    res.ClassAnnouncementData.State   = AnnouncementState.Created;
                    res.ClassAnnouncementData.Created = Context.NowSchoolTime.Date;

                    var activity = new Activity();
                    MapperFactory.GetMapper <Activity, AnnouncementDetails>().Map(activity, res);
                    activity = ConnectorLocator.ActivityConnector.CreateActivity(res.ClassAnnouncementData.ClassRef, activity);
                    if (da.Exists(activity.Id))
                    {
                        throw new ChalkableException("Announcement with such activityId already exists");
                    }

                    var annAttDa = new AnnouncementAssignedAttributeDataAccess(uow);
                    annAttDa.Delete(res.AnnouncementAttributes);
                    MapperFactory.GetMapper <AnnouncementDetails, Activity>().Map(res, activity);
                    var attributes = res.AnnouncementAttributes.Where(x => x.Attachment != null).ToList();
                    if (attributes.Count > 0)
                    {
                        var atts = new AttachmentDataAccess(uow).GetBySisAttachmentIds(attributes.Select(a => a.Attachment.SisAttachmentId.Value).ToList());
                        foreach (var attribute in res.AnnouncementAttributes)
                        {
                            if (attribute.Attachment == null)
                            {
                                continue;
                            }
                            var att = atts.FirstOrDefault(x => x.SisAttachmentId == attribute.Attachment.SisAttachmentId);
                            if (att == null)
                            {
                                continue;
                            }
                            attribute.AttachmentRef = att.Id;
                        }
                    }
                    annAttDa.Insert(res.AnnouncementAttributes);
                }
                da.Update(res.ClassAnnouncementData);
                uow.Commit();
            }
        }
Esempio n. 4
0
        public AnnouncementAssignedAttribute AddAttachment(AnnouncementTypeEnum announcementType, int announcementId, int assignedAttributeId, int attachmentId)
        {
            Trace.Assert(Context.PersonId.HasValue);
            var ann = ServiceLocator.GetAnnouncementService(announcementType).GetAnnouncementById(announcementId);

            if (!CanAttach(ann))
            {
                throw new ChalkableSecurityException();
            }

            using (var uow = Update())
            {
                var da        = new AnnouncementAssignedAttributeDataAccess(uow);
                var attribute = da.GetById(assignedAttributeId);
                if (attribute.AttachmentRef > 0)
                {
                    throw new ChalkableSisException("You can't attach more than one file to an attribute");
                }

                var attDa      = new AttachmentDataAccess(uow);
                var attachment = attDa.GetById(attachmentId);
                if (attachment.PersonRef != Context.PersonId)
                {
                    throw new ChalkableSecurityException();
                }

                attribute.AttachmentRef = attachment.Id;
                attribute.Attachment    = attachment;
                da.Update(attribute);

                if (announcementType == AnnouncementTypeEnum.Class)
                {
                    if (!attachment.SisAttachmentId.HasValue)
                    {
                        var attContent = ServiceLocator.AttachementService.GetAttachmentContent(attachment);
                        var stiAtt     = ConnectorLocator.AttachmentConnector.UploadAttachment(attachment.Name, attContent.Content).Last();
                        MapperFactory.GetMapper <Attachment, StiAttachment>().Map(attachment, stiAtt);
                        attDa.Update(attachment);
                    }

                    if (attribute.SisActivityAssignedAttributeId.HasValue)
                    {
                        var stiAttribute = ConnectorLocator.ActivityAssignedAttributeConnector.GetAttribute(attribute.SisActivityId.Value, attribute.SisActivityAssignedAttributeId.Value);
                        MapperFactory.GetMapper <ActivityAssignedAttribute, AnnouncementAssignedAttribute>().Map(stiAttribute, attribute);
                        ConnectorLocator.ActivityAssignedAttributeConnector.Update(stiAttribute.ActivityId, stiAttribute.Id, stiAttribute);
                    }
                }
                uow.Commit();
                return(attribute);
            }
        }
Esempio n. 5
0
        private static IList <AnnouncementAssignedAttribute> UploadMissingAttachments(ClassAnnouncement classAnn, IList <AnnouncementAssignedAttribute> attributes, UnitOfWork u
                                                                                      , ConnectorLocator connectorLocator, IServiceLocatorSchool serviceLocator)
        {
            var attributesForUpdate = attributes.Where(x => x.Attachment != null && x.Attachment.SisAttachmentId.HasValue && !x.AttachmentRef.HasValue).ToList();

            if (attributesForUpdate.Count == 0)
            {
                return(new List <AnnouncementAssignedAttribute>());
            }
            var da            = new AttachmentDataAccess(u);
            var existingsAtts = da.GetBySisAttachmentIds(attributesForUpdate.Select(x => x.Attachment.SisAttachmentId.Value).ToList());
            var attsForUpload = new List <Attachment>();

            foreach (var attribute in attributesForUpdate)
            {
                var attachment  = attribute.Attachment;
                var existingAtt = existingsAtts.FirstOrDefault(x => x.SisAttachmentId == attachment.SisAttachmentId);
                if (existingAtt == null)
                {
                    var content = connectorLocator.AttachmentConnector.GetAttachmentContent(attribute.Attachment.SisAttachmentId.Value);
                    if (serviceLocator.CrocodocService.IsDocument(attribute.Attachment.Name))
                    {
                        attachment.Uuid = serviceLocator.CrocodocService.UploadDocument(attribute.Attachment.Name, content).uuid;
                    }
                    attachment.UploadedDate     = classAnn.Created;
                    attachment.LastAttachedDate = attachment.UploadedDate;
                    attachment.PersonRef        = classAnn.PrimaryTeacherRef;
                    attsForUpload.Add(attachment);
                }
                else
                {
                    attribute.AttachmentRef = existingAtt.Id;
                }
            }
            if (attsForUpload.Count > 0)
            {
                da.Insert(attsForUpload);
                existingsAtts = da.GetBySisAttachmentIds(attsForUpload.Select(x => x.SisAttachmentId.Value).ToList());
                foreach (var attribute in attributesForUpdate.Where(x => !x.AttachmentRef.HasValue))
                {
                    var att = existingsAtts.First(x => x.SisAttachmentId == attribute.Attachment.SisAttachmentId);
                    attribute.Attachment    = att;
                    attribute.AttachmentRef = att.Id;
                }
            }
            return(attributesForUpdate);
        }
Esempio n. 6
0
 public void Delete(int attachmentId)
 {
     DoUpdate(u =>
     {
         var da  = new AttachmentDataAccess(u);
         var att = da.GetById(attachmentId);
         if (att.PersonRef != Context.PersonId)
         {
             throw new ChalkableSecurityException();
         }
         da.Delete(attachmentId);
         if (att.SisAttachmentId.HasValue)
         {
             ConnectorLocator.AttachmentConnector.DeleteAttachment(att.SisAttachmentId.Value);
         }
         else
         {
             ServiceLocator.StorageBlobService.DeleteBlob(ATTACHMENT_CONTAINER_ADDRESS, att.RelativeBlobAddress);
         }
     });
 }
Esempio n. 7
0
        public static Attachment Upload(string name, byte[] content, bool uploadToSti, UnitOfWork unitOfWork, IServiceLocatorSchool serviceLocator,
                                        ConnectorLocator connectorLocator, bool uploadToCrocodoc = true)
        {
            var context = serviceLocator.Context;

            Trace.Assert(context.PersonId.HasValue);
            var res = new Attachment
            {
                Name             = name,
                PersonRef        = context.PersonId.Value,
                Uuid             = null,
                UploadedDate     = context.NowSchoolTime,
                LastAttachedDate = context.NowSchoolTime
            };

            if (uploadToCrocodoc)
            {
                res.Uuid = UploadToCrocodoc(res.Name, content, serviceLocator);
            }

            var da = new AttachmentDataAccess(unitOfWork);

            if (uploadToSti)
            {
                var stiAtt = connectorLocator.AttachmentConnector.UploadAttachment(name, content).Last();

                //TODO : use mapping
                res.Name            = stiAtt.Name;
                res.SisAttachmentId = stiAtt.AttachmentId;
                res.MimeType        = stiAtt.MimeType;
                da.Insert(res);
                return(da.GetLast(context.PersonId.Value));
            }
            da.Insert(res);
            res = da.GetLast(context.PersonId.Value);
            res.RelativeBlobAddress = UploadToBlob(res, content, serviceLocator);
            da.Update(res);
            return(res);
        }
Esempio n. 8
0
 /// <summary>
 /// 获取某个批次的所有附件
 /// </summary>
 /// <param name="batch_id">批次ID</param>
 /// <returns></returns>
 public static List <SysAttachment> GetBatchAttachments(string batch_id)
 {
     return(AttachmentDataAccess.GetBatchAttachments(batch_id));
 }
Esempio n. 9
0
 /// <summary>
 /// 删除批次中的某个文件
 /// </summary>
 /// <param name="batch_id"></param>
 /// <param name="attachment_id"></param>
 public static void DeleteBatchAttachment(string batch_id, string attachment_id)
 {
     AttachmentDataAccess.DeleteBatchAttachment(batch_id, attachment_id);
 }
Esempio n. 10
0
 /// <summary>
 /// 删除某个批次的所有文件
 /// </summary>
 /// <param name="batch_id"></param>
 public static void DeleteBatch(string batch_id)
 {
     AttachmentDataAccess.DeleteBatch(batch_id);
 }
Esempio n. 11
0
 public static string GetDocPath(int path_id)
 {
     return(AttachmentDataAccess.GetDocPath(path_id));
 }
Esempio n. 12
0
 internal static void InsertAttachment(SysAttachment item)
 {
     AttachmentDataAccess.InsertAttachment(item);
 }
Esempio n. 13
0
 public static SysAttachment GetAttachment(string attachmentId)
 {
     return(AttachmentDataAccess.GetAttachment(attachmentId));
 }