首先,在PersonRepository的父类中查找save方法,如下:

@Override
    @TransactionalMethod
    public <S extends D> S save(S dto) {
        if (getApiContext().isPostingRelated()) {
            if (this.getResourceConfiguration().isSecondLevelPostDisabled()) {
                throw new UnsupportedRequestException("Current resource " + this.getResourceConfiguration().getResourceType() + " not supported second level post");
            }
            getApiContext().setDtoRelations(dto);
        }
        this.validate(dto);
        final String id = dto.getId();
        if (id == null) {
            Object entity = this.createEntity(dto);
            this.updateEntity(dto, entity);
            return (S) this.createDto(entity, dto);
        } else {
            return (S) this.updateEntity(dto, findEntity(id));
        }
    }

分析以上代码,理解:

如果id未null,则首先,新建一个entity实体,然后再更新entity字段值。

如果该id存在,则直接更新数据库中的entity的值。

故,为了实现该功能,需要在PersonRepository类重写createEntity方法。

    protected Person createEntity(PersonDto dto) {
        //暂时让contactSource定义为null,创建person后再塞值
       Person person = crmManager.createPerson(dto.getFirstName(),dto.getLastName(),null);
       return person;
    }

这里,第三个contactSource参数的传递,sunny思考了很久,因为,contactSource是个实体,它的构造器有点麻烦。不知道如何下手。

后,转化思路,首先可以传个null值,创建出来一个实体entity,把其他字段比如基本属性将post请求先做完。

于是,再查看父类方法:

protected D updateEntity(D dto, Object entity) {
        this.getConverter().updateEntity(dto, entity);

        this.repositoryEventListeners.forEach(listener -> listener.postUpdateEntity(dto, entity));

        return dto;
    }

点击,进入updateEntity方法,发现它是个接口,找到自己的实现类PersonConverter,重写其方法:

@Override
    public void updateEntity(PersonDto dto, Person entity) {

        String cellularNumber = dto.getMobilePhone();
        entity.setCellularNumber(PhoneNumberUtilWrapperImpl.getInstance().parse(cellularNumber));
        entity.setDepartment(dto.getDepartment());
        entity.setTitle(dto.getTitle());
        entity.setOtherId(dto.getOtherId());
        entity.setBirthdate(dto.getBirthdate());
        entity.setFirstName(dto.getFirstName());
        entity.setLastName(dto.getLastName());
        entity.setGender(dto.getGender());
        entity.setFundEmployee(dto.isFundEmployee());
        entity.setEmailHidden(dto.isEmailHidden());
        entity.setSyncDisabled(dto.isSyncDisabled());
        entity.setSuffix(dto.getSuffix());
        entity.setSalutation(dto.getSalutation());
        entity.setPronunciation(dto.getPronunciation());
        String website = dto.getWebsite();
        if (!ValidationUtils.isWebsiteValid(website)) {
            throw new IllegalArgumentException("Invalid website " + website);
        }
        entity.setWebsite(website);
        entity.setPartyDescription(dto.getDescription());

    }

就这样,sunny将personDto的基本属性都已经完成了post和patch请求。接下来,要完成,比较复杂的字段对应的post请求。

之所以复杂,是因为它的类型是个实体,而该实体的构造器需要的参数,不好获取。也许获取不到。总之,在sunny刚开始动手写代码的时候,很头疼。

经过不断的思考以及查看代码和debug分析,最终,从action开始入手。

找到前端界面对应的ManageContactAction,搜createPerson,找到关键代码:

if (CREATE_PERSON_ACTION.equals(action)) {
                if(!getPermissionManager().hasPermission(getUserManager().getCurrentUser(), PermissionLevel.MODIFY, Party.class)){
                    return new ActionRedirect("/backstop/index.jsp");
                }

                if (!form.getBoolean(IGNORE_WARNING)) {
                    String firstName = StringUtils.defaultString(form.getString("firstName")).trim();
                    String lastName = form.getString("lastName").trim();
                    Collection allByName = getCrmManager().wormHoleFindPartiesByName(firstName + ' ' + lastName);
                    if (!allByName.isEmpty()) {
                        // Warn the user that they are creating (maybe) creating a duplicate person
                        form.set(IGNORE_WARNING, "true");
                        request.setAttribute(CATEGORIES, getCategories(request));
                        request.setAttribute(ALL_CATEGORIES, CategorizeContacts.getModifiableCategories());
                        request.setAttribute(IS_CREATE_ATTR, Boolean.TRUE);
                        request.setAttribute(SIMILAR_PARTIES, allByName);
                        loadDefaultInformation(request, form);
                        return actionMapping.findForward("edit_person");
                    }
                }
                Person person = createPerson(request, form);
                RecentlyViewed.addRecent(request, response, person);
                if (Boolean.valueOf(request.getParameter(ADD_EMPLOYEE))) {
                    addEmployeeToOrganization(person, request.getParameter(COMPANY_NAME));
                }
                form.reset(actionMapping);
                return handleDisplay(person, SUMMARY_TYPE, request, response, actionMapping);

举个例子,sunny想实现contactSource的post请求,则,直接在ManageContactAction中搜:contactSource,然后就能找到对应的关键代码:

@TransactionalMethod
    private Person createPerson(HttpServletRequest request, ManageContactsForm form) throws UserInputException {
        Code contactSource = CodeLookup.getInstance().getContactSourceCode(null);
        if ( ! StringUtils.isEmpty(form.getString(CONTACT_SOURCE_ATTR)) ) {
            contactSource = CodeLookup.getInstance().getContactSourceCode((form.getInteger(CONTACT_SOURCE_ATTR)));
        }
        Person person = getCrmManager().createPerson(form.getString("lastName"), form.getString("firstName"),
                contactSource);
        Integer employerPartyId = form.getInteger(EMPLOYER_PARTY_ID);
        if (!employerPartyId.equals(NumberUtil.INTEGER_ZERO)) {
            Organization organization;
            try {
                organization = getCrmManager().findOrganizationById(employerPartyId);
            } catch (PartyNotFoundException e) {
                logger.error("Problem: " + e.getMessage(), e);
                throw new SystemException("Problem: " + e.getMessage(), e);
            }
            organization.addEmployee(person);
        }

        editContact(form, person, (form.get(LOCATIONS) != null), request);
        return person;
    }

需要注意的是,在post请求的时候,这里传入的contactSource是前端字符串对应的value值,也就是contactSource的id值。---这一点,感谢simon帮我一起分析。

结合以上代码,sunny的代码重构如下:

@Override
    public void updateEntity(PersonDto dto, Person entity) {

        //there ,ContactSourceName() is a responding contactSource Code.
        if(!Objects.isNull(dto.getContactSourceName())){
            int contactSourceCode = Integer.parseInt(dto.getContactSourceName());
            Code contactSource = CodeLookup.getInstance().getContactSourceCode(contactSourceCode);
            entity.setContactSource(contactSource);
        }

        String cellularNumber = dto.getMobilePhone();
        entity.setCellularNumber(PhoneNumberUtilWrapperImpl.getInstance().parse(cellularNumber));
        entity.setDepartment(dto.getDepartment());
        entity.setTitle(dto.getTitle());
        entity.setOtherId(dto.getOtherId());
        entity.setBirthdate(dto.getBirthdate());
        entity.setFirstName(dto.getFirstName());
        entity.setLastName(dto.getLastName());
        entity.setGender(dto.getGender());
        entity.setFundEmployee(dto.isFundEmployee());
        entity.setEmailHidden(dto.isEmailHidden());
        entity.setSyncDisabled(dto.isSyncDisabled());
        entity.setSuffix(dto.getSuffix());
        entity.setSalutation(dto.getSalutation());
        entity.setPronunciation(dto.getPronunciation());
        String website = dto.getWebsite();
        if (!ValidationUtils.isWebsiteValid(website)) {
            throw new IllegalArgumentException("Invalid website " + website);
        }
        entity.setWebsite(website);
        entity.setPartyDescription(dto.getDescription());

    }

其他复杂的字段,类似操作如上。sunny就不再一一例举了。

Person.post请求------详细过程的更多相关文章

  1. Asp.net页面生命周期详解任我行(3)-服务器处理请求详细过程

    前言 百度了一下才知道,传智的邹老师桃李满天下呀,我也是邹老师的粉丝,最开始学习页面生命周期的时候也是看了邹老师的视频. 本人是参考了以下前辈的作品,本文中也参合了本人心得,绝非有意盗版,旨在传播,最 ...

  2. Person的delete请求--------详细过程

    首先,数据库的增删改查都是在PersonRepository中实现,因此,直接进入PersonRepository,找到其父类,搜索delete. @Override @TransactionalMe ...

  3. 在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程)

    在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程) 原文链接:http://www.360doc.com/content/14/1117/10/16948208_42571794 ...

  4. 一个http请求的详细过程

    一个http请求的详细过程 我们来看当我们在浏览器输入http://www.mycompany.com:8080/mydir/index.html,幕后所发生的一切. 首先http是一个应用层的协议, ...

  5. USB枚举详细过程剖析(转)

    USB枚举详细过程剖析(转) 原文地址:http://blog.163.com/luge_arm/blog/static/6774972620071018117290/ 从驱动开发网看到一篇<U ...

  6. HTTP请求响应过程 与HTTPS区别

    原文:HTTP请求响应过程 与HTTPS区别 HTTP协议学习笔记,基础,干货 HTTP协议 HTTP协议主要应用是在服务器和客户端之间,客户端接受超文本. 服务器按照一定规则,发送到客户端(一般是浏 ...

  7. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

    文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...

  8. IDEA搭建SSMM框架(详细过程)

    IDEA搭建SSMM框架(详细过程) 相关环境 Intellij IDEA Ultimate Tomcat JDK MySql 5.6(win32/win64) Maven (可使用Intellij ...

  9. Tomcat配置(三):tomcat处理连接的详细过程

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

随机推荐

  1. python 获取本机ip地址的方法(Unix 平台)

    #!/usr/bin/python import socket import fcntl import struct def get_ip_address(ifname): s = socket.so ...

  2. 6、Selenium+Python登录案例 -- Github

    一:登录 1.指定浏览器,打开网址:https://github.com/login 2.设置等待时间: time.sleep(3) or driver.implicitly_wait(3) 3.输入 ...

  3. HDU4006(小根堆)

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  4. 几种排序方式的java实现(02:希尔排序,归并排序,堆排序)

    代码(部分为别人代码): 1.希尔排序(ShellSort) /* * 希尔排序:先取一个小于n的整数d1作为第一个增量, * 把文件的全部记录分成(n除以d1)个组.所有距离为d1的倍数的记录放在同 ...

  5. Breaking Good

    Time limit2000 ms Memory limit262144 kB Breaking Good is a new video game which a lot of gamers want ...

  6. linux下面的挂载点讲解

    linux.unix这类操作系统将系统中的一切都作为文件来管理.在windows中我们常见的硬件设备.磁盘分区等,在linux.unix中都被视作文件,对设备.分区的访问就是读写对应的文件.挂载点实际 ...

  7. oracle错误-ORA-12519, TNS:no appropriate service handler found

    转自:https://blog.csdn.net/u013147600/article/details/48380841

  8. xcode编译静态库选择cpu架构

    此前编译了一个静态库,默认支持了armv7,armv7s,arm64 编译的话肯定是上面三个静态库分别编译出来,然后在把这三个合并成一个单独的库. 如果单个库的大小是10M,那编译总的库大概就30M了 ...

  9. hbase.client.RetriesExhaustedException: Can't get the locations hive关联Hbase查询报错

    特征1: hbase.client.RetriesExhaustedException: Can't get the locations 特征2: hbase日志报错如下:org.apache.zoo ...

  10. matlab学习笔记(4)

    第9章 数学建模函数 曲线拟合: 多项式拟合: polyfit()函数:采用最小二乘法进行多项式拟合. polyfit(x,y,n);   %对于数据集[x,y],找到次数为n的多项式. [p E] ...