private void WriteOperations(IEntity entity)
        {
            OperationHelper oph = new OperationHelper(cw, interfacesEnvironment);

            foreach (IEntityOperation operation in entity.Operations)
            {
                if (operation.Access != EntityOperationAccess.Public)
                {
                    continue;
                }
                cw.BeginFunction("public " + environment.ToOperationSignature(operation, true));
                cw.WriteLine("{0} {1} = new {0}();",
                             NamingHelperStack.ToServiceRequestName(entity, interfacesEnvironment),
                             VarName_Request);
                cw.WriteLine("{0}.{1} = new {2}();",
                             VarName_Request,
                             OperationHelper.GetParamClassPropertyName(operation),
                             oph.GetParamClassName(operation, true));
                foreach (IEntityOperationParam param in operation.Params)
                {
                    cw.WriteLine("{0}.{1}.{2} = {3};",
                                 VarName_Request,
                                 OperationHelper.GetParamClassPropertyName(operation),
                                 oph.GetParamPropertyName(param),
                                 param.Name);
                }
                cw.WriteLine("{0}.{1} = this.{2};",
                             VarName_Request,
                             NamingHelper.ToDTOPropertyName(entity),
                             NamingHelper.PropertyName_AdapterDTO);

                cw.WriteLine("{0} {1} = WebClientFactory.GetJsonClient()",
                             NamingHelperStack.ToServiceResponseName(entity, interfacesEnvironment),
                             VarName_Response);
                cw.Indent++;
                cw.WriteLine(".Post<{0}>(\"{1}\", {2});",
                             NamingHelperStack.ToServiceResponseName(entity, interfacesEnvironment),
                             OperationHelper.GetRestServicePath(operation),
                             VarName_Request);
                cw.Indent--;

                cw.If("{0}.ResponseStatus.ErrorCode == \"{1}\"", VarName_Response, RestServiceHelper.ErrorCode);
                cw.WriteLine("throw new Exception({0}.ResponseStatus.Message);", VarName_Response);
                cw.EndIf();
                cw.WriteLine("this.{0} = {1}.{2};",
                             NamingHelper.PropertyName_AdapterDTO,
                             VarName_Response,
                             NamingHelper.ToDTOPropertyName(entity));


                if (operation.Params.HasReturns)
                {
                    cw.WriteLine("return {0}.{1}.{2};",
                                 VarName_Response,
                                 OperationHelper.GetParamClassPropertyName(operation),
                                 oph.GetParamPropertyName(operation.Params.Returns));
                }
                cw.EndFunction();
            }
        }
Exemplo n.º 2
0
        private void WriteOnPost(IEntity entity)
        {
            cw.BeginFunction("public override object OnPost({0} {1})",
                             NamingHelperStack.ToServiceRequestName(entity, interfacesEnvironment),
                             VarName_Request);
            cw.WriteLine("{0} {1} = new {0}();",
                         NamingHelperStack.ToServiceResponseName(entity, interfacesEnvironment),
                         VarName_Response);
            WriteInitResponse(VarName_Response);
            string requestDto = String.Format("{0}.{1}", VarName_Request, NamingHelper.ToDTOPropertyName(entity));

            cw.BeginTry();

            bool declared = false;

            foreach (IEntityOperation operation in entity.Operations)
            {
                if (operation.Access != EntityOperationAccess.Public)
                {
                    continue;
                }

                string condition = String.Format("{0}.{1} != null", VarName_Request, OperationHelper.GetParamClassPropertyName(operation));
                if (!declared)
                {
                    cw.If(condition);
                    declared = true;
                }
                else
                {
                    cw.ElseIf(condition);
                }
                if (entity.Persistence.Persisted)
                {
                    cw.WriteLine("{0} domain = null;", domainEnvironment.ToTypeName(entity, true));
                    cw.WriteLine("if ({0} != null) domain = {1}.{2}({3});",
                                 requestDto,
                                 domainEnvironment.ToTypeName(entity, true),
                                 DomainLayerConfig.Methods.GetById(entity.PrimaryId, domainEnvironment).Name,
                                 entity.PrimaryId.Attributes.ToNamesString(", ", requestDto + ".", ""));
                    cw.WriteLine("if (domain == null) domain = new {0}();",
                                 domainEnvironment.ToTypeName(entity, true));
                }
                else
                {
                    cw.WriteLine("{0} domain = new {0}();",
                                 domainEnvironment.ToTypeName(entity, true));
                }
                cw.WriteLine("{0}({1}, domain);",
                             MethodName_DTOToDomainObject,
                             requestDto);
                if (entity.Persistence.Persisted)
                {
                    cw.WriteLine("domain.Save();");
                }
                if (operation.Params.HasReturns)
                {
                    cw.Write("{0}.{1}.{2} = ",
                             VarName_Request,
                             OperationHelper.GetParamClassPropertyName(operation),
                             operation.Params.Returns.Name,
                             operation.Name);
                }
                cw.Write("domain.{0}(", operation.Name);
                for (int i = 0; i < operation.Params.Count; i++)
                {
                    if (operation.Params[i].IsOut || operation.Params[i].IsRef)
                    {
                        throw new GlException(
                                  "Output or reference parameters are not allowed on service layer. " +
                                  "Use ordinal parameters or define operation as internal. {0}",
                                  operation.Params[i].ToString());
                    }

                    cw.Write("{0}{1}.{2}.{3}",
                             i == 0 ? "" : ", ",
                             VarName_Request,
                             OperationHelper.GetParamClassPropertyName(operation),
                             operation.Params[i].Name);
                }
                cw.WriteLine(");");
                cw.WriteLine("{0}(domain, {1});",
                             MethodName_DomainObjectToDTO,
                             requestDto);
                //oph.WriteParamClassesPropertiesInitialization(entity);
                cw.WriteLine("{0}.{1} = {2}.{1};",
                             VarName_Response,
                             NamingHelper.ToDTOPropertyName(entity),
                             VarName_Request);
                cw.WriteLine("{0}.{1} = {2}.{1};",
                             VarName_Response,
                             OperationHelper.GetParamClassPropertyName(operation),
                             VarName_Request);
            }

            if (entity.Persistence.Persisted)
            {
                string condition2 = String.Format("{0} != null", requestDto);
                if (!declared)
                {
                    cw.If(condition2);
                }
                else
                {
                    cw.ElseIf(condition2);
                }
                declared = true;
                cw.WriteLine("{0} uow = new {0}();", NamingHelper.ClassName_UnitOfWorkDTO);
                cw.WriteLine("uow.Save({0}.{1});", VarName_Request, NamingHelper.ToDTOPropertyName(entity));
                cw.WriteLine("{0}.{1} = {2}.Commit(uow, null);",
                             VarName_Response,
                             NamingHelper.PropertyName_CommitResult,
                             NamingHelperStack.ToServiceName(NamingHelper.ServiceName_Persistence));
                cw.WriteLine("{0}.{1} = (uow.WorkItems[0].Item as {2});",
                             VarName_Response,
                             NamingHelper.ToDTOPropertyName(entity),
                             NamingHelper.ToDTOTypeName(entity, interfacesEnvironment));
                cw.If("{0}.{1}.HasError == true",
                      VarName_Response,
                      NamingHelper.PropertyName_CommitResult);
                WriteResponceError(VarName_Response, '"' + "Error saving object" + '"', null);
                cw.EndIf();
                cw.ElseIf("{0}.{1} != null", VarName_Request, NamingHelperStack.ParamName_Query);
                cw.WriteLine("{0} {1} = new {0}();",
                             NamingHelperStack.ToServiceResponseCollectionName(entity, interfacesEnvironment),
                             VarName_ResponseList);
                WriteInitResponse(VarName_ResponseList);
                cw.BeginTry();
                cw.WriteLine("IList<{0}> list = {0}.{1}({2}.{3}, {4}.ToDomainQueryParams({2}.{5}), {2}.{6} != null ? {2}.{6}.Value : 0, {2}.{7} != null ? {2}.{7}.Value : {8});",
                             domainEnvironment.ToTypeName(entity, true),
                             DomainLayerConfig.Methods.GetPageByHQL().Name,
                             VarName_Request,
                             NamingHelperStack.ParamName_Query,
                             ClassName_DomainQueryFactory,
                             NamingHelperStack.ParamName_QueryParams,
                             NamingHelperStack.ParamName_PageNumber,
                             NamingHelperStack.ParamName_PageSize,
                             RestServiceHelper.MaxPageSize);
                cw.WriteLine("{0}.{1} = {2}(list);",
                             VarName_ResponseList,
                             NamingHelperStack.ToDTOCollectionPropertyName(entity),
                             MethodName_DomainCollectionToDTO);
                cw.EndTry();
                WriteCatch(VarName_ResponseList);
                cw.WriteLine("return {0};", VarName_ResponseList);
            }
            if (declared)
            {
                cw.EndIf();
            }

            cw.EndTry();
            WriteCatch(VarName_Response);
            cw.WriteLine("return {0};", VarName_Response);
            cw.EndFunction();
        }