New CopyMethod java event listener for Trimm

New CopyMethod java event listener for Trimm

Ever had to do copy/clone methods in large objects?

Doing it by hand takes a long time, it’s a nightmare to maintain and can lead to some serious bugs.
You could always use some kind of serialization like Java Object Serialization java.io.ObjectInputStream and java.io.ObjectOutputStream, to do it.

But there is and easier way, you can now use Trimm to automatically create a copy method in all your pojos, just use dk.tigerteam.trimm.mdsd.java.generator.extension.CopyMethodEventListener in your yaml configuration like:

# Extensions (note these listeners can also be groovy scripts containing a single class implement the GeneratorEventListener interface (or
# which extend specializations thereof) 
eventListeners:
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.CopyMethodEventListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.SerialVersionUIDGeneratorListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.PropertySugarMethodsEventListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.ToStringListener
    - listener: dk.tigerteam.trimm.mdsd.java.generator.extension.HashCodeAndEqualsListener


and Trimm will create a copy method like:

@Entity
@Table(name = "Consultation")
public class Consultation extends dk.tigerteam.mddexample.model.AbstractEntity {
    private static final long serialVersionUID = 1378823903L;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Basic
    @Column(name = "billed", nullable = false)
    private boolean billed;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Basic(optional = true)
    @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
    @Column(name = "checkedIn")
    private org.joda.time.DateTime checkedIn;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Basic(optional = true)
    @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
    @Column(name = "checkedOut")
    private org.joda.time.DateTime checkedOut;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Basic
    @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
    @Column(name = "consultationDate", nullable = false)
    private org.joda.time.DateTime consultationDate;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Lob
    @Basic
    @Column(name = "diagnosis")
    private String diagnosis;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Lob
    @Basic
    @Column(name = "examination")
    private String examination;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Lob
    @Basic
    @Column(name = "history")
    private String history;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Basic
    @Column(name = "paid", nullable = false)
    private boolean paid;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Lob
    @Basic
    @Column(name = "plan")
    private String plan;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     */
    @Lob
    @Basic
    @Column(name = "treatment")
    private String treatment;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     * Property type hierarchy:
     * <ul>
     *   <li>User</li>
     *   <ul>
     *     <li><b>Veterinarian</b></li>
     *     <li>Customer</li>
     *   </ul>
     * </ul>
     */
    @ManyToOne(cascade =  {
        CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}
    , fetch = FetchType.LAZY, optional = false)
    @JoinColumn(nullable = false, name = "veterinarianId")
    private Veterinarian veterinarian;

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     * Property type hierarchy:
     * <ul>
     *   <li><b>Prescription</b></li>
     * </ul>
     */
    @OneToMany(cascade =  {
        CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}
    , fetch = FetchType.LAZY, orphanRemoval = true)
    @JoinColumn(name = "consultation_prescriptionId")
    private Set<Prescription> prescriptionCollection = new HashSet<Prescription>();

    /**
     * No documentation<p/>
     * Defined in Consultation<p/>
     * Property type hierarchy:
     * <ul>
     *   <li><b>SomeEmbeddableClass</b></li>
     * </ul>
     */
    @ElementCollection(fetch = FetchType.EAGER)
    @OrderBy("someProperty")
    @JoinTable(name = "Consultation_somes", joinColumns = @JoinColumn(name = "ConsultationId")
    )
    @AttributeOverrides({@AttributeOverride(name = "element.someOtherProperty",column = @Column(name = "somesCollection_someOtherPrope")
        )
        , @AttributeOverride(name = "element.someProperty",column = @Column(name = "somesCollection_someProperty")
        )
    })
    private List<SomeEmbeddableClass> somesCollection = new ArrayList<SomeEmbeddableClass>();

    <missing ... >
	
    public String toString() {
        return "Consultation[" + "billed: " + billed + ", checkedIn: " +
        checkedIn + ", checkedOut: " + checkedOut + ", consultationDate: " +
        consultationDate + ", diagnosis: " + diagnosis + ", examination: " +
        examination + ", history: " + history + ", paid: " + paid + ", plan: " +
        plan + ", treatment: " + treatment + ", veterinarian: " + veterinarian +
        "]";
    }

    public Consultation getCopy() {
        Consultation consultation = new Consultation();
        consultation.setBilled(billed);
        consultation.setCheckedIn(checkedIn);
        consultation.setCheckedOut(checkedOut);
        consultation.setConsultationDate(consultationDate);
        consultation.setDiagnosis(diagnosis);
        consultation.setExamination(examination);
        consultation.setHistory(history);
        consultation.setPaid(paid);
        consultation.setPlan(plan);
        consultation.setTreatment(treatment);
        consultation.setVeterinarian(veterinarian.getCopy());

        for (Prescription element : prescriptionCollection) {
            consultation.getPrescriptionCollection().add(element.getCopy());
        }

        for (SomeEmbeddableClass element : somesCollection) {
            consultation.getSomesCollection().add(element.getCopy());
        }

        return consultation;
    }
}

The listener is available in the Maven snapshot of TRIMM 1.0.1 with TrimmJava available from our Maven Repository.

 
Comments

No comments yet.