1 | ''' |
---|
2 | BSD Licence |
---|
3 | Copyright (c) 2012, Science & Technology Facilities Council (STFC) |
---|
4 | All rights reserved. |
---|
5 | |
---|
6 | Redistribution and use in source and binary forms, with or without modification, |
---|
7 | are permitted provided that the following conditions are met: |
---|
8 | |
---|
9 | * Redistributions of source code must retain the above copyright notice, |
---|
10 | this list of conditions and the following disclaimer. |
---|
11 | * Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | this list of conditions and the following disclaimer in the documentation |
---|
13 | and/or other materials provided with the distribution. |
---|
14 | * Neither the name of the Science & Technology Facilities Council (STFC) |
---|
15 | nor the names of its contributors may be used to endorse or promote |
---|
16 | products derived from this software without specific prior written permission. |
---|
17 | |
---|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
---|
20 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
21 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
---|
22 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
---|
23 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
25 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | Created on 3 Aug 2012 |
---|
30 | |
---|
31 | @author: mnagni |
---|
32 | ''' |
---|
33 | from cedaMoles.libs.epb import EPB |
---|
34 | from sqlalchemy import Table, Column, ForeignKey, Integer, String, event |
---|
35 | from sqlalchemy.orm import mapper |
---|
36 | from ea_model.moles3_4.utilities.mo_responsiblepartyinfo import MO_ResponsiblePartyInfo |
---|
37 | |
---|
38 | class PartyIndexes(object): |
---|
39 | ''' |
---|
40 | Allows to assign on a MO_ResponsiblePartyInfo basis an index |
---|
41 | to the the referred MO_Party instances. |
---|
42 | |
---|
43 | # The primary key refers to the MO_ResponsiblePartyInfo.id |
---|
44 | # The sorted_party column shall contain a string formatted as |
---|
45 | IDParty(1):partyOrder(1), IDParty(2):partyOrder(2), ..... |
---|
46 | |
---|
47 | where |
---|
48 | # IDParty(x) is a MO_Party.id in the mo_responsiblepartyinfo.party list |
---|
49 | # partyOrder(x) is the assigned index |
---|
50 | ''' |
---|
51 | |
---|
52 | def __init__(self): |
---|
53 | self.id = None |
---|
54 | self.party_order = None |
---|
55 | |
---|
56 | def associateMOParty_indexes(metadata): |
---|
57 | ''' |
---|
58 | Creates the table used to store the index associated per ResponsiblePartyIngo |
---|
59 | to each referenced party. |
---|
60 | |
---|
61 | **Parameters** |
---|
62 | * metadata: an SQLAlchemy Metadata instance |
---|
63 | ''' |
---|
64 | party_indexes_table = Table('party_indexes', metadata, \ |
---|
65 | Column('id', Integer, ForeignKey('mo_responsiblepartyinfo.id'), \ |
---|
66 | primary_key=True, unique=True), |
---|
67 | Column('party_orders', String)) |
---|
68 | mapper(PartyIndexes, party_indexes_table) |
---|
69 | metadata.create_all() |
---|
70 | event.listen(MO_ResponsiblePartyInfo, 'load', __associateIndexes) |
---|
71 | |
---|
72 | def addPartyIndexes(responsiblePartyInfo, session): |
---|
73 | ''' |
---|
74 | Associates to a MO_ResponsiblePartyInfo entity a PartyIndexes entity |
---|
75 | |
---|
76 | **Parameters** |
---|
77 | * responsiblePartyInfo: the MO_ResponsiblePartyInfo instance |
---|
78 | * session: an SQLAlchemy session instance |
---|
79 | ''' |
---|
80 | pi = createAssociateIndexes(responsiblePartyInfo) |
---|
81 | session.add(pi) |
---|
82 | #session.flush() |
---|
83 | |
---|
84 | def updatePartyIndexes(responsiblePartyInfo, session): |
---|
85 | ''' |
---|
86 | Updates a PartyIndexes entity associated with a MO_ResponsiblePartyInfo entity |
---|
87 | |
---|
88 | **Parameters** |
---|
89 | * responsiblePartyInfo: the MO_ResponsiblePartyInfo instance |
---|
90 | * session: an SQLAlchemy session instance |
---|
91 | ''' |
---|
92 | partyIndex = session.query(PartyIndexes).get(responsiblePartyInfo.id) |
---|
93 | #uses this method just as utility |
---|
94 | pi = createAssociateIndexes(responsiblePartyInfo) |
---|
95 | partyIndex.party_orders = pi.party_orders |
---|
96 | session.expunge(pi) |
---|
97 | #session.flush() |
---|
98 | |
---|
99 | def deletePartyIndexes(responsiblePartyInfo, session): |
---|
100 | ''' |
---|
101 | Deletes a PartyIndexes entity associated with a MO_ResponsiblePartyInfo entity |
---|
102 | |
---|
103 | **Parameters** |
---|
104 | * responsiblePartyInfo: the MO_ResponsiblePartyInfo instance |
---|
105 | * session: an SQLAlchemy session instance |
---|
106 | ''' |
---|
107 | partyIndex = session.query(PartyIndexes).get(responsiblePartyInfo.id) |
---|
108 | session.delete(partyIndex) |
---|
109 | #session.flush() |
---|
110 | |
---|
111 | |
---|
112 | def __associateIndexes(target, content): |
---|
113 | """ |
---|
114 | Associates to a responsiblePartyInfo instance an "party_order" attribute. |
---|
115 | The method is associated when SQLAlchemy load a MO_ResponsiblePartyInfo instance |
---|
116 | |
---|
117 | **Parameters** |
---|
118 | * target: the MO_ResponsiblePartyInfo instance |
---|
119 | * content: an SQLAlchemy content instance |
---|
120 | """ |
---|
121 | partyIndex = EPB.search(PartyIndexes, target.id, content.session) |
---|
122 | _associateIndexes(target, partyIndex) |
---|
123 | |
---|
124 | def _associateIndexes(responsiblePartyInfo, partyIndex): |
---|
125 | """ |
---|
126 | Associates to a responsiblePartyInfo instance an "party_order" attribute. |
---|
127 | This method is separated from the associateIndexes method purely for unittest purpose. |
---|
128 | |
---|
129 | **Parameters** |
---|
130 | * responsiblePartyInfo: the MO_ResponsiblePartyInfo instance |
---|
131 | * partyIndex: a string formatted like '1:1, 2:3, 3:2' where the pair #:# represents |
---|
132 | the partyID:orderIndex |
---|
133 | """ |
---|
134 | if partyIndex is None: |
---|
135 | return |
---|
136 | pi = dict((x.split(':')[0], x.split(':')[1]) for x in partyIndex.party_orders.split(',')) |
---|
137 | for party in responsiblePartyInfo.party: |
---|
138 | if pi.has_key(str(party.id)): |
---|
139 | party.party_order = int(pi[str(party.id)]) |
---|
140 | |
---|
141 | |
---|
142 | def createAssociateIndexes(responsiblePartyInfo): |
---|
143 | """ |
---|
144 | Creates from MO_ResponsiblePartyInfo instance a PartyIndexes instance. |
---|
145 | The PartyIndexes instance reads the MO_ResponsiblePartyInfo.party instances |
---|
146 | and for each of them try to read their `party_order` attribute. |
---|
147 | If `party_order` does not exists the methods assign automatically an incremental |
---|
148 | index for each MO_Party |
---|
149 | |
---|
150 | **Parameters** |
---|
151 | * responsiblePartyInfo: the MO_ResponsiblePartyInfo instance |
---|
152 | |
---|
153 | **Return** |
---|
154 | * a PartyIndexes instance |
---|
155 | """ |
---|
156 | pi = PartyIndexes() |
---|
157 | pi.id = getattr(responsiblePartyInfo, 'id', None) |
---|
158 | |
---|
159 | order = [] |
---|
160 | index = set([-1]) |
---|
161 | for party in responsiblePartyInfo.party: |
---|
162 | pindex = max(index) + 1 |
---|
163 | if hasattr(party, 'party_order'): |
---|
164 | pindex = getattr(party, 'party_order') |
---|
165 | order.append('%s:%s,' % (party.id, pindex)) |
---|
166 | index.add(pindex) |
---|
167 | party_orders = ''.join(order)[:-1] |
---|
168 | pi.party_orders = party_orders |
---|
169 | return pi |
---|