summaryrefslogtreecommitdiff
path: root/cpp/demo/Database/Oracle/occi/EmpI.cpp
blob: 3b0020fd9b06ec025e4c3e217d1a7e8c2211a4e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <EmpI.h>

using namespace std;
using namespace oracle::occi;

EmpI::EmpI(const RefAny& ref, const ConnectionHolderPtr& conh, const DeptFactoryIPtr& factory) :
    _ref(ref), _conh(conh), _factory(factory)
{
}

HR::EmpDesc 
EmpI::getDesc(const Ice::Current& current)
{
    Environment* env = _factory->getEnv();
    HR::EmpDesc result;
 
    Ref<EMP_T> empRef = decodeRef(current.id.name, env, _conh->connection());
    
    result.empno = empRef->getEmpno();
    result.ename = empRef->getEname();
    result.job = empRef->getJob();
    
    Ref<EMP_T> mgrRef = empRef->getMgrref();
    if(!mgrRef.isNull())
    {
        Ice::Identity mgrId;
        mgrId.name = encodeRef(mgrRef, env);
        mgrId.category = _factory->getCategory();
        result.mgr = HR::EmpPrx::uncheckedCast(current.adapter->createProxy(mgrId));
    }
    
    result.hiredate = empRef->getHiredate().toText();
    
    if(!empRef->getSal().isNull())
    {
        try
        {
            result.sal = empRef->getSal().toText(env, "99999.99");
        }
        catch(const std::exception&)
        {
            cerr << "sal overflow" << endl;
        }
    }
    
    if(!empRef->getComm().isNull())
    {
        try
        {
            result.comm = empRef->getComm().toText(env, "99999.99");
        }
        catch(const std::exception&)
        {
            cerr << "comm overflow" << endl;
        }
    }
    
    Ref<DEPT_T> deptRef = empRef->getDeptref();
    if(!deptRef.isNull())
    {
        Ice::Identity deptId;
        deptId.name = encodeRef(deptRef, env);
        deptId.category = _factory->getCategory();
        result.edept = HR::DeptPrx::uncheckedCast(current.adapter->createProxy(deptId));
    }
    _conh->commit();
    return result;
}
    
void 
EmpI::updateField(const string& field, const string& newValue, const Ice::Current& current)
{
    Ref<EMP_T> empRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());
    
    if(field == "ename")
    {
        empRef->setEname(newValue);
    }
    else if(field == "job")
    {
        empRef->setJob(newValue);
    }
    else if(field == "hiredate")
    {
        if(newValue == "")
        {
            empRef->setHiredate(Date());
        }
        else
        {
            Date hiredate(_factory->getEnv());
            hiredate.fromText(newValue);
            empRef->setHiredate(hiredate);
        }
    }
    else if(field == "sal")
    {
        if(newValue == "")
        {
            empRef->setSal(Number());
        }
        else
        {
            Number sal(0);
            sal.fromText(_factory->getEnv(), newValue, "99999.99");
            empRef->setSal(sal);
        }
    }
    else if(field == "comm")
    {
        if(newValue == "")
        {
            empRef->setComm(Number());  
        }
        else
        {
            Number comm(0);
            comm.fromText(_factory->getEnv(), newValue, "0.999");
            empRef->setComm(comm);
        }
    }
    else
    {
        throw HR::SqlException("There is no field " + field + " in type EMP_T");
    }   
    empRef->markModified();
    _conh->commit();
}

void 
EmpI::updateMgr(int newMgr, const Ice::Current& current)
{
    Ref<EMP_T> empRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());
    
    if(newMgr == 0)
    {
        empRef->setMgrref(Ref<EMP_T>());
    }
    else
    {
        Ref<EMP_T> mgrRef = _factory->findEmpRefByNo(newMgr, _conh->connection());
        
        if(mgrRef.isNull())
        {
            throw HR::SqlException("There is no employee with this empno");
        }
        empRef->setMgrref(mgrRef);
    }
    empRef->markModified();
    _conh->commit();
}

void 
EmpI::updateDept(int newDept, const Ice::Current& current)
{
    Ref<EMP_T> empRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());
    
    if(newDept == 0)
    {
        empRef->setDeptref(Ref<DEPT_T>());
    }
    else
    {
        Ref<DEPT_T> deptRef = _factory->findDeptRefByNo(newDept, _conh->connection());
        
        if(deptRef.isNull())
        {
            throw HR::SqlException("There is no department with this deptno");
        }
        empRef->setDeptref(deptRef);
    }
    empRef->markModified();
    _conh->commit();
}

void 
EmpI::remove(const Ice::Current& current)
{
    Ref<EMP_T> empRef = decodeRef(current.id.name, _factory->getEnv(), _conh->connection());
    empRef->markDelete();
    _conh->commit();
}