View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.mylasta.direction.sponsor;
17  
18  import java.util.List;
19  import java.util.Locale;
20  
21  import org.codelibs.fess.mylasta.direction.FessConfig;
22  import org.dbflute.mail.send.SMailDeliveryDepartment;
23  import org.dbflute.mail.send.SMailPostalMotorbike;
24  import org.dbflute.mail.send.SMailPostalParkingLot;
25  import org.dbflute.mail.send.SMailPostalPersonnel;
26  import org.dbflute.mail.send.embedded.personnel.SMailDogmaticPostalPersonnel;
27  import org.dbflute.mail.send.embedded.receptionist.SMailConventionReceptionist;
28  import org.dbflute.mail.send.supplement.async.SMailAsyncStrategy;
29  import org.dbflute.mail.send.supplement.filter.SMailSubjectFilter;
30  import org.dbflute.mail.send.supplement.label.SMailLabelStrategy;
31  import org.dbflute.optional.OptionalThing;
32  import org.dbflute.util.DfStringUtil;
33  import org.lastaflute.core.magic.async.AsyncManager;
34  import org.lastaflute.core.magic.async.ConcurrentAsyncCall;
35  import org.lastaflute.core.message.MessageManager;
36  import org.lastaflute.core.util.ContainerUtil;
37  
38  /**
39   * @author jflute
40   */
41  public class FessMailDeliveryDepartmentCreator {
42  
43      // ===================================================================================
44      //                                                                           Attribute
45      //                                                                           =========
46      protected final FessConfig fessConfig;
47  
48      // ===================================================================================
49      //                                                                         Constructor
50      //                                                                         ===========
51      public FessMailDeliveryDepartmentCreator(final FessConfig fessConfig) {
52          this.fessConfig = fessConfig;
53      }
54  
55      // ===================================================================================
56      //                                                                              Create
57      //                                                                              ======
58      public SMailDeliveryDepartment create() {
59          return new SMailDeliveryDepartment(createPostalParkingLot(), createPostalPersonnel());
60      }
61  
62      // -----------------------------------------------------
63      //                                    Postal Parking Lot
64      //                                    ------------------
65      protected SMailPostalParkingLot createPostalParkingLot() {
66          final SMailPostalParkingLot parkingLot = new SMailPostalParkingLot();
67          final SMailPostalMotorbike motorbike = new SMailPostalMotorbike();
68          final String hostAndPort = fessConfig.getMailSmtpServerMainHostAndPort();
69          final List<String> hostPortList = DfStringUtil.splitListTrimmed(hostAndPort, ":");
70          motorbike.registerConnectionInfo(hostPortList.get(0), Integer.parseInt(hostPortList.get(1)));
71          motorbike.registerReturnPath(fessConfig.getMailReturnPath());
72          parkingLot.registerMotorbikeAsMain(motorbike);
73          return parkingLot;
74      }
75  
76      // -----------------------------------------------------
77      //                                      Postal Personnel
78      //                                      ----------------
79      protected SMailPostalPersonnel createPostalPersonnel() {
80          final SMailDogmaticPostalPersonnel personnel = createDogmaticPostalPersonnel();
81          return fessConfig.isMailSendMock() ? personnel.asTraining() : personnel;
82      }
83  
84      protected SMailDogmaticPostalPersonnel createDogmaticPostalPersonnel() { // #ext_point e.g. locale, database
85          final String testPrefix = fessConfig.getMailSubjectTestPrefix();
86          final AsyncManager asyncManager = getAsyncManager();
87          final MessageManager messageManager = getMessageManager();
88          return new SMailDogmaticPostalPersonnel() {
89  
90              // *if you need user locale switching or templates from database,
91              // override createConventionReceptionist() (see the method for the details)
92              @Override
93              protected SMailConventionReceptionist createConventionReceptionist() {
94                  return super.createConventionReceptionist().asReceiverLocale(postcard -> {
95                      return OptionalThing.empty();
96                  });
97              }
98  
99              @Override
100             protected OptionalThing<SMailSubjectFilter> createSubjectFilter() {
101                 return OptionalThing.of((view, subject) -> testPrefix + subject);
102             }
103 
104             @Override
105             protected OptionalThing<SMailAsyncStrategy> createAsyncStrategy() {
106                 return OptionalThing.of((view, runnable) -> async(asyncManager, runnable));
107             }
108 
109             @Override
110             protected OptionalThing<SMailLabelStrategy> createLabelStrategy() {
111                 return OptionalThing.of((view, locale, label) -> resolveLabelIfNeeds(messageManager, locale, label));
112             }
113         };
114     }
115 
116     // ===================================================================================
117     //                                                                        Asynchronous
118     //                                                                        ============
119     protected void async(final AsyncManager asyncManager, final Runnable runnable) {
120         asyncManager.async(new ConcurrentAsyncCall() {
121             @Override
122             public void callback() {
123                 runnable.run();
124             }
125 
126             @Override
127             public boolean asPrimary() {
128                 return true; // mail is primary business
129             }
130         });
131     }
132 
133     // ===================================================================================
134     //                                                                       Resolve Label
135     //                                                                       =============
136     protected String resolveLabelIfNeeds(final MessageManager messageManager, final Locale locale, final String label) {
137         return label.startsWith("labels.") ? messageManager.getMessage(locale, label) : label;
138     }
139 
140     // ===================================================================================
141     //                                                                           Component
142     //                                                                           =========
143     protected MessageManager getMessageManager() {
144         return ContainerUtil.getComponent(MessageManager.class);
145     }
146 
147     protected AsyncManager getAsyncManager() {
148         return ContainerUtil.getComponent(AsyncManager.class);
149     }
150 }