⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mrequest.java

📁 大家共享愉快, 共享愉快, 共享愉快, 共享愉快,共享愉快
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		//
		m_emailTo = new StringBuffer();
		if (update != null || sendInfo.size() > 0)
		{
			sendNotices(sendInfo);
			
			//	Update
			setDateLastAction(getUpdated());
			setLastResult(getResult());
			setDueType();
			//	Reset
			setConfidentialTypeEntry (getConfidentialType());
			setStartDate(null);
			setEndTime(null);
			setR_StandardResponse_ID(0);
			setR_MailText_ID(0);
			setResult(null);
		//	setQtySpent(null);
		//	setQtyInvoiced(null);
		}
		return true;
	}	//	beforeSave

	/**
	 * 	Check for changes
	 *	@param ra request action
	 *	@param columnName column
	 *	@return true if changes
	 */
	private boolean checkChange (MRequestAction ra, String columnName)
	{
		if (is_ValueChanged(columnName))
		{
			Object value = get_ValueOld(columnName);
			if (value == null)
				ra.addNullColumn(columnName);
			else
				ra.set_ValueNoCheck(columnName, value);
			m_changed = true;
			return true;
		}
		return false;
	}	//	checkChange
	
	/**
	 *  Check the ability to send email.
	 *  @return AD_Message or null if no error
	 */
	private String checkEMail ()
	{
		//  Mail Host
		MClient client = MClient.get(getCtx());
		if (client == null 
			|| client.getSMTPHost() == null
			|| client.getSMTPHost().length() == 0)
			return "RequestActionEMailNoSMTP";

		//  Mail To
		MUser to = new MUser (getCtx(), getAD_User_ID(), get_TrxName());
		if (to == null
			|| to.getEMail() == null
			|| to.getEMail().length() == 0)
			return "RequestActionEMailNoTo";

		//  Mail From real user
		MUser from = MUser.get(getCtx(), Env.getAD_User_ID(getCtx()));
		if (from == null 
			|| from.getEMail() == null
			|| from.getEMail().length() == 0)
			return "RequestActionEMailNoFrom";
		
		//  Check that UI user is Request User
//		int realSalesRep_ID = Env.getContextAsInt (getCtx(), "#AD_User_ID");
//		if (realSalesRep_ID != getSalesRep_ID())
//			setSalesRep_ID(realSalesRep_ID);

		//  RequestActionEMailInfo - EMail from {0} to {1}
//		Object[] args = new Object[] {emailFrom, emailTo};
//		String msg = Msg.getMsg(getCtx(), "RequestActionEMailInfo", args);
//		setLastResult(msg);
		//
		
		return null;
	}   //  checkEMail

	/**
	 * 	Set SalesRep_ID
	 *	@param SalesRep_ID id
	 */
	public void setSalesRep_ID (int SalesRep_ID)
	{
		if (SalesRep_ID != 0)
			super.setSalesRep_ID (SalesRep_ID);
		else if (getSalesRep_ID() != 0)
			log.warning("Ignored - Tried to set SalesRep_ID to 0 from " + getSalesRep_ID());
	}	//	setSalesRep_ID
	
	/**
	 * 	After Save
	 *	@param newRecord new
	 *	@param success success
	 *	@return success
	 */
	protected boolean afterSave (boolean newRecord, boolean success)
	{
		if (success) {
			//
			String sql = "UPDATE R_Request"
						 + " SET SourceRecord_ID ="
						 + getR_Request_ID()
						 + " WHERE R_Request_ID=" + getR_Request_ID();
			int no = DB.executeUpdate(sql, get_TrxName());
		}

		if (!success)
			return success;
		
		//	Create Update
		if (newRecord && getResult() != null)
		{
			MRequestUpdate update = new MRequestUpdate(this);
			update.save();
		}
		//	Initial Mail
		if (newRecord)
			sendNotices(new ArrayList());

		//	ChangeRequest - created in Request Processor
		if (getM_ChangeRequest_ID() != 0
			&& is_ValueChanged("R_Group_ID"))	//	different ECN assignment?
		{
			int oldID = get_ValueOldAsInt("R_Group_ID");
			if (getR_Group_ID() == 0)
				setM_ChangeRequest_ID(0);	//	not effective as in afterSave
			else
			{
				MGroup oldG = MGroup.get(getCtx(), oldID);
				MGroup newG = MGroup.get(getCtx(), getR_Group_ID());
				if (oldG.getM_BOM_ID() != newG.getM_BOM_ID()
					|| oldG.getM_ChangeNotice_ID() != newG.getM_ChangeNotice_ID())
				{
					MChangeRequest ecr = new MChangeRequest(getCtx(), getM_ChangeRequest_ID(), get_TrxName());
					if (!ecr.isProcessed()
						|| ecr.getM_FixChangeNotice_ID() == 0)
					{
						ecr.setM_BOM_ID(newG.getM_BOM_ID());
						ecr.setM_ChangeNotice_ID(newG.getM_ChangeNotice_ID());
						ecr.save();
					}
				}
			}
		}
		
		if (m_emailTo.length() > 0)
			log.saveInfo ("RequestActionEMailOK", m_emailTo.toString());
		
		return success;
	}	//	afterSave

	/**
	 * 	Send transfer Message
	 */
	private void sendTransferMessage ()
	{
		//	Sender
		int AD_User_ID = Env.getContextAsInt(p_ctx, "#AD_User_ID");
		if (AD_User_ID == 0)
			AD_User_ID = getUpdatedBy();
		//	Old
		Object oo = get_ValueOld("SalesRep_ID");
		int oldSalesRep_ID = 0;
		if (oo instanceof Integer)
			oldSalesRep_ID = ((Integer)oo).intValue();

		//  RequestActionTransfer - Request {0} was transfered by {1} from {2} to {3}
		Object[] args = new Object[] {getDocumentNo(), 
			MUser.getNameOfUser(AD_User_ID), 
			MUser.getNameOfUser(oldSalesRep_ID),
			MUser.getNameOfUser(getSalesRep_ID())
			};
		String subject = Msg.getMsg(getCtx(), "RequestActionTransfer", args);
		String message = subject + "\n" + getSummary();
		MClient client = MClient.get(getCtx());
		MUser from = MUser.get (getCtx(), AD_User_ID);
		MUser to = MUser.get (getCtx(), getSalesRep_ID());
		//
		client.sendEMail(from, to, subject, message, createPDF());
	}	//	afterSaveTransfer

	
	/**
	 * 	Send Update EMail/Notices
	 * 	@param list list of changes
	 */
	public void sendNotices(ArrayList list)
	{
		//	Subject
		String subject = Msg.translate(getCtx(), "R_Request_ID") 
			+ " " + Msg.getMsg(getCtx(), "Updated") + ": " + getDocumentNo();
		//	Message
		StringBuffer message = new StringBuffer();
		//		UpdatedBy: Joe
		int UpdatedBy = Env.getAD_User_ID(getCtx());
		MUser from = MUser.get(getCtx(), UpdatedBy);
		if (from != null)
			message.append(Msg.translate(getCtx(), "UpdatedBy")).append(": ")
				.append(from.getName());
		//		LastAction/Created: ...	
		if (getDateLastAction() != null)
			message.append("\n").append(Msg.translate(getCtx(), "DateLastAction"))
				.append(": ").append(getDateLastAction());
		else
			message.append("\n").append(Msg.translate(getCtx(), "Created"))
				.append(": ").append(getCreated());
		//	Changes
		for (int i = 0; i < list.size(); i++)
		{
			String columnName = (String)list.get(i);
			message.append("\n").append(Msg.getElement(getCtx(), columnName))
				.append(": ").append(get_DisplayValue(columnName, false))
				.append(" -> ").append(get_DisplayValue(columnName, true));
		}
		//	NextAction
		if (getDateNextAction() != null)
			message.append("\n").append(Msg.translate(getCtx(), "DateNextAction"))
				.append(": ").append(getDateNextAction());
		message.append(SEPARATOR)
			.append(getSummary());
		if (getResult() != null)
			message.append("\n----------\n").append(getResult());
		message.append(getMailTrailer(null));
		File pdf = createPDF();
		log.finer(message.toString());
		
		//	Prepare sending Notice/Mail
		MClient client = MClient.get(getCtx());
		//	Reset from if external
		if (from.getEMailUser() == null || from.getEMailUserPW() == null)
			from = null;
		int success = 0;
		int failure = 0;
		int notices = 0;
		//
		ArrayList<Integer> userList = new ArrayList<Integer>();
		String sql = "SELECT u.AD_User_ID, u.NotificationType, u.EMail, u.Name, MAX(r.AD_Role_ID) "
			+ "FROM RV_RequestUpdates_Only ru"
			+ " INNER JOIN AD_User u ON (ru.AD_User_ID=u.AD_User_ID)"
			+ " LEFT OUTER JOIN AD_User_Roles r ON (u.AD_User_ID=r.AD_User_ID) "
			+ "WHERE ru.R_Request_ID=? "
			+ "GROUP BY u.AD_User_ID, u.NotificationType, u.EMail, u.Name";
		PreparedStatement pstmt = null;
		try
		{
			pstmt = DB.prepareStatement (sql, null);
			pstmt.setInt (1, getR_Request_ID());
			ResultSet rs = pstmt.executeQuery ();
			while (rs.next ())
			{
				int AD_User_ID = rs.getInt(1);
				String NotificationType = rs.getString(2);
				if (NotificationType == null)
					NotificationType = MUser.NOTIFICATIONTYPE_EMail;
				String email = rs.getString(3);
				String Name = rs.getString(4);
				//	Role
				int AD_Role_ID = rs.getInt(5);
				if (rs.wasNull())
					AD_Role_ID = -1;
				
				//	Don't send mail to oneself
		//		if (AD_User_ID == UpdatedBy)
		//			continue;
				
				//	No confidential to externals
				if (AD_Role_ID == -1 
					&& (getConfidentialTypeEntry().equals(CONFIDENTIALTYPE_Internal)
						|| getConfidentialTypeEntry().equals(CONFIDENTIALTYPE_PrivateInformation)))
					continue;
				
				if (MUser.NOTIFICATIONTYPE_None.equals(NotificationType))
				{
					log.config("Opt out: " + Name);
					continue;
				}
				if ((MUser.NOTIFICATIONTYPE_EMail.equals(NotificationType)
					|| MUser.NOTIFICATIONTYPE_EMailPlusNotice.equals(NotificationType))
					&& (email == null || email.length() == 0))
				{
					if (AD_Role_ID >= 0)
						NotificationType = MUser.NOTIFICATIONTYPE_Notice;
					else
					{
						log.config("No EMail: " + Name);
						continue;
					}
				}
				if (MUser.NOTIFICATIONTYPE_Notice.equals(NotificationType)
					&& AD_Role_ID >= 0)
				{
					log.config("No internal User: " + Name);
					continue;
				}

				//	Check duplicate receivers
				Integer ii = new Integer (AD_User_ID);
				if (userList.contains(ii))
					continue;
				userList.add(ii);
				//
				MUser to = MUser.get (getCtx(), AD_User_ID);
				//	Send Mail
				if (MUser.NOTIFICATIONTYPE_EMail.equals(NotificationType)
					|| MUser.NOTIFICATIONTYPE_EMailPlusNotice.equals(NotificationType))
				{
					if (client.sendEMail(from, to, subject, message.toString(), pdf)) 
					{
						success++;
						if (m_emailTo.length() > 0)
							m_emailTo.append(", ");
						m_emailTo.append(to.getEMail());
					}
					else
					{
						log.warning("Failed: " + Name);
						failure++;
						NotificationType = MUser.NOTIFICATIONTYPE_Notice;
					}
				}
				//	Send Note
				if (MUser.NOTIFICATIONTYPE_Notice.equals(NotificationType)
					|| MUser.NOTIFICATIONTYPE_EMailPlusNotice.equals(NotificationType))
				{
					int AD_Message_ID = 834;
					MNote note = new MNote(getCtx(), AD_Message_ID, AD_User_ID,
						MRequest.Table_ID, getR_Request_ID(), 
						subject, message.toString(), get_TrxName());
					if (note.save())
						notices++;
				}
			}
			rs.close ();
			pstmt.close ();
			pstmt = null;
		}
		catch (Exception e)
		{
			log.log (Level.SEVERE, sql, e);
		}
		try
		{
			if (pstmt != null)
				pstmt.close ();
			pstmt = null;
		}
		catch (Exception e)
		{
			pstmt = null;
		}
		
		log.info("EMail Success=" + success + ", Failure=" + failure
			+ " - Notices=" + notices);
	}	//	sendNotice
	
	/**************************************************************************
	 * 	Get MailID
	 * 	@param serverAddress server address
	 *	@return Mail Trailer
	 */
	public String getMailTrailer(String serverAddress)
	{
		StringBuffer sb = new StringBuffer("\n").append(SEPARATOR)
			.append(Msg.translate(getCtx(), "R_Request_ID"))
			.append(": ").append(getDocumentNo())
			.append("  ").append(getMailTag())
			.append("\nSent by CompiereMail");
		if (serverAddress != null)
			sb.append(" from ").append(serverAddress);
		return sb.toString();
	}	//	getMailTrailer

	/**
	 * 	Get Mail Tag
	 *	@return [Req@{id}@]
	 */
	public String getMailTag()
	{
		return TAG_START + get_ID() + TAG_END;
	}	//	getMailTag
	
	/**
	 * 	(Soft) Close request.
	 * 	Must be called after webUpdate
	 */
	public void doClose()
	{
		MStatus status = MStatus.get(getCtx(), getR_Status_ID());
		if (!status.isClosed())
		{
			MStatus[] closed = MStatus.getClosed(getCtx());
			MStatus newStatus = null;
			for (int i = 0; i < closed.length; i++)
			{
				if (!closed[i].isFinalClose())
				{
					newStatus = closed[i];
					break;
				}
			}
			if (newStatus == null && closed.length > 0)
				newStatus = closed[0];
			if (newStatus != null)
				setR_Status_ID(newStatus.getR_Status_ID());
		}
	}	//	doClose
	
	/**
	 * 	Escalate request
	 * 	@param user true if user escalated - otherwise system
	 */
	public void doEscalate(boolean user)
	{
		if (user)
		{
			String Importance = getPriorityUser();
			if (PRIORITYUSER_Urgent.equals(Importance))
				;	//	high as it goes
			else if (PRIORITYUSER_High.equals(Importance))
				setPriorityUser(PRIORITYUSER_Urgent);
			else if (PRIORITYUSER_Medium.equals(Importance))
				setPriorityUser(PRIORITYUSER_High);
			else if (PRIORITYUSER_Low.equals(Importance))
				setPriorityUser(PRIORITYUSER_Medium);
			else if (PRIORITYUSER_Minor.equals(Importance))
				setPriorityUser(PRIORITYUSER_Low);
		}
		else
		{
			String Importance = getPriority();
			if (PRIORITY_Urgent.equals(Importance))
				;	//	high as it goes
			else if (PRIORITY_High.equals(Importance))
				setPriority(PRIORITY_Urgent);
			else if (PRIORITY_Medium.equals(Importance))
				setPriority(PRIORITY_High);
			else if (PRIORITY_Low.equals(Importance))
				setPriority(PRIORITY_Medium);
			else if (PRIORITY_Minor.equals(Importance))
				setPriority(PRIORITY_Low);
		}
	}	//	doEscalate
	
}	//	MRequest

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -