IDEMPIERE-6314 Issues with attachment on System records (#2556)

* IDEMPIERE-6314 Issues with attachment on System records

* - change approach to make it read-only

* - support for UUID key tables
- IDEMPIERE-5567 fix for the PO.is_new method
This commit is contained in:
Carlos Ruiz 2024-11-26 12:55:40 +01:00
parent f4fb07b66f
commit 38f452e4b0
3 changed files with 78 additions and 25 deletions

View File

@ -38,6 +38,7 @@ import org.compiere.tools.FileUtil;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.compiere.util.Util;
/**
@ -55,9 +56,9 @@ import org.compiere.util.Util;
public class MAttachment extends X_AD_Attachment
{
/**
* generated serial id
*
*/
private static final long serialVersionUID = 5615231734722570658L;
private static final long serialVersionUID = 5422581050563711060L;
/**
* @param ctx
@ -209,7 +210,31 @@ public class MAttachment extends X_AD_Attachment
/** string replaces the attachment root in stored xml file
* to allow the changing of the attachment root. */
public final String ATTACHMENT_FOLDER_PLACEHOLDER = "%ATTACHMENT_FOLDER%";
/* Attachment files can be read, but not written/deleted */
private Boolean isReadOnly = null;
/**
* If the related record is on System and the user is operating on Tenant, the attachment is read-only
* @return
*/
public boolean isReadOnly() {
if (isReadOnly == null) {
isReadOnly = true;
MTable table = MTable.get(getAD_Table_ID());
if (table != null) {
PO po = null;
if (table.isUUIDKeyTable())
po = table.getPOByUU(getRecord_UU(), get_TrxName());
else
po = table.getPO(getRecord_ID(), get_TrxName());
if (po != null && ! po.is_new() && po.getAD_Client_ID() == Env.getAD_Client_ID(getCtx()))
isReadOnly = false;
}
}
return isReadOnly;
}
/**
* Initialize storage provider
* @param ctx
@ -427,6 +452,8 @@ public class MAttachment extends X_AD_Attachment
* @return true if deleted
*/
public boolean deleteEntry(int index) {
if (isReadOnly())
throw new AdempiereException(Msg.getMsg(getCtx(), "R/O"));
if (m_items == null)
loadLOBData();
if (index >= 0 && index < m_items.size()) {
@ -570,6 +597,8 @@ public class MAttachment extends X_AD_Attachment
@Override
protected boolean beforeSave (boolean newRecord)
{
if (isReadOnly())
throw new AdempiereException(Msg.getMsg(getCtx(), "R/O"));
if (Util.isEmpty(getTitle()))
setTitle(NONE);
if (getRecord_ID() > 0 && getAD_Table_ID() > 0 && Util.isEmpty(getRecord_UU())) {
@ -581,6 +610,13 @@ public class MAttachment extends X_AD_Attachment
return saveLOBData(); // save in BinaryData
} // beforeSave
@Override
protected boolean beforeDelete() {
if (isReadOnly())
throw new AdempiereException(Msg.getMsg(getCtx(), "R/O"));
return true;
}
/**
* Ask storage provider to remove attachment content
* @return true if saved

View File

@ -1948,7 +1948,9 @@ public abstract class PO
// Search for Primary Key
for (int i = 0; i < p_info.getColumnCount(); i++)
{
if (p_info.isKey(i))
if ( p_info.isKey(i)
|| ( !p_info.hasKeyColumn()
&& p_info.getColumn(i).ColumnName.equals(PO.getUUIDColumnName(p_info.getTableName()))))
{
String ColumnName = p_info.getColumnName(i);
m_KeyColumns = new String[] {ColumnName};
@ -2314,7 +2316,7 @@ public abstract class PO
//
for (int i = 0; i < m_IDs.length; i++)
{
if (m_IDs[i].equals(I_ZERO) || m_IDs[i] == Null.NULL)
if (m_IDs[i] == null || m_IDs[i].equals(I_ZERO) || m_IDs[i] == Null.NULL)
continue;
return false; // one value is non-zero
}

View File

@ -93,9 +93,9 @@ import org.zkoss.zul.impl.XulElement;
public class WAttachment extends Window implements EventListener<Event>
{
/**
* generated serial id
*
*/
private static final long serialVersionUID = -8534334828539841412L;
private static final long serialVersionUID = 1041937899860394478L;
private static final CLogger log = CLogger.getCLogger(WAttachment.class);
@ -252,14 +252,21 @@ public class WAttachment extends Window implements EventListener<Event>
{
}
String maxUploadSize = "";
int size = MSysConfig.getIntValue(MSysConfig.ZK_MAX_UPLOAD_SIZE, 0);
if (size > 0)
maxUploadSize = "" + size;
if (m_attachment.isReadOnly()) {
toolBar.removeChild(bLoad);
toolBar.removeChild(bDelete);
confirmPanel.removeChild(bDeleteAll);
text.setReadonly(true);
} else {
String maxUploadSize = "";
int size = MSysConfig.getIntValue(MSysConfig.ZK_MAX_UPLOAD_SIZE, 0);
if (size > 0)
maxUploadSize = "" + size;
Clients.evalJavaScript("idempiere.dropToAttachFiles('" + this.getUuid() + "','" + mainPanel.getUuid() + "','"
+ this.getDesktop().getId() + "','" + progress.getUuid() + "','" + sizeLabel.getUuid() + "','"
+ maxUploadSize + "');");
Clients.evalJavaScript("idempiere.dropToAttachFiles('" + this.getUuid() + "','" + mainPanel.getUuid() + "','"
+ this.getDesktop().getId() + "','" + progress.getUuid() + "','" + sizeLabel.getUuid() + "','"
+ maxUploadSize + "');");
}
} // WAttachment
@ -703,10 +710,7 @@ public class WAttachment extends Window implements EventListener<Event>
if (newText.length() > 0 || m_attachment.getEntryCount() > 0) {
if (m_change) {
m_attachment.setBinaryData(new byte[0]); // ATTENTION! HEAVY HACK HERE... Else it will not save :(
m_attachment.setTextMsg(text.getText());
m_attachment.saveEx();
m_change = false;
saveAttachment();
}
} else {
m_attachment.delete(true);
@ -730,17 +734,27 @@ public class WAttachment extends Window implements EventListener<Event>
autoPreview (cbContent.getSelectedIndex(), false);
} else if (e.getTarget() == bSave) {
// Open Attachment
saveAttachmentToFile();
exportAttachmentToFile();
} else if (e.getTarget() == bPreview) {
displayData(cbContent.getSelectedIndex(), true);
} else if (e.getTarget() == bSaveAllAsZip) {
saveAllAsZip();
exportAllAsZip();
} else if(e.getTarget()==bEmail){
sendMail();
}
} // onEvent
/**
* Save the attachment to database
*/
private void saveAttachment() {
m_attachment.setBinaryData(new byte[0]); // ATTENTION! HEAVY HACK HERE... Else it will not save :(
m_attachment.setTextMsg(text.getText());
m_attachment.saveEx();
m_change = false;
}
/**
* Handle onCancel event
*/
@ -876,21 +890,22 @@ public class WAttachment extends Window implements EventListener<Event>
if (result)
{
if (m_attachment.deleteEntry(index)) {
// must save the attachment immediately, on external storage providers the file doesn't exist at this point
saveAttachment();
cbContent.removeItemAt(index);
clearPreview();
autoPreview (cbContent.getSelectedIndex(), true);
}
m_change = true;
}
}
});
} // deleteAttachment
/**
* Save current Attachment entry to File
* Export current Attachment entry to File
*/
private void saveAttachmentToFile()
private void exportAttachmentToFile()
{
int index = cbContent.getSelectedIndex();
if (log.isLoggable(Level.INFO))
@ -931,9 +946,9 @@ public class WAttachment extends Window implements EventListener<Event>
}
/**
* Save all attachment items as zip file
* Export all attachment items as zip file
*/
private void saveAllAsZip() {
private void exportAllAsZip() {
File zipFile = m_attachment.saveAsZip();
if (zipFile != null) {