Last active
August 4, 2022 11:27
Star
You must be signed in to star a gist
Discover full tutorial here : https://cedricleruth.com/how-to-duplicate-a-row-programmatically-in-oracle-adf/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Oracle ADF ActionEvent to duplicate a row | |
public void duplicateRowLine(ActionEvent e) { | |
ViewObject vo = this.getViewObjectFromIterator("YOUR_ITERATOR_NAME"); | |
Row currentRow = vo.getCurrentRow(); | |
Row duplicatedRow = vo.createRow(); | |
AttributeDef[] voKeyAttributes = vo.getKeyAttributeDefs(); //List of the primary keys that need to be unique | |
String[] currentRowAttributes = currentRow.getAttributeNames(); | |
for (String attributeName : currentRowAttributes) { | |
int attributeIndex = duplicatedRow.getAttributeIndexOf(attributeName); | |
//Check if the attribute is updatable | |
if (duplicatedRow.isAttributeUpdateable(attributeIndex)){ | |
//Check if the attribute is a Primary Key | |
if (!this.attributeIsPrimaryKey(voKeyAttributes, attributeName)) { | |
//If it's not, copy the attribute value to the duplicated row | |
duplicatedRow.setAttribute(attributeName, currentRow.getAttribute(attributeName)); | |
} | |
//Mandatory: Add an else here to set the primary key with a unique value in java | |
} | |
} | |
//Add the duplicated row to your View Object | |
vo.insertRow(duplicatedRow); | |
//Optionnal : Commit the VO to add row in database | |
//Optionnal : Refresh the table AdfFacesContext.getCurrentInstance().addPartialTarget("YOUR_TABLE_ID"); | |
} | |
private boolean attributeIsPrimaryKey(AttributeDef[] voKeyAttributes, String currentAttributeName) { | |
for (AttributeDef voKeyAttribute : voKeyAttributes) { | |
if (voKeyAttribute.isPrimaryKey() && currentAttributeName.equals(voKeyAttribute.getName())) { | |
//If one of the primaryKey attribute is the current attribute | |
return true; | |
} | |
} | |
return false; | |
} | |
/*** This function is usually a public available in your ADFUtils package and should be reusable | |
Get the full library here : https://github.com/CedricL46/Oracle-ADF-and-JSF-Utils-libraries ***/ | |
private static ViewObjectImpl getViewObjectFromIterator(String nomIterator) { | |
_Logger.log(Level.INFO, "" + nomIterator); | |
ViewObjectImpl returnVO = null; | |
DCBindingContainer dcb = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry(); | |
if (dcb != null) { | |
DCIteratorBinding iter = dcb.findIteratorBinding(nomIterator); | |
if (iter != null) { | |
returnVO = (ViewObjectImpl)iter.getViewObject(); | |
} | |
} | |
return returnVO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment