File Coverage

blib/lib/Dicom/UID/Generator.pm
Criterion Covered Total %
statement 27 55 49.0
branch 0 4 0.0
condition n/a
subroutine 7 13 53.8
pod 5 5 100.0
total 39 77 50.6


line stmt bran cond sub pod time code
1             package Dicom::UID::Generator;
2              
3 3     3   183007 use strict;
  3         6  
  3         123  
4 3     3   16 use warnings;
  3         7  
  3         272  
5              
6 3     3   5206 use Class::Utils qw(set_params);
  3         52974  
  3         100  
7 3     3   2800 use DateTime::HiRes;
  3         1894932  
  3         116  
8 3     3   54 use English;
  3         6  
  3         21  
9 3     3   1432 use Readonly;
  3         9  
  3         2456  
10              
11             our $VERSION = 0.02;
12              
13             # Constants.
14             Readonly::Scalar our $EMPTY_STR => q{};
15              
16             # Constructor.
17             sub new {
18 1     1 1 460563 my ($class, @params) = @_;
19              
20             # Create object.
21 1         3 my $self = bless {}, $class;
22              
23             # Library number.
24 1         6 $self->{'library_number'} = undef;
25              
26             # Model number.
27 1         3 $self->{'model_number'} = undef;
28              
29             # Serial number.
30 1         3 $self->{'serial_number'} = undef;
31              
32             # TimeZone.
33 1         4 $self->{'timezone'} = 'Europe/Prague';
34              
35             # UID counter.
36 1         3 $self->{'uid_counter'} = 0;
37              
38             # Process parameters.
39 1         7 set_params($self, @params);
40              
41 1         9 return $self;
42             }
43              
44             # Create series instance UID.
45             sub create_series_instance_uid {
46 0     0 1   my $self = shift;
47 0           return $self->create_uid($self->_root_uid.'.1.3');
48             }
49              
50             # Create SOP instance UID.
51             sub create_sop_instance_uid {
52 0     0 1   my $self = shift;
53 0           return $self->create_uid($self->_root_uid.'.1.4');
54             }
55              
56             # Create study instance UID.
57             sub create_study_instance_uid {
58 0     0 1   my $self = shift;
59 0           return $self->create_uid($self->_root_uid.'.1.2');
60             }
61              
62             # Create UID.
63             sub create_uid {
64 0     0 1   my ($self, $prefix) = @_;
65 0           my $uid = $prefix;
66 0           $uid .= '.'.$PID;
67 0           $uid .= '.'.DateTime::HiRes->now->set_time_zone($self->{'timezone'})
68             ->strftime('%Y%m%d%H%M%S%3N');
69 0           $self->{'uid_counter'}++;
70 0           $uid .= '.'.$self->{'uid_counter'};
71 0           return $uid;
72             }
73              
74             # Add part of UID.
75             sub _add_part {
76 0     0     my ($self, $uid_part_sr, $part) = @_;
77 0 0         if (defined $self->{$part}) {
78 0 0         if (${$uid_part_sr} ne $EMPTY_STR) {
  0            
79 0           ${$uid_part_sr} .= '.';
  0            
80             }
81 0           ${$uid_part_sr} .= $self->{$part};
  0            
82             }
83 0           return;
84             }
85              
86             # Get root UID.
87             sub _root_uid {
88 0     0     my $self = shift;
89 0           my $uid_part = $EMPTY_STR;
90 0           $self->_add_part(\$uid_part, 'library_number');
91 0           $self->_add_part(\$uid_part, 'model_number');
92 0           $self->_add_part(\$uid_part, 'serial_number');
93 0           return $uid_part;
94             }
95              
96             1;
97              
98             __END__
99              
100             =pod
101              
102             =encoding utf8
103              
104             =head1 NAME
105              
106             Dicom::UID::Generator - DICOM UID generator.
107              
108             =head1 SYNOPSIS
109              
110             use Dicom::UID::Generator;
111              
112             my $obj = Dicom::UID::Generator->new(%params);
113             my $uid = $obj->create_series_instance_uid;
114             my $uid = $obj->create_sop_instance_uid;
115             my $uid = $obj->create_study_instance_uid;
116             my $uid = $obj->create_uid($prefix);
117              
118             =head1 METHODS
119              
120             =over 8
121              
122             =item C<new(%params)>
123              
124             Constructor.
125              
126             =over 8
127              
128             =item * C<library_number>
129              
130             DICOM library number.
131             Default value is undef.
132              
133             =item * C<model_number>
134              
135             Device model number.
136             Default value is undef.
137              
138             =item * C<serial_number>
139              
140             Device serial number.
141             Default value is undef.
142              
143             =item * C<timezone>
144              
145             Time zone for time in UID..
146             Default value is 'Europe/Prague'.
147              
148             =item * C<uid_counter>
149              
150             UID counter number for part of final UID.
151             Default value is 0.
152              
153             =back
154              
155             =item C<create_series_instance_uid()>
156              
157             Get DICOM Series Instance UID.
158             Returns string.
159              
160             =item C<create_sop_instance_uid()>
161              
162             Get DICOM SOP Instance UID.
163             Returns string.
164              
165             =item C<create_study_instance_uid()>
166              
167             Get DICOM Study Instance UID.
168             Returns string.
169              
170             =item C<create_uid($prefix)>
171              
172             Get DICOM UID defined by prefix.
173             Returns string.
174              
175             =back
176              
177             =head1 ERRORS
178              
179             new():
180             From Class::Utils::set_params():
181             Unknown parameter '%s'.
182              
183             =head1 EXAMPLE
184              
185             =for comment filename=gen_uid_numbers.pl
186              
187             use strict;
188             use warnings;
189              
190             use Dicom::UID::Generator;
191              
192             # Object.
193             my $obj = Dicom::UID::Generator->new(
194             'library_number' => 999,
195             'model_number' => '001',
196             'serial_number' => 123,
197             );
198              
199             # Get Series Instance UID.
200             my $series_instance_uid = $obj->create_series_instance_uid;
201              
202             # Get Study Instance UID.
203             my $study_instance_uid = $obj->create_study_instance_uid;
204              
205             # Get SOP Instance UID.
206             my $sop_instance_uid = $obj->create_sop_instance_uid;
207              
208             # Print out.
209             print "Study Instance UID: $study_instance_uid\n";
210             print "Series Instance UID: $series_instance_uid\n";
211             print "SOP Instance UID: $sop_instance_uid\n";
212              
213             # Output like:
214             # Study Instance UID: 999.001.123.1.2.976.20160825112022726.2
215             # Series Instance UID: 999.001.123.1.3.976.20160825112022647.1
216             # SOP Instance UID: 999.001.123.1.4.976.20160825112022727.3
217              
218             # Comments:
219             # 999 is DICOM library number.
220             # 001 is device model number.
221             # 123 is device serial number.
222             # 1.2, 1.3, 1.4 are hardcoded resolutions of DICOM UID type.
223             # 976 is PID of process.
224             # 20160825112022726 is timestamp.
225             # last number is number of 'uid_counter' parameter.
226              
227             =head1 DEPENDENCIES
228              
229             L<Class::Utils>,
230             L<DateTime::HiRes>,
231             L<English>,
232             L<Readonly>,
233              
234             =head1 SEE ALSO
235              
236             =over
237              
238             =item L<Task::Dicom>
239              
240             Install the Dicom modules.
241              
242             =back
243              
244             =head1 REPOSITORY
245              
246             L<https://github.com/michal-josef-spacek/Dicom-UID-Generator>
247              
248             =head1 AUTHOR
249              
250             Michal Josef Špaček L<mailto:skim@cpan.org>
251              
252             L<http://skim.cz>
253              
254             =head1 LICENSE AND COPYRIGHT
255              
256             © Michal Josef Špaček 2016
257              
258             BSD 2-Clause License
259              
260             =head1 VERSION
261              
262             0.02
263              
264             =cut