/ui/auto/libperl-OBM/OBM/EntitiesFactory/contactFactory.pm

https://github.com/goldoraf/OBM · Perl · 293 lines · 210 code · 82 blank · 1 comment · 27 complexity · b17e0584fae8aeb21253c163eb2642bb MD5 · raw file

  1. package OBM::EntitiesFactory::contactFactory;
  2. $VERSION = '1.0';
  3. use OBM::EntitiesFactory::factory;
  4. use OBM::Log::log;
  5. @ISA = ('OBM::EntitiesFactory::factory', 'OBM::Log::log');
  6. $debug = 1;
  7. use 5.006_001;
  8. require Exporter;
  9. use strict;
  10. use OBM::Parameters::regexp;
  11. sub new {
  12. my $class = shift;
  13. my( $updateType, $parentDomain, $ids ) = @_;
  14. my $self = bless { }, $class;
  15. $self->{'updateType'} = $updateType;
  16. if( !$self->_checkUpdateType() ) {
  17. return undef;
  18. }
  19. if( !defined($parentDomain) ) {
  20. $self->_log( 'description du domaine père indéfini', 1 );
  21. return undef;
  22. }
  23. if( ref($parentDomain) ne 'OBM::Entities::obmDomain' ) {
  24. $self->_log( 'description du domaine père incorrecte', 1 );
  25. return undef;
  26. }
  27. $self->{'parentDomain'} = $parentDomain;
  28. $self->{'domainId'} = $parentDomain->getId();
  29. if( ref($self->{'domainId'}) || ($self->{'domainId'} !~ /$regexp_id/) ) {
  30. $self->_log( 'identifiant de domaine \''.$self->{'domainId'}.'\' incorrect', 1 );
  31. return undef;
  32. }
  33. if( defined($ids) && (ref($ids) ne 'ARRAY') ) {
  34. $self->_log( 'liste d\'ID à traiter incorrecte', 1 );
  35. return undef;
  36. }
  37. if( $#{$ids} >= 0 ) {
  38. $self->{'ids'} = $ids;
  39. }
  40. $self->{'running'} = undef;
  41. $self->{'currentEntity'} = undef;
  42. $self->{'contactDescList'} = undef;
  43. return $self;
  44. }
  45. sub _start {
  46. my $self = shift;
  47. $self->_log( 'debut de traitement', 4 );
  48. if( $self->_loadContacts() ) {
  49. $self->_log( 'problème lors de l\'obtention de la description des utilisateurs du domaine d\'identifiant \''.$self->{'domainId'}.'\'', 1 );
  50. return 0;
  51. }
  52. $self->{'running'} = 1;
  53. return $self->{'running'};
  54. }
  55. sub next {
  56. my $self = shift;
  57. $self->_log( 'obtention de l\'entité suivante', 4 );
  58. if( !$self->isRunning() ) {
  59. if( !$self->_start() ) {
  60. $self->_reset();
  61. return undef;
  62. }
  63. }
  64. while( defined($self->{'entitiesDescList'}) && (my $userDesc = $self->{'entitiesDescList'}->fetchrow_hashref()) ) {
  65. require OBM::Entities::obmContact;
  66. if( !(my $current = OBM::Entities::obmContact->new( $self->{'parentDomain'}, $userDesc )) ) {
  67. next;
  68. }else {
  69. $self->{'currentEntity'} = $current;
  70. SWITCH: {
  71. if( $self->{'updateType'} =~ /^(UPDATE_ALL|UPDATE_ENTITY|UPDATE_LINKS)$/ ) {
  72. if( $self->_loadContactLinks() ) {
  73. $self->_log( 'probleme au chargement des liens de l\'entité '.$self->{'currentEntity'}->getDescription(), 1 );
  74. next;
  75. }
  76. $self->_log( 'mise à jour de l\'entité et des liens, '.$self->{'currentEntity'}->getDescription(), 3 );
  77. $self->{'currentEntity'}->setUpdateEntity();
  78. $self->{'currentEntity'}->setUpdateLinks();
  79. last SWITCH;
  80. }
  81. if( $self->{'updateType'} eq 'DELETE' ) {
  82. $self->_log( 'suppression de l\'entité, '.$self->{'currentEntity'}->getDescription(), 3 );
  83. $self->{'currentEntity'}->setDelete();
  84. last SWITCH;
  85. }
  86. $self->_log( 'type de mise à jour inconnu \''.$self->{'updateType'}.'\'', 0 );
  87. return undef;
  88. }
  89. return $self->{'currentEntity'};
  90. }
  91. }
  92. $self->{'currentEntity'} = undef;
  93. return undef;
  94. }
  95. sub _loadContacts {
  96. my $self = shift;
  97. $self->_log( 'chargement des contacts du domaine d\'identifiant \''.$self->{'domainId'}.'\'', 4 );
  98. require OBM::Tools::obmDbHandler;
  99. my $dbHandler = OBM::Tools::obmDbHandler->instance();
  100. if( !$dbHandler ) {
  101. $self->_log( 'connexion à la base de données impossible', 1 );
  102. return 1;
  103. }
  104. my $query = 'SELECT Contact.*,
  105. ContactEntity.contactentity_entity_id,
  106. Company.*
  107. FROM Contact
  108. INNER JOIN ContactEntity ON ContactEntity.contactentity_contact_id=Contact.contact_id
  109. LEFT JOIN Company ON Company.company_id=Contact.contact_company_id
  110. WHERE Contact.contact_domain_id='.$self->{'domainId'};
  111. if( $self->{'ids'} ) {
  112. $query .= ' AND Contact.contact_id IN ('.join( ', ', @{$self->{'ids'}}).')';
  113. }
  114. # $query .= ' ORDER BY Contact.contact_lastname';
  115. if( !defined($dbHandler->execQuery( $query, \$self->{'entitiesDescList'} )) ) {
  116. $self->_log( 'chargement des contacts depuis la BD impossible', 1 );
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. sub _loadContactLinks {
  122. my $self = shift;
  123. my $links;
  124. my $entityId = $self->{'currentEntity'}->getId();
  125. $links->{'phone'} = $self->_loadContactPhones();
  126. $links->{'address'} = $self->_loadContactAddresses();
  127. $links->{'email'} = $self->_loadContactEmail();
  128. $links->{'website'} = $self->_loadContactWebsite();
  129. $self->{'currentEntity'}->setLinks( $links );
  130. return 0;
  131. }
  132. sub _loadContactPhones {
  133. my $self = shift;
  134. my $entityId = $self->{'currentEntity'}->getId();
  135. my $query = 'SELECT Phone.*
  136. FROM Phone
  137. INNER JOIN ContactEntity ON ContactEntity.contactentity_contact_id='.$entityId.'
  138. WHERE Phone.phone_entity_id=ContactEntity.contactentity_entity_id';
  139. require OBM::Tools::obmDbHandler;
  140. my $dbHandler = OBM::Tools::obmDbHandler->instance();
  141. if( !$dbHandler ) {
  142. $self->_log( 'connexion à la base de données impossible', 1 );
  143. return undef;
  144. }
  145. my $phonesList;
  146. if( !defined($dbHandler->execQuery( $query, \$phonesList ) ) ) {
  147. $self->_log( 'chargement des liens de '.$self->{'currentEntity'}->getDescription().' depuis la BD impossible', 1 );
  148. return undef;
  149. }
  150. return $phonesList->fetchall_arrayref({});
  151. }
  152. sub _loadContactAddresses {
  153. my $self = shift;
  154. my $entityId = $self->{'currentEntity'}->getId();
  155. my $query = 'SELECT Address.*
  156. FROM Address
  157. INNER JOIN ContactEntity ON ContactEntity.contactentity_contact_id='.$entityId.'
  158. WHERE Address.address_entity_id=ContactEntity.contactentity_entity_id';
  159. require OBM::Tools::obmDbHandler;
  160. my $dbHandler = OBM::Tools::obmDbHandler->instance();
  161. if( !$dbHandler ) {
  162. $self->_log( 'connexion à la base de données impossible', 1 );
  163. return undef;
  164. }
  165. my $addressesList;
  166. if( !defined($dbHandler->execQuery( $query, \$addressesList ) ) ) {
  167. $self->_log( 'chargement des liens de '.$self->{'currentEntity'}->getDescription().' depuis la BD impossible', 1 );
  168. return undef;
  169. }
  170. return $addressesList->fetchall_arrayref({});
  171. }
  172. sub _loadContactEmail {
  173. my $self = shift;
  174. my $entityId = $self->{'currentEntity'}->getId();
  175. my $query = 'SELECT Email.*
  176. FROM Email
  177. INNER JOIN ContactEntity ON ContactEntity.contactentity_contact_id='.$entityId.'
  178. WHERE Email.email_entity_id=ContactEntity.contactentity_entity_id';
  179. require OBM::Tools::obmDbHandler;
  180. my $dbHandler = OBM::Tools::obmDbHandler->instance();
  181. if( !$dbHandler ) {
  182. $self->_log( 'connexion à la base de données impossible', 1 );
  183. return undef;
  184. }
  185. my $addressesList;
  186. if( !defined($dbHandler->execQuery( $query, \$addressesList ) ) ) {
  187. $self->_log( 'chargement des liens de '.$self->{'currentEntity'}->getDescription().' depuis la BD impossible', 1 );
  188. return undef;
  189. }
  190. return $addressesList->fetchall_arrayref({});
  191. }
  192. sub _loadContactWebsite {
  193. my $self = shift;
  194. my $entityId = $self->{'currentEntity'}->getId();
  195. my $query = 'SELECT Website.*
  196. FROM Website
  197. INNER JOIN ContactEntity ON ContactEntity.contactentity_contact_id='.$entityId.'
  198. WHERE Website.website_entity_id=ContactEntity.contactentity_entity_id';
  199. require OBM::Tools::obmDbHandler;
  200. my $dbHandler = OBM::Tools::obmDbHandler->instance();
  201. if( !$dbHandler ) {
  202. $self->_log( 'connexion à la base de données impossible', 1 );
  203. return undef;
  204. }
  205. my $websitesList;
  206. if( !defined($dbHandler->execQuery( $query, \$websitesList ) ) ) {
  207. $self->_log( 'chargement des liens de '.$self->{'currentEntity'}->getDescription().' depuis la BD impossible', 1 );
  208. return undef;
  209. }
  210. return $websitesList->fetchall_arrayref({});
  211. }