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

📄 notificationmanagerejb.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                user.setEmail(userObj.getEmail());
                user.setPasswordDigest(userObj.getPassword());
                                
                Integer userGroupNotifyMethod = 
                    SecurityHelper.loadUserGroupNotifyMethod(ls, user, new Long(group.getGroupID()));
                if( userGroupNotifyMethod == null ) {
                    // default notification
                    notify( user );
                } else {
                    // notification using group
                    notify( user, userGroupNotifyMethod.intValue() );
                }
            }
        } else {
            // Notify workgroup
            notify( group, tier, notifyMethod );
        }
    }

    /**
     * Notify via alert
     *
     * @param group
     *            Group object
     * @param tier
     *            group tier
     */
    public void notifyByAlert( WorkGroup group, Integer tier ) {

        long time = System.currentTimeMillis();

        if( getLogger().isInfoEnabled() ) {
            INFO( "Try to send alert notification to group '" + group
                   + "' with tier '" + tier + "'" );
        }
        
        List userList = SecurityHelper.loadUsers(ls, group, tier);
        int size = ( userList == null ) ? 0 : userList.size();
        InboxManagerLocal inboxLocal = getInboxManagerLocal();
        for( int i = 0; i < size; i++ ) {
            UserObject userObj = ( UserObject ) userList.get( i );
            InboxMessage mailMessage = buildAlertInboxMessage( AccessRightsManager.getSystemLogonSession().getUser().getUserID() , userObj.getPkey() );
            inboxLocal.sendAlertMessage(ls, mailMessage);
        }        
        if( getLogger().isInfoEnabled() ) {
            INFO( "Send notification to group via alert - ok. Time(ms)="
                   + ( System.currentTimeMillis() - time ) );
        }
    }

    /**
     * Notify via E-mail
     *
     * @param group
     *            Group object
     */
    public void notifyByMail( WorkGroup group ) {

        long time = System.currentTimeMillis();

        if( getLogger().isDebugEnabled() ) {
            DEBUG( "Try to send mail notification to group '" + group + "'" );
        }

        try {
            InboxManagerLocal inboxLocal = getInboxManagerLocal();
            MailAddress[] to = new MailAddress[] {new MailAddress( group )};
            InboxMessage mailMessage = buildInboxMessage( to );

            // Send!
            inboxLocal.sendEmailMessage( ls, mailMessage, null );

            if( getLogger().isDebugEnabled() ) {
                DEBUG( "Send notification to group via e-mail - ok. Time(ms)="
                       + ( System.currentTimeMillis() - time ) );
                DEBUG( "Send notification to address " + mailMessage.getTo()
                       + " from address " + mailMessage.getFrom() );
            }

        } catch( MessagingException mex ) {
            WARN( "Send notification to group via e-mail - fail. Problem:\n"
                  + mex.getMessage() + "\nTime(ms)="
                  + ( System.currentTimeMillis() - time ) );

        } catch( Throwable t ) {
            ErrorHelper.throwSystemException( t, this );
        }
    }

    // ----------------------------------------------------- private methods

    //
    // User notification
    //
    private void notify( User user, int notifyMethod ) {

        // Checks email. If NULL - send alerts
        if( notifyMethod == User.NOTIFY_METHOD_EMAIL && user.getEmail() != null ) {
            notifyByMail( user );
        } else {
            notifyByAlert( user );
        }
    }

    //
    // Workgroup notification
    //
    private void notify( WorkGroup group, Integer tier, int notifyMethod ) {

        // Checks email. If NULL - send alerts
        if( notifyMethod == WorkGroup.NOTIFY_METHOD_EMAIL
            && group.getEmail() != null ) {
            notifyByMail( group );
        } else {
            notifyByAlert( group, tier );
        }
    }

    //
    // Get translated subject
    //
    private String getTranslatedSubject() {
        doTranslatePatterns();
        return translatedSubject;
    }

    //
    // Get translated message
    //
    private String getTranslatedMessage() {
        doTranslatePatterns();
        return translatedMessage;
    }
    
    //
    // Do translate patterns
    //
    private synchronized void doTranslatePatterns() {
        if( !isTranslated ) {
            FormsManager formsManager = new FormsManager( entity.getName(), key );
            try {
                translatedSubject = formsManager.translateBody( subject, false );
                translatedMessage = formsManager.translateBody( message, false );
            } catch( FormTransformException ex ) {
                ERROR( "Transformation error: " + ex.getMessage() );
            }
            isTranslated = true;
        }
    }

    //
    // Constrcuts InboxMessage.
    //
    private InboxMessage buildInboxMessage( MailAddress[] to ) {

        InboxMessage mailMessage = new InboxMessage(to, new MailAddress(InboxHelper.getDefSender()), getTranslatedSubject(), getTranslatedMessage());
        mailMessage.setFrom( new MailAddress( InboxHelper.getDefSender() ) );

        // Add linked object.
        Long callID = new Long( ( String ) key.getKey( 0 ) );
        if( callID == null ) {
            throw new NullPointerException( "Wrong compound key. Got NULL Call ID." );
        }
        Integer callType = null;
        if( key.size() == 2 ) {
            callType = new Integer( ( String ) key.getKey( 1 ) );
        } else if( key.size() != 1 ) {
            throw new IllegalStateException( "Wrong compound key. Only Call ID and Call Type supported!" );
        }
        mailMessage.setObjectInfo(callID, callType);

        addAttachments(mailMessage);
        
        return mailMessage;
        
    }
    
    //
    // Adds attachemnts to the email.
    //
    private void addAttachments(InboxMessage mailMessage) {
        String body = mailMessage.getBody();
        
        if (!StringHelper.isHTML(body) ) {
            return;
        }
        RE imgTag;
		RE src;
		try {
			imgTag = new RE("<img [^>].{5,}<--notAttachment>", RE.MATCH_CASEINDEPENDENT);
			src = new RE("src=\".+\"", RE.MATCH_CASEINDEPENDENT);
		} catch (RESyntaxException e) {
			throw new GenericSystemException(e);
		}

        int curPos = 0;
        while (imgTag.match(body, curPos)) {
            int startPos = imgTag.getParenStart(0);
            int endPos = imgTag.getParenEnd(0);
            String tag = body.substring(startPos, endPos);
            if (src.match(tag)) {
                int sPos = src.getParenStart(0);
                int ePos = src.getParenEnd(0);
                String srcText = tag.substring(sPos, ePos);
                sPos = srcText.indexOf('"');
                ePos = srcText.lastIndexOf('"');
                String filePath = srcText.substring(sPos + 1, ePos);
                createAttachment(filePath, mailMessage);
            }
            curPos = endPos;
        }
    }
    
    private void createAttachment(String filePath, InboxMessage mailMessage) {
        String fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
        byte[] fileData = getFileData(null, fileName);
        Attachment attachment = new Attachment(fileName, fileData);
        mailMessage.addAttachments(attachment);
    }
    
    private byte[] getFileData(String id, String filepath) throws FileManagerException {
        FileUploadManager manager = new FileUploadManager(ls);
        byte[] data = null;
        try {
            FileManager.FileInfo fi;
            if (!StringHelper.isEmpty(id)) {
                fi = manager.loadFile(Long.parseLong(id));
            } else {
                fi = FileManager.loadFile(filepath);
            }
            if (fi.getData() != null) {
                data = fi.getData();
            }
        } catch (FileManagerException ex) {
        }
        return data;
    }

    private InboxMessage buildAlertInboxMessage(Long senderId, Long recipientId ) {

        // Add linked object.
        Long callID = new Long( ( String ) key.getKey( 0 ) );
        if( callID == null ) {
            throw new NullPointerException( "Wrong compound key. Got NULL Call ID." );
        }
        Integer callType = null;
        if( key.size() == 2 ) {
            callType = new Integer( ( String ) key.getKey( 1 ) );
        } else if( key.size() != 1 ) {
            throw new IllegalStateException( "Wrong compound key. Only Call ID and Call Type supported!" );
        }
        
        return new InboxMessage( senderId, recipientId, null, getTranslatedSubject(), getTranslatedMessage(), callType, callID);

    }
    
    //
    // Get InboxManagerLocal local interface
    //
    private InboxManagerLocal getInboxManagerLocal() {
        return( InboxManagerLocal ) getLocalObject( CustomJNDINames.InboxManager,
            InboxManagerLocalHome.class );
    }
}

⌨️ 快捷键说明

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