View Javadoc
1   /*
2    * Copyright 2012-2021 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 AsyncManager asyncManager = getAsyncManager();
86          final MessageManager messageManager = getMessageManager();
87          return new SMailDogmaticPostalPersonnel() {
88  
89              // *if you need user locale switching or templates from database,
90              // override createConventionReceptionist() (see the method for the details)
91              @Override
92              protected SMailConventionReceptionist createConventionReceptionist() {
93                  return super.createConventionReceptionist().asReceiverLocale(postcard -> OptionalThing.empty());
94              }
95  
96              @Override
97              protected OptionalThing<SMailSubjectFilter> createSubjectFilter() {
98                  return OptionalThing.of((view, subject) -> subject);
99              }
100 
101             @Override
102             protected OptionalThing<SMailAsyncStrategy> createAsyncStrategy() {
103                 return OptionalThing.of((view, runnable) -> async(asyncManager, runnable));
104             }
105 
106             @Override
107             protected OptionalThing<SMailLabelStrategy> createLabelStrategy() {
108                 return OptionalThing.of((view, locale, label) -> resolveLabelIfNeeds(messageManager, locale, label));
109             }
110         };
111     }
112 
113     // ===================================================================================
114     //                                                                        Asynchronous
115     //                                                                        ============
116     protected void async(final AsyncManager asyncManager, final Runnable runnable) {
117         asyncManager.async(new ConcurrentAsyncCall() {
118             @Override
119             public void callback() {
120                 runnable.run();
121             }
122 
123             @Override
124             public boolean asPrimary() {
125                 return true; // mail is primary business
126             }
127         });
128     }
129 
130     // ===================================================================================
131     //                                                                       Resolve Label
132     //                                                                       =============
133     protected String resolveLabelIfNeeds(final MessageManager messageManager, final Locale locale, final String label) {
134         return label.startsWith("labels.") ? messageManager.getMessage(locale, label) : label;
135     }
136 
137     // ===================================================================================
138     //                                                                           Component
139     //                                                                           =========
140     protected MessageManager getMessageManager() {
141         return ContainerUtil.getComponent(MessageManager.class);
142     }
143 
144     protected AsyncManager getAsyncManager() {
145         return ContainerUtil.getComponent(AsyncManager.class);
146     }
147 }