Component.pm 18.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Tiago R. Mello <timello@async.com.br>
16
#                 Frédéric Buclin <LpSolit@gmail.com>
17 18
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
#                 Akamai Technologies <bugzilla-dev@akamai.com>
19 20 21 22 23

use strict;

package Bugzilla::Component;

24 25
use base qw(Bugzilla::Object);

26
use Bugzilla::Constants;
27 28
use Bugzilla::Util;
use Bugzilla::Error;
29
use Bugzilla::User;
30
use Bugzilla::FlagType;
31
use Bugzilla::Series;
32 33 34 35 36

###############################
####    Initialization     ####
###############################

37 38
use constant DB_TABLE => 'components';

39
use constant DB_COLUMNS => qw(
40 41 42 43 44 45
    id
    name
    product_id
    initialowner
    initialqacontact
    description
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
use constant REQUIRED_CREATE_FIELDS => qw(
    name
    product
    initialowner
    description
);

use constant UPDATE_COLUMNS => qw(
    name
    initialowner
    initialqacontact
    description
);

use constant VALIDATORS => {
    product          => \&_check_product,
    initialowner     => \&_check_initialowner,
    initialqacontact => \&_check_initialqacontact,
    description      => \&_check_description,
    initial_cc       => \&_check_cc_list,
};

use constant UPDATE_VALIDATORS => {
    name => \&_check_name,
};

74 75 76
###############################

sub new {
77 78
    my $class = shift;
    my $param = shift;
79 80
    my $dbh = Bugzilla->dbh;

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    my $product;
    if (ref $param) {
        $product = $param->{product};
        my $name = $param->{name};
        if (!defined $product) {
            ThrowCodeError('bad_arg',
                {argument => 'product',
                 function => "${class}::new"});
        }
        if (!defined $name) {
            ThrowCodeError('bad_arg',
                {argument => 'name',
                 function => "${class}::new"});
        }

        my $condition = 'product_id = ? AND name = ?';
        my @values = ($product->id, $name);
        $param = { condition => $condition, values => \@values };
99 100
    }

101 102
    unshift @_, $param;
    my $component = $class->SUPER::new(@_);
103 104
    # Add the product object as attribute only if the component exists.
    $component->{product} = $product if ($component && $product);
105
    return $component;
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
sub create {
    my $class = shift;
    my $dbh = Bugzilla->dbh;

    $dbh->bz_start_transaction();

    $class->check_required_create_fields(@_);
    my $params = $class->run_create_validators(@_);
    my $cc_list = delete $params->{initial_cc};

    my $component = $class->insert_create_data($params);

    # We still have to fill the component_cc table.
    $component->_update_cc_list($cc_list);

    # Create series for the new component.
    $component->_create_series();

    $dbh->bz_commit_transaction();
    return $component;
}

sub run_create_validators {
    my $class  = shift;
    my $params = $class->SUPER::run_create_validators(@_);

    my $product = delete $params->{product};
    $params->{product_id} = $product->id;
    $params->{name} = $class->_check_name($params->{name}, $product);

    return $params;
}

sub update {
    my $self = shift;
    my $changes = $self->SUPER::update(@_);

    # Update the component_cc table if necessary.
    if (defined $self->{cc_ids}) {
        my $diff = $self->_update_cc_list($self->{cc_ids});
        $changes->{cc_list} = $diff if defined $diff;
    }
    return $changes;
}

sub remove_from_db {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    $dbh->bz_start_transaction();

    if ($self->bug_count) {
        if (Bugzilla->params->{'allowbugdeletion'}) {
            require Bugzilla::Bug;
            foreach my $bug_id (@{$self->bug_ids}) {
                # Note: We allow admins to delete bugs even if they can't
                # see them, as long as they can see the product.
                my $bug = new Bugzilla::Bug($bug_id);
                $bug->remove_from_db();
            }
        } else {
            ThrowUserError('component_has_bugs', {nb => $self->bug_count});
        }
    }

    $dbh->do('DELETE FROM flaginclusions WHERE component_id = ?',
             undef, $self->id);
    $dbh->do('DELETE FROM flagexclusions WHERE component_id = ?',
             undef, $self->id);
    $dbh->do('DELETE FROM component_cc WHERE component_id = ?',
             undef, $self->id);
    $dbh->do('DELETE FROM components WHERE id = ?', undef, $self->id);

    $dbh->bz_commit_transaction();
}

################################
# Validators
################################

sub _check_name {
    my ($invocant, $name, $product) = @_;

    $name = trim($name);
    $name || ThrowUserError('component_blank_name');

    if (length($name) > MAX_COMPONENT_SIZE) {
        ThrowUserError('component_name_too_long', {'name' => $name});
    }

    $product = $invocant->product if (ref $invocant);
    my $component = new Bugzilla::Component({product => $product, name => $name});
    if ($component && (!ref $invocant || $component->id != $invocant->id)) {
        ThrowUserError('component_already_exists', { name    => $component->name,
                                                     product => $product });
    }
    return $name;
}

sub _check_description {
    my ($invocant, $description) = @_;

    $description = trim($description);
    $description || ThrowUserError('component_blank_description');
    return $description;
}

sub _check_initialowner {
    my ($invocant, $owner) = @_;

    $owner || ThrowUserError('component_need_initialowner');
    my $owner_id = Bugzilla::User->check($owner)->id;
    return $owner_id;
}

sub _check_initialqacontact {
    my ($invocant, $qa_contact) = @_;

    my $qa_contact_id;
    if (Bugzilla->params->{'useqacontact'}) {
        $qa_contact_id = Bugzilla::User->check($qa_contact)->id if $qa_contact;
    }
    elsif (ref $invocant) {
        $qa_contact_id = $invocant->{initialqacontact};
    }
    return $qa_contact_id;
}

sub _check_product {
    my ($invocant, $product) = @_;
    return Bugzilla->user->check_can_admin_product($product->name);
}

sub _check_cc_list {
    my ($invocant, $cc_list) = @_;

    my %cc_ids;
    foreach my $cc (@$cc_list) {
        my $id = login_to_id($cc, THROW_ERROR);
        $cc_ids{$id} = 1;
    }
    return [keys %cc_ids];
}

###############################
####       Methods         ####
###############################

sub _update_cc_list {
    my ($self, $cc_list) = @_;
    my $dbh = Bugzilla->dbh;

    my $old_cc_list =
      $dbh->selectcol_arrayref('SELECT user_id FROM component_cc
                                WHERE component_id = ?', undef, $self->id);

    my ($removed, $added) = diff_arrays($old_cc_list, $cc_list);
    my $diff;
    if (scalar @$removed || scalar @$added) {
        $diff = [join(', ', @$removed), join(', ', @$added)];
    }

    $dbh->do('DELETE FROM component_cc WHERE component_id = ?', undef, $self->id);

    my $sth = $dbh->prepare('INSERT INTO component_cc
                             (user_id, component_id) VALUES (?, ?)');
    $sth->execute($_, $self->id) foreach (@$cc_list);

    return $diff;
}

sub _create_series {
    my $self = shift;

    # Insert default charting queries for this product.
    # If they aren't using charting, this won't do any harm.
    my $prodcomp = "&product="   . url_quote($self->product->name) .
                   "&component=" . url_quote($self->name);

    my $open_query = 'field0-0-0=resolution&type0-0-0=notregexp&value0-0-0=.' .
                     $prodcomp;
    my $nonopen_query = 'field0-0-0=resolution&type0-0-0=regexp&value0-0-0=.' .
                        $prodcomp;

    my @series = ([get_text('series_all_open'), $open_query],
                  [get_text('series_all_closed'), $nonopen_query]);

    foreach my $sdata (@series) {
        my $series = new Bugzilla::Series(undef, $self->product->name,
                                          $self->name, $sdata->[0],
                                          Bugzilla->user->id, 1, $sdata->[1], 1);
        $series->writeToDatabase();
    }
}

sub set_name { $_[0]->set('name', $_[1]); }
sub set_description { $_[0]->set('description', $_[1]); }
sub set_default_assignee {
    my ($self, $owner) = @_;

    $self->set('initialowner', $owner);
    # Reset the default owner object.
    delete $self->{default_assignee};
}
sub set_default_qa_contact {
    my ($self, $qa_contact) = @_;

    $self->set('initialqacontact', $qa_contact);
    # Reset the default QA contact object.
    delete $self->{default_qa_contact};
}
sub set_cc_list {
    my ($self, $cc_list) = @_;

    $self->{cc_ids} = $self->_check_cc_list($cc_list);
    # Reset the list of CC user objects.
    delete $self->{initial_cc};
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
sub bug_count {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    if (!defined $self->{'bug_count'}) {
        $self->{'bug_count'} = $dbh->selectrow_array(q{
            SELECT COUNT(*) FROM bugs
            WHERE component_id = ?}, undef, $self->id) || 0;
    }
    return $self->{'bug_count'};
}

sub bug_ids {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    if (!defined $self->{'bugs_ids'}) {
        $self->{'bugs_ids'} = $dbh->selectcol_arrayref(q{
            SELECT bug_id FROM bugs
            WHERE component_id = ?}, undef, $self->id);
    }
    return $self->{'bugs_ids'};
}

sub default_assignee {
    my $self = shift;

    if (!defined $self->{'default_assignee'}) {
        $self->{'default_assignee'} =
            new Bugzilla::User($self->{'initialowner'});
    }
    return $self->{'default_assignee'};
}

sub default_qa_contact {
    my $self = shift;

    if (!defined $self->{'default_qa_contact'}) {
        $self->{'default_qa_contact'} =
            new Bugzilla::User($self->{'initialqacontact'});
    }
    return $self->{'default_qa_contact'};
}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
sub flag_types {
    my $self = shift;

    if (!defined $self->{'flag_types'}) {
        $self->{'flag_types'} = {};
        $self->{'flag_types'}->{'bug'} =
          Bugzilla::FlagType::match({ 'target_type'  => 'bug',
                                      'product_id'   => $self->product_id,
                                      'component_id' => $self->id });

        $self->{'flag_types'}->{'attachment'} =
          Bugzilla::FlagType::match({ 'target_type'  => 'attachment',
                                      'product_id'   => $self->product_id,
                                      'component_id' => $self->id });
    }
    return $self->{'flag_types'};
}

389 390 391 392 393
sub initial_cc {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    if (!defined $self->{'initial_cc'}) {
394 395 396 397 398 399 400 401
        # If set_cc_list() has been called but data are not yet written
        # into the DB, we want the new values defined by it.
        my $cc_ids = $self->{cc_ids}
                     || $dbh->selectcol_arrayref('SELECT user_id FROM component_cc
                                                  WHERE component_id = ?',
                                                  undef, $self->id);

        $self->{'initial_cc'} = Bugzilla::User->new_from_list($cc_ids);
402 403 404 405
    }
    return $self->{'initial_cc'};
}

406 407 408 409 410 411 412 413 414
sub product {
    my $self = shift;
    if (!defined $self->{'product'}) {
        require Bugzilla::Product; # We cannot |use| it.
        $self->{'product'} = new Bugzilla::Product($self->product_id);
    }
    return $self->{'product'};
}

415 416 417 418
###############################
####      Accessors        ####
###############################

419 420 421 422
sub id          { return $_[0]->{'id'};          }
sub name        { return $_[0]->{'name'};        }
sub description { return $_[0]->{'description'}; }
sub product_id  { return $_[0]->{'product_id'};  }
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

###############################
####      Subroutines      ####
###############################

1;

__END__

=head1 NAME

Bugzilla::Component - Bugzilla product component class.

=head1 SYNOPSIS

    use Bugzilla::Component;

440 441
    my $component = new Bugzilla::Component($comp_id);
    my $component = new Bugzilla::Component({ product => $product, name => $name });
442

443 444
    my $bug_count          = $component->bug_count();
    my $bug_ids            = $component->bug_ids();
445 446 447 448 449 450
    my $id                 = $component->id;
    my $name               = $component->name;
    my $description        = $component->description;
    my $product_id         = $component->product_id;
    my $default_assignee   = $component->default_assignee;
    my $default_qa_contact = $component->default_qa_contact;
451 452
    my $initial_cc         = $component->initial_cc;
    my $product            = $component->product;
453 454
    my $bug_flag_types     = $component->flag_types->{'bug'};
    my $attach_flag_types  = $component->flag_types->{'attachment'};
455

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    my $component = Bugzilla::Component->check({ product => $product, name => $name });

    my $component =
      Bugzilla::Component->create({ name             => $name,
                                    product          => $product,
                                    initialowner     => $user_login1,
                                    initialqacontact => $user_login2,
                                    description      => $description});

    $component->set_name($new_name);
    $component->set_description($new_description);
    $component->set_default_assignee($new_login_name);
    $component->set_default_qa_contact($new_login_name);
    $component->set_cc_list(\@new_login_names);
    $component->update();

    $component->remove_from_db;
473 474 475 476 477 478 479 480 481 482 483 484

=head1 DESCRIPTION

Component.pm represents a Product Component object.

=head1 METHODS

=over

=item C<new($param)>

 Description: The constructor is used to load an existing component
485 486
              by passing a component ID or a hash with the product
              object the component belongs to and the component name.
487 488

 Params:      $param - If you pass an integer, the integer is the
489 490 491 492 493
                       component ID from the database that we want to
                       read in. If you pass in a hash with the 'name'
                       and 'product' keys, then the value of the name
                       key is the name of a component being in the given
                       product.
494 495 496

 Returns:     A Bugzilla::Component object.

497 498 499 500 501 502 503 504 505 506 507
=item C<bug_count()>

 Description: Returns the total of bugs that belong to the component.

 Params:      none.

 Returns:     Integer with the number of bugs.

=item C<bugs_ids()>

 Description: Returns all bug IDs that belong to the component.
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
 Params:      none.

 Returns:     A reference to an array of bug IDs.

=item C<default_assignee()>

 Description: Returns a user object that represents the default assignee for
              the component.

 Params:      none.

 Returns:     A Bugzilla::User object.

=item C<default_qa_contact()>

 Description: Returns a user object that represents the default QA contact for
              the component.

 Params:      none.

 Returns:     A Bugzilla::User object.

531 532
=item C<initial_cc>

533 534 535 536 537 538
 Description: Returns a list of user objects representing users being
              in the initial CC list.

 Params:      none.

 Returns:     An arrayref of L<Bugzilla::User> objects.
539

540 541
=item C<flag_types()>

542 543
 Description: Returns all bug and attachment flagtypes available for
              the component.
544

545
 Params:      none.
546

547
 Returns:     Two references to an array of flagtype objects.
548

549 550
=item C<product()>

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
 Description: Returns the product the component belongs to.

 Params:      none.

 Returns:     A Bugzilla::Product object.

=item C<set_name($new_name)>

 Description: Changes the name of the component.

 Params:      $new_name - new name of the component (string). This name
                          must be unique within the product.

 Returns:     Nothing.

=item C<set_description($new_desc)>

 Description: Changes the description of the component.

 Params:      $new_desc - new description of the component (string).

 Returns:     Nothing.

=item C<set_default_assignee($new_assignee)>

 Description: Changes the default assignee of the component.

 Params:      $new_owner - login name of the new default assignee of
                           the component (string). This user account
                           must already exist.
581

582
 Returns:     Nothing.
583

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
=item C<set_default_qa_contact($new_qa_contact)>

 Description: Changes the default QA contact of the component.

 Params:      $new_qa_contact - login name of the new QA contact of
                                the component (string). This user
                                account must already exist.

 Returns:     Nothing.

=item C<set_cc_list(\@cc_list)>

 Description: Changes the list of users being in the CC list by default.

 Params:      \@cc_list - list of login names (string). All the user
                          accounts must already exist.

 Returns:     Nothing.

=item C<update()>

 Description: Write changes made to the component into the DB.

 Params:      none.

 Returns:     A hashref with changes made to the component object.

=item C<remove_from_db()>

 Description: Deletes the current component from the DB. The object itself
              is not destroyed.

 Params:      none.

 Returns:     Nothing.
619

620 621
=back

622
=head1 CLASS METHODS
623 624 625

=over

626
=item C<create(\%params)>
627

628
 Description: Create a new component for the given product.
629

630 631 632 633 634 635 636 637 638 639 640 641
 Params:      The hashref must have the following keys:
              name            - name of the new component (string). This name
                                must be unique within the product.
              product         - a Bugzilla::Product object to which
                                the Component is being added.
              description     - description of the new component (string).
              initialowner    - login name of the default assignee (string).
              The following keys are optional:
              initiaqacontact - login name of the default QA contact (string),
                                or an empty string to clear it.
              initial_cc      - an arrayref of login names to add to the
                                CC list by default.
642

643
 Returns:     A Bugzilla::Component object.
644

645 646 647
=back

=cut