Project

General

Profile

« Previous | Next » 

Revision 36081

changed organizationDAO save to updateorsave

View differences:

modules/uoa-goldoa-service/trunk/src/main/java/eu/dnetlib/goldoa/service/OrganizationManagerImpl.java
6 6
import eu.dnetlib.goldoa.domain.Vocabulary;
7 7
import eu.dnetlib.goldoa.service.dao.BudgetDAO;
8 8
import eu.dnetlib.goldoa.service.dao.OrganizationDAO;
9
import org.apache.commons.codec.digest.DigestUtils;
9 10
import org.springframework.beans.factory.annotation.Autowired;
10 11
import org.springframework.dao.EmptyResultDataAccessException;
11 12
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
......
79 80
	@Override
80 81
	public String saveOrganization(final Organization organization) throws OrganizationManagerException {
81 82

  
82
		if (organization.getId() == null)
83
			organization.setId(UUID.randomUUID().toString());
83
		if (organization.getId() == null) {
84
            organization.setId("portal::" + DigestUtils.md5Hex(organization.getName()));
85
            organization.setSource("portal");
86
        }
84 87

  
85
		try {
86
			organizationDAO.getOrganization(organization.getId());
87
			throw new OrganizationManagerException(OrganizationManagerException.ErrorCause.ALREADY_EXISTS);
88
		} catch (OrganizationManagerException e) {
89
			if (e.getErrorCause() == OrganizationManagerException.ErrorCause.ALREADY_EXISTS)
90
				throw e;
91
		}
88
		organizationDAO.saveOrganization(organization);
89
        organizationDAO.deleteOrganizationBudgets(organization);
90
		organizationDAO.insertOrganizationBudgets(organization);
92 91

  
93
		organizationDAO.insertOrganization(organization);
94
		organizationDAO.insertOrganizationBudget(organization);
95

  
96 92
		return organization.getId();
97 93
	}
98

  
99
	public OrganizationDAO getOrganizationDAO() {
100
		return organizationDAO;
101
	}
102

  
103
	public void setOrganizationDAO(OrganizationDAO organizationDAO) {
104
		this.organizationDAO = organizationDAO;
105
	}
106 94
}
modules/uoa-goldoa-service/trunk/src/main/java/eu/dnetlib/goldoa/service/dao/OrganizationDAO.java
2 2

  
3 3
import eu.dnetlib.goldoa.domain.Budget;
4 4
import eu.dnetlib.goldoa.domain.Organization;
5
import eu.dnetlib.goldoa.domain.Publication;
5 6
import eu.dnetlib.goldoa.domain.Vocabulary;
6 7
import org.springframework.beans.factory.annotation.Autowired;
7 8
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
......
36 37
            "where a.person=?\n" +
37 38
            "group by o.id, name, shortname";
38 39

  
39
	private final String INSERT_ORGANIZATION = "insert into organization (id, name, shortname, source) values (?, ?, ?, 'user')";
40
	private final String INSERT_ORGANIZATION = "insert into organization (name, shortname, source, id) values (?,?,?,?)";
41
    private final String UPDATE_ORGANIZATION = "update organization set name=?, shortname=?, source=? where id=?";
40 42

  
41 43
	private final String INSERT_ORGANIZATION_BUDGET = "insert into organization_budget (organization, budget) values (?, ?)";
42 44

  
......
70 72
		});
71 73
	}
72 74

  
75
    public Organization saveOrganization(Organization organization) {
76
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
77
        Object[] args = {organization.getName(), organization.getShortName(), organization.getSource(), organization.getId()};
78
        int[] types = {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
79

  
80
        if (jdbcTemplate.update(UPDATE_ORGANIZATION, args, types) == 0)
81
            jdbcTemplate.update(INSERT_ORGANIZATION, args, types);
82

  
83
        return organization;
84
    }
85

  
73 86
	public void insertOrganization(final Organization organization) {
74 87
		new JdbcTemplate(dataSource).update(INSERT_ORGANIZATION, new PreparedStatementSetter() {
75 88
			@Override
......
81 94
		});
82 95
	}
83 96

  
84
	public void insertOrganizationBudget(final Organization organization) {
97
	public void insertOrganizationBudgets(final Organization organization) {
85 98
		new JdbcTemplate(dataSource).batchUpdate(INSERT_ORGANIZATION_BUDGET, new BatchPreparedStatementSetter() {
86 99
			@Override
87 100
			public void setValues(PreparedStatement ps, int i) throws SQLException {
......
116 129
            }
117 130
        });
118 131
    }
132

  
133
    public void deleteOrganizationBudgets(Organization organization) {
134
        new JdbcTemplate(dataSource).update("delete from organization_budget where organization = ?", new String[] {organization.getId()}, new int[] {Types.VARCHAR});
135
    }
119 136
}
modules/uoa-goldoa-service/trunk/src/main/java/eu/dnetlib/goldoa/domain/Organization.java
11 11
	private String id;
12 12
	private String name;
13 13
	private String shortName;
14
    private String source;
14 15
	private List<Budget> budgets;
15 16

  
16 17
	public Organization() {
17 18
	}
18 19

  
19
	public Organization(String id, String name, String shortName, List<Budget> budgets) {
20
		this.id = id;
21
		this.name = name;
22
		this.shortName = shortName;
23
		this.budgets = budgets;
24
	}
20
    public Organization(String id, String name, String shortName, String source, List<Budget> budgets) {
21
        this.id = id;
22
        this.name = name;
23
        this.shortName = shortName;
24
        this.source = source;
25
        this.budgets = budgets;
26
    }
25 27

  
26
	public Organization(String id) {
28
    public Organization(String id) {
27 29
		this.id = id;
28 30
	}
29 31

  
......
51 53
		this.shortName = shortName;
52 54
	}
53 55

  
54
	public List<Budget> getBudgets() {
56
    public String getSource() {
57
        return source;
58
    }
59

  
60
    public void setSource(String source) {
61
        this.source = source;
62
    }
63

  
64
    public List<Budget> getBudgets() {
55 65
		return budgets;
56 66
	}
57 67

  

Also available in: Unified diff