Forums de discussion

PrincipalThreadLocal.getName() is null

Veronica Rodriguez, modifié il y a 7 années.

PrincipalThreadLocal.getName() is null

New Member Publications: 3 Date d'inscription: 31/03/17 Publications récentes
Hi!,

I have installed audit portly and I audit add, download, remove and update files in Documents and Media.

I am able to get value for PrincipalThreadLocal.getName(); in my controller class, where any ways i have request object, but getting null value in listener class only when audit download.

Any help regarding this?

Thanks
thumbnail
Amos Fong, modifié il y a 7 années.

RE: PrincipalThreadLocal.getName() is null

Liferay Legend Publications: 2047 Date d'inscription: 07/10/08 Publications récentes
What kind of listener class is it? If it's asynchronous (ie new thread), then the threadLocals won't be available.
Veronica Rodriguez, modifié il y a 7 années.

RE: PrincipalThreadLocal.getName() is null

New Member Publications: 3 Date d'inscription: 31/03/17 Publications récentes
I called DLFileEntryListener class in that extends to BaseModelListener<DLFileEntry>

But in the same class for audit add, delete or update files it works fine.
thumbnail
Amos Fong, modifié il y a 7 années.

RE: PrincipalThreadLocal.getName() is null

Liferay Legend Publications: 2047 Date d'inscription: 07/10/08 Publications récentes
It's not clear to me what's going on. Can you post some code snippets where it doesn't work?
Veronica Rodriguez, modifié il y a 7 années.

RE: PrincipalThreadLocal.getName() is null

New Member Publications: 3 Date d'inscription: 31/03/17 Publications récentes
Ok

I have this class
public class DLFileEntryListener extends BaseModelListener<dlfileentry></dlfileentry>


and implement this method

	public void onBeforeUpdate(DLFileEntry dlFileEntry) throws ModelListenerException{
		
		_log.info("Before Update File Entry Action ready to run");
		
		
		boolean trueUpdate=true;		
		
		String actionType=_UPDATE;
		String additionalInfo = "Document was edited successfully";
		long companyId = CompanyThreadLocal.getCompanyId();
		long userId = 0;
		String userName="dd";
		User updater = null;
		
		try{
			DLFileEntry oldDlFileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(dlFileEntry.getFileEntryId());			 
			//check if event corresponds to Download
			 if(oldDlFileEntry.getReadCount() &lt; dlFileEntry.getReadCount()){
				 _log.info("Download file");
				 	actionType=_DOWNLOAD;
					additionalInfo = "Document was downloaded successfully";
			 }
			}
			catch(PortalException e){
				e.printStackTrace();
			}
		catch(SystemException e){
			e.printStackTrace();
		}
		
		//get the user that invoked the action of update
		if (PrincipalThreadLocal.getName() != null) { 
			   userId = GetterUtil.getLong(PrincipalThreadLocal.getName()); 
			   userName = Long.toString(userId);
			   _log.info("User ID: "+userId);
			   try{
				   userName = UserLocalServiceUtil.getUser(userId).getFullName();
				  // updater = UserLocalServiceUtil.getUser(userId);
				  // userName = updater.getFullName();
				} catch(PortalException e){
					   e.printStackTrace();
					   userName = "Portail exception:"+userId;
					   _log.error("Failed to retrieve username with user id:"+userId);
				}catch(SystemException e){
				   e.printStackTrace();
				   userName = "SystemException:"+userId;

				   _log.error("Failed to retrieve username with user id:"+userId);
			   }
		}else{
			AuditRequestThreadLocal auditRequestThreadLocal = AuditRequestThreadLocal.getAuditThreadLocal();
			 long realUserId = auditRequestThreadLocal.getRealUserId();		 
			 userName = PortalUtil.getUserName( realUserId, "idUsuario" + Long.toString(realUserId));
			 
		}
		
						 
		 long docId = dlFileEntry.getFileEntryId();
		 		 	
		 String fileTitle = dlFileEntry.getTitle();
		 
		 String fileVersion = dlFileEntry.getVersion();
		 
		 _log.debug("Version: "+dlFileEntry.getVersion());
		 
		 TimeZone timeZone = TimeZone.getTimeZone("UTC");
		 now = Calendar.getInstance(timeZone);
		 
		 Date modified = now.getTime();
		 long modifiedTime = modified.getTime();
		 
		 System.out.println("modified Time "+modifiedTime);
		 Date create = dlFileEntry.getCreateDate();
		 
		 long createTime = create.getTime();		 
		 System.out.println("Create Time "+createTime);
		 
		 long difference = java.lang.Math.abs(modifiedTime - createTime);
		 System.out.println("Difference is "+difference);
		 if (difference &lt; MAXTime) {
			 trueUpdate=false;
			 _log.info("file entry "+dlFileEntry.getTitle()+" has just been created, no reason to log it as an update");
			 
			 //Check if document is on a workflow and if not, send an e-mail		 			 
			 try{
			 DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();	 
			 if(dlFileVersion !=null){
				 //status: 0 =&gt; no workflow exists
				 
				 if (dlFileVersion.getStatus() == 0) {					 					 
					 //sendMail(updater, dlFileEntry);				
				 }
			 }
				 		 
			 }
			 catch(PortalException e){
				 e.printStackTrace();
			 }
			 catch(SystemException e){
				 e.printStackTrace();
			 }
			 
		 }else{			 
			_log.info("Normal update");
		 }
		 
		 //Add AuditDoc POJO
		 if(trueUpdate){
		   try{
			   if(dlFileEntry.isInTrash()||dlFileEntry.isInTrashContainer()){
				   _log.debug("File Entry is in Trash");
				   additionalInfo = "Document was deleted succesfully";				 
				   actionType=_DELETE;
			    }
			    else{
				   _log.debug("File Entry is not in Trash");
			    }
			 
			       AuditDoc auditDoc = AuditDocLocalServiceUtil.addAuditDoc(userName, companyId, docId, create, modified, actionType, _DOC, additionalInfo, fileTitle, fileVersion);
			      _log.info("AuditDoc record was added with id:"+auditDoc.getId());
		      }
		     catch(PortalException e){
			   e.printStackTrace();
			  _log.error("Failed to add an AuditDoc record");
		     }
		     catch(SystemException e){
			  e.printStackTrace();
			 _log.error("Failed to add an AuditDoc record");
		    }
		 }  
				
	}


And the problem is here,
	//get the user that invoked the action of update
		if (PrincipalThreadLocal.getName() != null) { 
			   userId = GetterUtil.getLong(PrincipalThreadLocal.getName()); 
			   userName = Long.toString(userId);
			   _log.info("User ID: "+userId);
			   try{
				   userName = UserLocalServiceUtil.getUser(userId).getFullName();
				  // updater = UserLocalServiceUtil.getUser(userId);
				  // userName = updater.getFullName();
				} catch(PortalException e){
					   e.printStackTrace();
					   userName = "Portail exception:"+userId;
					   _log.error("Failed to retrieve username with user id:"+userId);
				}catch(SystemException e){
				   e.printStackTrace();
				   userName = "SystemException:"+userId;

				   _log.error("Failed to retrieve username with user id:"+userId);
			   }
		}else{
			System.out.println("PrincipalThreadLocal.getName  null");
			 
		}


This method is the same as when audit update file.

Sorry, but I new in Liferay :/
thumbnail
Amos Fong, modifié il y a 7 années.

RE: PrincipalThreadLocal.getName() is null

Liferay Legend Publications: 2047 Date d'inscription: 07/10/08 Publications récentes
It looks like it should work to me.

only when audit download

What does this mean? How is an update to DLFileEntry triggered?
Alex Voronin, modifié il y a 6 années.

RE: PrincipalThreadLocal.getName() is null

New Member Publications: 17 Date d'inscription: 07/02/13 Publications récentes
I ran into the same problem. In version 6.2, this code worked fine. Is there any solution?