Esempio n. 1
0
        public object Post(DataPackageSaveDefaultRequestTep request)
        {
            //Get all requests from database
            var context = TepWebContext.GetWebContext(PagePrivileges.UserView);
            WebDataPackageTep result;

            try{
                context.Open();
                context.LogInfo(this, string.Format("/data/package/default POST"));

                DataPackage def = DataPackage.GetTemporaryForCurrentUser(context);
                DataPackage tmp = new DataPackage(context);

                context.StartTransaction();

                if (string.IsNullOrEmpty(request.Identifier) && string.IsNullOrEmpty(request.Name))
                {
                    throw new Exception("No identifier set");
                }
                //var identifier = !string.IsNullOrEmpty(request.Identifier) ? TepUtility.ValidateIdentifier(request.Identifier) : TepUtility.ValidateIdentifier(request.Name);

                if (request.Overwrite && tmp.OwnerId == context.UserId)
                {
                    tmp = DataPackage.FromNameAndOwner(context, request.Name, context.UserId);
                    foreach (var res in tmp.Resources)
                    {
                        res.Delete();
                    }
                    tmp.Items = new EntityList <RemoteResource>(context);
                }
                else
                {
                    tmp = (DataPackage)request.ToEntity(context, tmp);
                    //tmp.Identifier = identifier;
                    try{
                        tmp.Store();
                    }catch (DuplicateEntityIdentifierException e) {
                        //tmp = DataPackage.FromIdentifier(context, identifier);
                        //if(tmp.OwnerId == context.UserId){
                        throw new DuplicateNameException(e.Message);
                        //} else {
                        //    throw e;
                        //}
                    }catch (Exception e) {
                        if (e.Message.StartsWith("Duplicate entry"))
                        {
                            throw new DuplicateNameException(e.Message);
                        }
                        throw e;
                    }
                }

                foreach (RemoteResource res in def.Resources)
                {
                    RemoteResource tmpres = new RemoteResource(context);
                    tmpres.Location = res.Location;
                    tmp.AddResourceItem(tmpres);
                }
                context.Commit();

                result = new WebDataPackageTep(tmp);
                context.Close();
            }catch (Exception e) {
                context.LogError(this, e.Message, e);
                context.Close();
                throw e;
            }

            return(result);
        }