How to get the multi selected entity in a table component and insert/add to another entity/database table?

Hi,
How to get multi selected entity values in a table component one by one and insert these values into another database table? I want to iterate all selected rows and get the value of it. After this, I would add these values into multi entities then insert into table. Could you share the full example code? I’m new to Java and Cuba. I have many questions. Thank you very much.

Below is my table.

        <table id="ccsWoesTable"
               multiselect="true"
               width="100%">
            <actions>
                <action id="create"/>
                <action id="edit"/>
                <action id="remove"/>
            </actions>
            <columns>
                <column id="wodate"/>
                <column id="dpment"/>
                <column id="shf"/>
                <column id="begintime"/>
                <column id="endtime"/>
                <column id="hrs"/>
                <column id="item"/>
                <column id="mach"/>
                <column id="stdp"/>
            </columns>
            <rows datasource="ccsWoesDs"/>
            <rowsCount/>
            <buttonsPanel id="buttonsPanel"
                          alwaysVisible="true">
                <button id="createBtn"
                        action="ccsWoesTable.create"
                        caption="msg://btn_create"/>
                <button id="editBtn"
                        action="ccsWoesTable.edit"
                        caption="msg://btn_edit"/>
                <button id="removeBtn"
                        action="ccsWoesTable.remove"
                        caption="msg://btn_delete"/>
            </buttonsPanel>
        </table>

For example, I selected two rows. I want to click btn_upload to insert into another entity/table from the values I select.
Upload

Hi,

Briefly, you need to 1) get selected rows (Table’s getSelected() method), 2) create a set of entities that duplicates the selected rows (metadata.create()), 3) save the set of new entites in DB (dataManager.commit()).

Now let’s see these steps in details.

For example, you want to save the selected Customer entity instances to another database table. To do this, you should create a new entity that duplicates Customer but is bound to another table in DB. By default, the entity inheritance strategy is SINGLE_TABLE, so you should change it to TABLE_PER_CLASS:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@NamePattern("%s|name")
@Table(name = "SALES_CUSTOMER")
@Entity(name = "sales$Customer")
public class Customer extends StandardEntity {
    private static final long serialVersionUID = -6018518150505269719L;

    @Column(name = "NAME", nullable = false, length = 100)
    protected String name;

    @Column(name = "EMAIL", length = 100)
    protected String email;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

}

The new CopiedCustomer entity extends Customer to avoid duplicating the code:

@Table(name = "SALES_COPIED_CUSTOMER")
@Entity(name = "sales$CopiedCustomer")
public class CopiedCustomer extends Customer {
    private static final long serialVersionUID = 6675598050120952818L;
}

In the customer-browse XML descriptor let’s create a button that invokes the copySelectedItemsToDb() method:

<button id="copySelectedBtn"
    caption="Copy selected to DB"
    invoke="copySelectedItemsToDb"/>

In the CustomerBrowse Java controller we create this method and implement the logic I described above:

public class CustomerBrowse extends AbstractLookup {
    
    @Inject
    private GroupTable<Customer> customersTable;
    @Inject
    private Metadata metadata;
    @Inject
    private DataManager dataManager;

    public void copySelectedItemsToDb() {
        if (customersTable.getSelected() != null) {
            Set<CopiedCustomer> customersToCopy = new HashSet<>();
            for (Customer customer : customersTable.getSelected()) {
                CopiedCustomer copiedCustomer = metadata.create(CopiedCustomer.class);
                copiedCustomer.setName(customer.getName());
                copiedCustomer.setEmail(customer.getEmail());
                customersToCopy.add(copiedCustomer);
            }
            CommitContext context = new CommitContext(customersToCopy);
            dataManager.commit(context);
            showNotification(customersToCopy.size() + " customers have been copied to DB");
        }
    }
}

I hope that helped.

Hi,
Thank you. I have a requirement that loop the table multi rows. I did it by getItems() and a “for” loop. Is it OK?

Collection<CcsWo> woList = ccsWoesDs.getItems();
        for (CcsWo wo : woList) {
            String JdeYN = wo.getJdeyn().toString();
            if (JdeYN.equals("N") == true) {
                CcLbmast woorder = wo.getWo();
                String woNum = woorder.getAajono();
                String curwoDate = sdfWodate.format(wo.getWodate());
                if (curwoDate.equals(woDateStr) && 
 wo.getShf().equals(convertShift(shiftField.getValue()))) {
                    CcsSummary CcsSummarydata = convertToSUMMARYModel(wo);
                    service.addCCSSUMMARY(CcsSummarydata);
                }
                showNotification(woNum + "upload!", NotificationType.TRAY);
            }
        }

It is OK if the ccsWoesDs is populated with the items you need.
To track the rows selected in the table, you need the table.getSelected() method anyway.

P.S. if (JdeYN.equals("N")) is enough, == true is redundant here.