1   /*
2    * Copyright 2003 - 2013 The eFaps Team
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.update.schema.common;
22  
23  import java.net.URL;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.UUID;
29  
30  import org.efaps.admin.common.MsgPhrase;
31  import org.efaps.admin.common.MsgPhrase.Argument;
32  import org.efaps.admin.common.MsgPhrase.Label;
33  import org.efaps.admin.user.Company;
34  import org.efaps.ci.CIAdmin;
35  import org.efaps.ci.CIAdminCommon;
36  import org.efaps.db.Delete;
37  import org.efaps.db.Insert;
38  import org.efaps.db.Instance;
39  import org.efaps.db.InstanceQuery;
40  import org.efaps.db.QueryBuilder;
41  import org.efaps.update.AbstractUpdate;
42  import org.efaps.update.UpdateLifecycle;
43  import org.efaps.update.util.InstallationException;
44  import org.efaps.util.EFapsException;
45  import org.efaps.util.cache.CacheReloadException;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * Handles the import / update of system configurations for eFaps read from a
51   * XML configuration item file.
52   *
53   * @author The eFaps Team
54   * @version $Id: MsgPhraseUpdate.java 14409 2014-11-11 03:50:45Z jan@moxter.net
55   *          $
56   */
57  public class MsgPhraseUpdate
58      extends AbstractUpdate
59  {
60  
61      /**
62       * Logging instance used to give logging information of this class.
63       */
64      private static final Logger LOG = LoggerFactory.getLogger(MsgPhraseUpdate.class);
65  
66      /**
67       * Default constructor to initialize this system configuration update
68       * instance for given <code>_url</code>.
69       *
70       * @param _url URL of the file
71       */
72      public MsgPhraseUpdate(final URL _url)
73      {
74          super(_url, "Admin_Common_MsgPhrase");
75      }
76  
77      /**
78       * Creates new instance of class {@link Definition}.
79       *
80       * @return new definition instance
81       * @see Definition
82       */
83      @Override
84      protected AbstractDefinition newDefinition()
85      {
86          return new Definition();
87      }
88  
89      /**
90       * Handles the definition of one version for an system configuration defined
91       * within XML configuration item file.
92       */
93      public class Definition
94          extends AbstractDefinition
95      {
96  
97          private String parent = null;
98  
99          private final Set<MsgPhrase.Label> labels = new HashSet<>();
100 
101         private final Set<MsgPhrase.Argument> arguments = new HashSet<>();
102 
103         /**
104          * @param _tags current path as list of single tags
105          * @param _attributes attributes for current path
106          * @param _text content for current path
107          * @throws EFapsException on error
108          */
109         @Override
110         protected void readXML(final List<String> _tags,
111                                final Map<String, String> _attributes,
112                                final String _text)
113             throws EFapsException
114         {
115             final String value = _tags.get(0);
116             switch (value) {
117                 case "parent":
118                     this.parent = _text;
119                     break;
120                 case "label":
121                     addLabel(_text, _attributes.get("language"), _attributes.get("company"));
122                     break;
123                 case "argument":
124                     addArgument(_text, _attributes.get("index"), _attributes.get("language"),
125                                     _attributes.get("company"));
126                     break;
127                 default:
128                     super.readXML(_tags, _attributes, _text);
129                     break;
130             }
131         }
132 
133         /**
134          * @param _text
135          * @param _string
136          * @param _string2
137          * @param _string3
138          */
139         private void addArgument(final String _value,
140                                  final String _index,
141                                  final String _language,
142                                  final String _company)
143             throws CacheReloadException
144         {
145             final MsgPhrase.Argument argument = new MsgPhrase.Argument();
146             argument.setValue(_value);
147             argument.setIndex(Integer.valueOf(_index));
148             if (_company != null) {
149                 final Company company = Company.get(UUID.fromString(_company));
150                 if (company != null) {
151                     argument.setCompanyId(company.getId());
152                 }
153             }
154             if (_language != null) {
155                 argument.setLanguageId(getLanguageId(_language));
156             }
157             this.arguments.add(argument);
158         }
159 
160         private Long getLanguageId(final String _language)
161         {
162             Long ret = null;
163             try {
164                 final QueryBuilder queryBldr = new QueryBuilder(CIAdmin.Language);
165                 queryBldr.addWhereAttrEqValue(CIAdmin.Language.Language, _language);
166                 final InstanceQuery query = queryBldr.getQuery();
167                 query.executeWithoutAccessCheck();
168                 if (query.next()) {
169                     ret = query.getCurrentValue().getId();
170                 }
171             } catch (final EFapsException e) {
172                 LOG.error("getLanguageId()", e);
173             }
174             return ret;
175         }
176 
177         /**
178          * @param _text
179          * @param _string
180          * @param _string2
181          */
182         private void addLabel(final String _value,
183                               final String _language,
184                               final String _company)
185             throws CacheReloadException
186         {
187             final MsgPhrase.Label label = new MsgPhrase.Label();
188             label.setValue(_value);
189             if (_company != null) {
190                 final Company company = Company.get(UUID.fromString(_company));
191                 if (company != null) {
192                     label.setCompanyId(company.getId());
193                 }
194             }
195             if (_language != null) {
196                 label.setLanguageId(getLanguageId(_language));
197             }
198             this.labels.add(label);
199         }
200 
201         /**
202          * If the current life cycle <code>step</code> is
203          * {@link UpdateLifecycle#EFAPS_UPDATE EFAPS_UPDATE}, the
204          * {@link #attributes} are updated.
205          *
206          * @param _step current life cycle update step
207          * @param _allLinkTypes all link types to update
208          * @throws InstallationException if update failed
209          * @see #attributes
210          */
211         @Override
212         public void updateInDB(final UpdateLifecycle _step,
213                                final Set<Link> _allLinkTypes)
214             throws InstallationException
215         {
216             try {
217                 if (_step == UpdateLifecycle.EFAPS_UPDATE) {
218                     // set the id of the parent (if defined)
219                     if (this.parent != null && this.parent.length() > 0) {
220                         final QueryBuilder queryBldr = new QueryBuilder(CIAdminCommon.MsgPhrase);
221                         queryBldr.addWhereAttrEqValue(CIAdminCommon.MsgPhrase.Name, this.parent);
222                         final InstanceQuery query = queryBldr.getQuery();
223                         query.executeWithoutAccessCheck();
224                         if (query.next()) {
225                             final Instance instance = query.getCurrentValue();
226                             addValue(CIAdminCommon.MsgPhrase.ParentLink.name, "" + instance.getId());
227                         } else {
228                             addValue(CIAdminCommon.MsgPhrase.ParentLink.name, null);
229                         }
230                     } else {
231                         addValue(CIAdminCommon.MsgPhrase.ParentLink.name, null);
232                     }
233                 }
234 
235                 super.updateInDB(_step, _allLinkTypes);
236 
237                 if (_step == UpdateLifecycle.EFAPS_UPDATE && getInstance() != null && getInstance().isValid()) {
238                     final QueryBuilder queryBldr = new QueryBuilder(CIAdminCommon.MsgPhraseConfigAbstract);
239                     queryBldr.addWhereAttrEqValue(CIAdminCommon.MsgPhraseConfigAbstract.AbstractLink, getInstance());
240                     final InstanceQuery query = queryBldr.getQuery();
241                     query.executeWithoutAccessCheck();
242                     while (query.next()) {
243                         new Delete(query.getCurrentValue()).executeWithoutAccessCheck();
244                     }
245 
246                     for (final Label label : this.labels) {
247                         final Insert insert = new Insert(CIAdminCommon.MsgPhraseLabel);
248                         insert.add(CIAdminCommon.MsgPhraseLabel.AbstractLink, getInstance());
249                         insert.add(CIAdminCommon.MsgPhraseLabel.Value, label.getValue());
250                         insert.add(CIAdminCommon.MsgPhraseLabel.CompanyLink, label.getCompanyId());
251                         insert.add(CIAdminCommon.MsgPhraseLabel.LanguageLink, label.getLanguageId());
252                         insert.executeWithoutAccessCheck();
253                     }
254 
255                     for (final Argument argument : this.arguments) {
256                         final Insert insert = new Insert(CIAdminCommon.MsgPhraseArgument);
257                         insert.add(CIAdminCommon.MsgPhraseArgument.AbstractLink, getInstance());
258                         insert.add(CIAdminCommon.MsgPhraseArgument.Value, argument.getValue());
259                         insert.add(CIAdminCommon.MsgPhraseArgument.Index, argument.getIndex());
260                         insert.add(CIAdminCommon.MsgPhraseArgument.CompanyLink, argument.getCompanyId());
261                         insert.add(CIAdminCommon.MsgPhraseArgument.LanguageLink, argument.getLanguageId());
262                         insert.executeWithoutAccessCheck();
263                     }
264                 }
265             } catch (final EFapsException e) {
266                 throw new InstallationException("update did not work", e);
267             }
268         }
269     }
270 }