Source code: org/jeteam/bean/common/DTOFactory.java
1 /*
2 * JETeam, Java Enterprise TeamWork
3 *
4 * Distributable under the GPL license.
5 * See terms of licence at http://www.gnu.org
6 *
7 * $Source: /cvsroot/jeteam/jeteam/jeteam-bcx/src/java/org/jeteam/bean/common/DTOFactory.java,v $
8 * $Date: 2003/05/29 08:49:01 $
9 * $Author: draftdog $
10 * $Revision: 1.8 $
11 */
12 package org.jeteam.bean.common;
13
14 import org.jeteam.bean.config.PriorityDTO;
15 import org.jeteam.bean.config.PriorityData;
16 import org.jeteam.bean.config.PriorityLocal;
17 import org.jeteam.bean.config.SettingDTO;
18 import org.jeteam.bean.config.SettingData;
19 import org.jeteam.bean.config.SettingLocal;
20 import org.jeteam.bean.config.StatusDTO;
21 import org.jeteam.bean.config.StatusData;
22 import org.jeteam.bean.config.StatusLocal;
23 import org.jeteam.bean.project.ProjectDTO;
24 import org.jeteam.bean.project.ProjectData;
25 import org.jeteam.bean.project.ProjectLocal;
26 import org.jeteam.bean.security.PrivilegeDTO;
27 import org.jeteam.bean.security.PrivilegeData;
28 import org.jeteam.bean.security.PrivilegeLocal;
29 import org.jeteam.bean.security.RoleDTO;
30 import org.jeteam.bean.security.RoleData;
31 import org.jeteam.bean.security.RoleLocal;
32 import org.jeteam.bean.task.NoteDTO;
33 import org.jeteam.bean.task.NoteData;
34 import org.jeteam.bean.task.NoteLocal;
35 import org.jeteam.bean.task.TaskDTO;
36 import org.jeteam.bean.task.TaskData;
37 import org.jeteam.bean.task.TaskLocal;
38 import org.jeteam.bean.user.UserDTO;
39 import org.jeteam.bean.user.UserData;
40 import org.jeteam.bean.user.UserLocal;
41
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Iterator;
45
46 /**
47 * The DTOFactory is a utility class that centralizes the code that converts a
48 * data transfer objects (DTOs) into value objects. Value objects are representations of
49 * the domain, similar but very much simpler than the actual entity beans they
50 * are associated with. DTOs on the other hand are the client's view of the
51 * beans.<p>
52 *
53 * For each bean we need an object that can transfer the bean attributes from
54 * the service to the entity, in the case of Container Manager Relations we do
55 * not include associated bean information in the DTO. <p>
56 *
57 * Since a service may need to work with several entity beans it is therefore
58 * necessary to centralize the code to create the DTOs, otherwise we risk having
59 * duplicate code spread over the services. This class is designed to do exactly
60 * that. <p>
61 *
62 * As an extra there are method provided that will convert a collection of DTO
63 * objects into a collection of DTOs. <p>
64 *
65 * For better performance there are also some methods that allow working with
66 * arrays rather than Collection instances, A Collection implies a cast for each
67 * element, this typically is slow and error-prone. Arrays are type-safe.
68 *
69 * The DTOFactory is instantiated using the <code>getInstance()</code> method,
70 * there is no public constructor available. <p>
71 *
72 * There is also a Factory that works in the other direction: DataFactory
73 *
74 * @author Wouter Zoons (<a href="mailto:draftdog@users.sourceforge.net">
75 * draftdog@users.sourceforge.net</a> )
76 * @see org.jeteam.bean.common.DataFactory
77 */
78 public class DTOFactory
79 {
80 private static DTOFactory factory = null;
81
82 /**
83 * The constructor is not publicly available as part of the Singleton design
84 * pattern, use <code>getInstance()</code> instead.
85 */
86 private DTOFactory()
87 {
88 }
89
90 /**
91 * This method is designed to always return the same DTOFactory instance, it
92 * is using a lazy creation strategy where the factory is created the first
93 * time the actual call to this method is made, al subsequent calls return
94 * the previously created DTOFactory instance.
95 *
96 * @return The instance value
97 */
98 public static synchronized DTOFactory getInstance()
99 {
100 if (factory == null)
101 {
102 factory = new DTOFactory();
103 }
104 return factory;
105 }
106
107 /**
108 * Returns a new Collection containing data transfer object instances representing
109 * the argument local bean collection.
110 *
111 * @param noteLocals A Collection containing only Local interface of the required bean.
112 * @return A new Collection containing only DTOs representing the
113 * argument Local interfaces.
114 */
115 public Collection createNoteDTOCollection(Collection noteLocals)
116 {
117 ArrayList list = new ArrayList(noteLocals.size());
118 Iterator iter = noteLocals.iterator();
119 while (iter.hasNext())
120 {
121 list.add(createDTO((NoteLocal) iter.next()));
122 }
123 return list;
124 }
125
126 /**
127 * Converts an array of NoteData instances into NoteDTO instances.
128 *
129 * @param datas The input NoteData instances
130 * @return The array of NoteDTO instances.
131 */
132 public NoteDTO[] createDTO(NoteData[] datas)
133 {
134 NoteDTO[] dtos = new NoteDTO[datas.length];
135 for (int i = 0; i < dtos.length; i++)
136 {
137 dtos[i] = createDTO(datas[i]);
138 }
139 return dtos;
140 }
141
142 /**
143 * Converts an array of NoteLocal instances into NoteDTO instances.
144 *
145 * @param locals The input NoteLocal instances
146 * @return The array of NoteDTO instances.
147 */
148 public NoteDTO[] createNoteDTOArray(Collection locals)
149 {
150 NoteDTO[] dtos = new NoteDTO[locals.size()];
151 Iterator iter = locals.iterator();
152 int i = 0;
153 while (iter.hasNext())
154 {
155 dtos[i++] = createDTO((NoteLocal) iter.next());
156 }
157 return dtos;
158 }
159
160 /**
161 * Returns a new Collection containing data transfer object instances representing
162 * the argument local bean collection.
163 *
164 * @param priorityLocals A Collection containing only Local interface of the required bean.
165 * @return A new Collection containing only DTOs representing
166 * the argument DTOs.
167 */
168 public Collection createPriorityDTOCollection(Collection priorityLocals)
169 {
170 ArrayList list = new ArrayList(priorityLocals.size());
171 Iterator iter = priorityLocals.iterator();
172 while (iter.hasNext())
173 {
174 list.add(createDTO((PriorityLocal) iter.next()));
175 }
176 return list;
177 }
178
179 /**
180 * Converts an array of PriorityData instances into PriorityDTO instances.
181 *
182 * @param datas The input PriorityData instances
183 * @return The array of PriorityDTO instances.
184 */
185 public PriorityDTO[] createDTO(PriorityData[] datas)
186 {
187 PriorityDTO[] dtos = new PriorityDTO[datas.length];
188 for (int i = 0; i < dtos.length; i++)
189 {
190 dtos[i] = createDTO(datas[i]);
191 }
192 return dtos;
193 }
194
195 /**
196 * Converts an array of PriorityLocal instances into PriorityDTO instances.
197 *
198 * @param locals The input PriorityLocal instances
199 * @return The array of PriorityDTO instances.
200 */
201 public PriorityDTO[] createPriorityDTOArray(Collection locals)
202 {
203 PriorityDTO[] dtos = new PriorityDTO[locals.size()];
204 Iterator iter = locals.iterator();
205 int i = 0;
206 while (iter.hasNext())
207 {
208 dtos[i++] = createDTO((PriorityLocal) iter.next());
209 }
210 return dtos;
211 }
212
213 /**
214 * Returns a new Collection containing data transfer object instances representing
215 * the argument local bean collection.
216 *
217 * @param privilegeLocals A Collection containing only Local interface of the required bean.
218 * @return A new Collection containing only DTOs representing
219 * the argument DTOs.
220 */
221 public Collection createPrivilegeDTOCollection(Collection privilegeLocals)
222 {
223 ArrayList list = new ArrayList(privilegeLocals.size());
224 Iterator iter = privilegeLocals.iterator();
225 while (iter.hasNext())
226 {
227 list.add(createDTO((PrivilegeLocal) iter.next()));
228 }
229 return list;
230 }
231
232 /**
233 * Converts an array of PrivilegeData instances into PrivilegeDTO instances.
234 *
235 * @param datas The input PrivilegeData instances
236 * @return The array of PrivilegeDTO instances.
237 */
238 public PrivilegeDTO[] createDTO(PrivilegeData[] datas)
239 {
240 PrivilegeDTO[] dtos = new PrivilegeDTO[datas.length];
241 for (int i = 0; i < dtos.length; i++)
242 {
243 dtos[i] = createDTO(datas[i]);
244 }
245 return dtos;
246 }
247
248 /**
249 * Converts an array of PrivilegeLocal instances into PrivilegeDTO instances.
250 *
251 * @param locals The input PrivilegeLocal instances
252 * @return The array of PrivilegeDTO instances.
253 */
254 public PrivilegeDTO[] createPrivilegeDTOArray(Collection locals)
255 {
256 PrivilegeDTO[] dtos = new PrivilegeDTO[locals.size()];
257 Iterator iter = locals.iterator();
258 int i = 0;
259 while (iter.hasNext())
260 {
261 dtos[i++] = createDTO((PrivilegeLocal) iter.next());
262 }
263 return dtos;
264 }
265
266 /**
267 * Returns a new Collection containing data transfer object instances representing
268 * the argument local bean collection.
269 *
270 * @param projectLocals A Collection containing only Local interface of the required bean.
271 * @return A new Collection containing only DTOs representing
272 * the argument local beans.
273 */
274 public Collection createProjectDTOCollection(Collection projectLocals)
275 {
276 ArrayList list = new ArrayList(projectLocals.size());
277 Iterator iter = projectLocals.iterator();
278 while (iter.hasNext())
279 {
280 list.add(createDTO((ProjectLocal) iter.next()));
281 }
282 return list;
283 }
284
285 /**
286 * Converts an array of ProjectData instances into ProjectDTO instances.
287 *
288 * @param datas The input ProjectData instances
289 * @return The array of ProjectDTO instances.
290 */
291 public ProjectDTO[] createDTO(ProjectData[] datas)
292 {
293 ProjectDTO[] dtos = new ProjectDTO[datas.length];
294 for (int i = 0; i < dtos.length; i++)
295 {
296 dtos[i] = createDTO(datas[i]);
297 }
298 return dtos;
299 }
300
301 /**
302 * Converts an array of ProjectLocal instances into ProjectDTO instances.
303 *
304 * @param locals The input ProjectLocal instances
305 * @return The array of ProjectDTO instances.
306 */
307 public ProjectDTO[] createProjectDTOArray(Collection locals)
308 {
309 ProjectDTO[] dtos = new ProjectDTO[locals.size()];
310 Iterator iter = locals.iterator();
311 int i = 0;
312 while (iter.hasNext())
313 {
314 dtos[i++] = createDTO((ProjectLocal) iter.next());
315 }
316 return dtos;
317 }
318
319 /**
320 * Returns a new Collection containing data transfer object instances representing
321 * the argument local bean collection.
322 *
323 * @param roleLocals A Collection containing only Local interface of the required bean.
324 * @return A new Collection containing only DTOs representing the
325 * argument Local interfaces.
326 */
327 public Collection createRoleDTOCollection(Collection roleLocals)
328 {
329 ArrayList list = new ArrayList(roleLocals.size());
330 Iterator iter = roleLocals.iterator();
331 while (iter.hasNext())
332 {
333 list.add(createDTO((RoleLocal) iter.next()));
334 }
335 return list;
336 }
337
338 /**
339 * Converts an array of RoleData instances into RoleDTO instances.
340 *
341 * @param datas The input RoleData instances
342 * @return The array of RoleDTO instances.
343 */
344 public RoleDTO[] createDTO(RoleData[] datas)
345 {
346 RoleDTO[] dtos = new RoleDTO[datas.length];
347 for (int i = 0; i < dtos.length; i++)
348 {
349 dtos[i] = createDTO(datas[i]);
350 }
351 return dtos;
352 }
353
354 /**
355 * Converts an array of RoleLocal instances into RoleDTO instances.
356 *
357 * @param locals The input RoleLocal instances
358 * @return The array of RoleDTO instances.
359 */
360 public RoleDTO[] createRoleDTOArray(Collection locals)
361 {
362 RoleDTO[] dtos = new RoleDTO[locals.size()];
363 Iterator iter = locals.iterator();
364 int i = 0;
365 while (iter.hasNext())
366 {
367 dtos[i++] = createDTO((RoleLocal) iter.next());
368 }
369 return dtos;
370 }
371
372 /**
373 * Returns a new Collection containing data transfer object instances representing
374 * the argument local bean collection.
375 *
376 * @param settingLocals A Collection containing only Local interface of the required bean.
377 * @return A new Collection containing only DTOs representing
378 * the argument DTOs.
379 */
380 public Collection createSettingDTOCollection(Collection settingLocals)
381 {
382 ArrayList list = new ArrayList(settingLocals.size());
383 Iterator iter = settingLocals.iterator();
384 while (iter.hasNext())
385 {
386 list.add(createDTO((SettingLocal) iter.next()));
387 }
388 return list;
389 }
390
391 /**
392 * Converts an array of SettingData instances into SettingDTO instances.
393 *
394 * @param datas The input SettingData instances
395 * @return The array of SettingDTO instances.
396 */
397 public SettingDTO[] createDTO(SettingData[] datas)
398 {
399 SettingDTO[] dtos = new SettingDTO[datas.length];
400 for (int i = 0; i < dtos.length; i++)
401 {
402 dtos[i] = createDTO(datas[i]);
403 }
404 return dtos;
405 }
406
407 /**
408 * Converts an array of SettingLocal instances into SettingDTO instances.
409 *
410 * @param locals The input SettingLocal instances
411 * @return The array of SettingDTO instances.
412 */
413 public SettingDTO[] createSettingDTOArray(Collection locals)
414 {
415 SettingDTO[] dtos = new SettingDTO[locals.size()];
416 Iterator iter = locals.iterator();
417 int i = 0;
418 while (iter.hasNext())
419 {
420 dtos[i++] = createDTO((SettingLocal) iter.next());
421 }
422 return dtos;
423 }
424
425 /**
426 * Returns a new Collection containing data transfer object instances representing
427 * the argument local bean collection.
428 *
429 * @param statusLocals A Collection containing only Local interface of the required bean.
430 * @return A new Collection containing only DTOs representing the
431 * argument Local interfaces.
432 */
433 public Collection createStatusDTOCollection(Collection statusLocals)
434 {
435 ArrayList list = new ArrayList(statusLocals.size());
436 Iterator iter = statusLocals.iterator();
437 while (iter.hasNext())
438 {
439 list.add(createDTO((StatusLocal) iter.next()));
440 }
441 return list;
442 }
443
444 /**
445 * Converts an array of StatusData instances into StatusDTO instances.
446 *
447 * @param datas The input StatusData instances
448 * @return The array of StatusDTO instances.
449 */
450 public StatusDTO[] createDTO(StatusData[] datas)
451 {
452 StatusDTO[] dtos = new StatusDTO[datas.length];
453 for (int i = 0; i < dtos.length; i++)
454 {
455 dtos[i] = createDTO(datas[i]);
456 }
457 return dtos;
458 }
459
460 /**
461 * Converts an array of StatusLocal instances into StatusDTO instances.
462 *
463 * @param locals The input StatusLocal instances
464 * @return The array of StatusDTO instances.
465 */
466 public StatusDTO[] createStatusDTOArray(Collection locals)
467 {
468 StatusDTO[] dtos = new StatusDTO[locals.size()];
469 Iterator iter = locals.iterator();
470 int i = 0;
471 while (iter.hasNext())
472 {
473 dtos[i++] = createDTO((StatusLocal) iter.next());
474 }
475 return dtos;
476 }
477
478 /**
479 * Returns a new Collection containing data transfer object instances representing
480 * the argument local bean collection.
481 *
482 * @param taskLocals A Collection containing only Local interface of the required bean.
483 * @return A new Collection containing only DTOs representing the
484 * argument Local interfaces.
485 */
486 public Collection createTaskDTOCollection(Collection taskLocals)
487 {
488 ArrayList list = new ArrayList(taskLocals.size());
489 Iterator iter = taskLocals.iterator();
490 while (iter.hasNext())
491 {
492 list.add(createDTO((TaskLocal) iter.next()));
493 }
494 return list;
495 }
496
497 /**
498 * Converts an array of TaskData instances into TaskDTO instances.
499 *
500 * @param datas The input TaskData instances
501 * @return The array of TaskDTO instances.
502 */
503 public TaskDTO[] createDTO(TaskData[] datas)
504 {
505 TaskDTO[] dtos = new TaskDTO[datas.length];
506 for (int i = 0; i < dtos.length; i++)
507 {
508 dtos[i] = createDTO(datas[i]);
509 }
510 return dtos;
511 }
512
513 /**
514 * Converts an array of TaskLocal instances into TaskDTO instances.
515 *
516 * @param locals The input TaskLocal instances
517 * @return The array of TaskDTO instances.
518 */
519 public TaskDTO[] createTaskDTOArray(Collection locals)
520 {
521 TaskDTO[] dtos = new TaskDTO[locals.size()];
522 Iterator iter = locals.iterator();
523 int i = 0;
524 while (iter.hasNext())
525 {
526 dtos[i++] = createDTO((TaskLocal) iter.next());
527 }
528 return dtos;
529 }
530
531 /**
532 * Returns a new Collection containing data transfer object instances representing
533 * the argument local bean collection.
534 *
535 * @param userLocals A Collection containing only Local interface of the required bean.
536 * @return A new Collection containing only DTOs representing the
537 * argument Local interfaces.
538 */
539 public Collection createUserDTOCollection(Collection userLocals)
540 {
541 ArrayList list = new ArrayList(userLocals.size());
542 Iterator iter = userLocals.iterator();
543 while (iter.hasNext())
544 {
545 list.add(createDTO((UserLocal) iter.next()));
546 }
547 return list;
548 }
549
550 /**
551 * Converts an array of UserData instances into UserDTO instances.
552 *
553 * @param datas The input UserData instances
554 * @return The array of UserDTO instances.
555 */
556 public UserDTO[] createDTO(UserData[] datas)
557 {
558 UserDTO[] dtos = new UserDTO[datas.length];
559 for (int i = 0; i < dtos.length; i++)
560 {
561 dtos[i] = createDTO(datas[i]);
562 }
563 return dtos;
564 }
565
566 /**
567 * Converts an array of UserLocal instances into UserDTO instances.
568 *
569 * @param locals The input UserLocal instances
570 * @return The array of UserDTO instances.
571 */
572 public UserDTO[] createUserDTOArray(Collection locals)
573 {
574 UserDTO[] dtos = new UserDTO[locals.size()];
575 Iterator iter = locals.iterator();
576 int i = 0;
577 while (iter.hasNext())
578 {
579 dtos[i++] = createDTO((UserLocal) iter.next());
580 }
581 return dtos;
582 }
583
584 /**
585 * Creates a value object from the argument data transfer object.
586 *
587 * @param data The data transfer object to convert.
588 * @return The new value object.
589 */
590 public NoteDTO createDTO(NoteData data)
591 {
592 return new NoteDTO(
593 data.getId(), data.getSubject(), data.getMessage(), data.getCreated(),
594 data.getDeleted(), data.getLastUpdated());
595 }
596
597 /**
598 * Creates a value object from the argument local note.
599 *
600 * @param data The local interface to convert.
601 * @return The new value object.
602 */
603 public NoteDTO createDTO(NoteLocal data)
604 {
605 return createDTO(data.getNoteData());
606 }
607
608 /**
609 * Creates a value object from the argument data transfer object.
610 *
611 * @param data The data transfer object to convert.
612 * @return The new value object.
613 */
614 public TaskDTO createDTO(TaskData data)
615 {
616 return new TaskDTO(
617 data.getId(), data.getName(), data.getDescription(), data.getCreated(),
618 data.getDeleted(), data.getDue(), data.getPercentageCompleted(),
619 data.getLastUpdated());
620 }
621
622 /**
623 * Creates a value object from the argument local task.
624 *
625 * @param data The local interface to convert.
626 * @return The new value object.
627 */
628 public TaskDTO createDTO(TaskLocal data)
629 {
630 return createDTO(data.getTaskData());
631 }
632
633 /**
634 * Creates a value object from the argument data transfer object.
635 *
636 * @param data The data transfer object to convert.
637 * @return The new value object.
638 */
639 public PrivilegeDTO createDTO(PrivilegeData data)
640 {
641 return new PrivilegeDTO(data.getId(), data.getPermission(), data.getDescription());
642 }
643
644 /**
645 * Creates a value object from the argument local privilege.
646 *
647 * @param data The local interface to convert.
648 * @return The new value object.
649 */
650 public PrivilegeDTO createDTO(PrivilegeLocal data)
651 {
652 return createDTO(data.getPrivilegeData());
653 }
654
655 /**
656 * Creates a value object from the argument data transfer object.
657 *
658 * @param data The data transfer object to convert.
659 * @return The new value object.
660 */
661 public PriorityDTO createDTO(PriorityData data)
662 {
663 return new PriorityDTO(data.getId(), data.getName(), data.getDescription(), data.getLevel());
664 }
665
666 /**
667 * Creates a value object from the argument local priority.
668 *
669 * @param data The local interface to convert.
670 * @return The new value object.
671 */
672 public PriorityDTO createDTO(PriorityLocal data)
673 {
674 return createDTO(data.getPriorityData());
675 }
676
677 /**
678 * Creates a value object from the argument data transfer object.
679 *
680 * @param data The data transfer object to convert.
681 * @return The new value object.
682 */
683 public SettingDTO createDTO(SettingData data)
684 {
685 return new SettingDTO(data.getId(), data.getKey(), data.getValue());
686 }
687
688 /**
689 * Creates a value object from the argument local setting.
690 *
691 * @param data The local interface to convert.
692 * @return The new value object.
693 */
694 public SettingDTO createDTO(SettingLocal data)
695 {
696 return createDTO(data.getSettingData());
697 }
698
699 /**
700 * Creates a value object from the argument data transfer object.
701 *
702 * @param data The data transfer object to convert.
703 * @return The new value object.
704 */
705 public StatusDTO createDTO(StatusData data)
706 {
707 return new StatusDTO(data.getId(), data.getName(), data.getDescription());
708 }
709
710 /**
711 * Creates a value object from the argument local status.
712 *
713 * @param data The local interface to convert.
714 * @return The new value object.
715 */
716 public StatusDTO createDTO(StatusLocal data)
717 {
718 return createDTO(data.getStatusData());
719 }
720
721 /**
722 * Creates a value object from the argument data transfer object.
723 *
724 * @param data The data transfer object to convert.
725 * @return The new value object.
726 */
727 public RoleDTO createDTO(RoleData data)
728 {
729 return new RoleDTO(data.getId(), data.getName(), data.getDescription());
730 }
731
732 /**
733 * Creates a value object from the argument local role.
734 *
735 * @param data The local interface to convert.
736 * @return The new value object.
737 */
738 public RoleDTO createDTO(RoleLocal data)
739 {
740 return createDTO(data.getRoleData());
741 }
742
743 /**
744 * Creates a value object from the argument data transfer object.
745 *
746 * @param data The data transfer object to convert.
747 * @return The new value object.
748 */
749 public UserDTO createDTO(UserData data)
750 {
751 return new UserDTO(
752 data.getId(), data.getUserName(), data.getPassword(), data.getFirstName(),
753 data.getLastName(), data.getEmail(), data.getCreated(), data.getDeleted(),
754 data.getDescription(), data.getLastUpdated());
755 }
756
757 /**
758 * Creates a value object from the argument local user.
759 *
760 * @param data The local interface to convert.
761 * @return The new value object.
762 */
763 public UserDTO createDTO(UserLocal data)
764 {
765 return createDTO(data.getUserData());
766 }
767
768 /**
769 * Creates a value object from the argument data transfer object.
770 *
771 * @param data The data transfer object to convert.
772 * @return The new value object.
773 */
774 public ProjectDTO createDTO(ProjectData data)
775 {
776 return new ProjectDTO(
777 data.getId(), data.getName(), data.getDescription(),
778 data.getCreated(), data.getDeleted(), data.getLastUpdated());
779 }
780
781 /**
782 * Creates a value object from the argument local project.
783 *
784 * @param data The local interface to convert.
785 * @return The new value object.
786 */
787 public ProjectDTO createDTO(ProjectLocal data)
788 {
789 return createDTO(data.getProjectData());
790 }
791
792 }
793