001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.model;
016    
017    import com.liferay.portal.LocaleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.Image;
026    import com.liferay.portal.service.ImageLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
031    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
032    import com.liferay.portlet.expando.model.ExpandoBridge;
033    
034    import java.io.File;
035    import java.io.Serializable;
036    
037    import java.util.Date;
038    import java.util.HashMap;
039    import java.util.Locale;
040    import java.util.Map;
041    
042    /**
043     * @author Marcellus Tavares
044     */
045    public class JournalTemplateAdapter implements JournalTemplate {
046    
047            public JournalTemplateAdapter(DDMTemplate ddmTemplate) {
048                    _ddmTemplate = ddmTemplate;
049            }
050    
051            @Override
052            public Object clone() {
053                    JournalTemplateAdapter journalTemplateAdapter =
054                            new JournalTemplateAdapter();
055    
056                    journalTemplateAdapter.setUuid(getUuid());
057                    journalTemplateAdapter.setId(getId());
058                    journalTemplateAdapter.setGroupId(getGroupId());
059                    journalTemplateAdapter.setCompanyId(getCompanyId());
060                    journalTemplateAdapter.setUserId(getUserId());
061                    journalTemplateAdapter.setUserName(getUserName());
062                    journalTemplateAdapter.setCreateDate(getCreateDate());
063                    journalTemplateAdapter.setModifiedDate(getModifiedDate());
064                    journalTemplateAdapter.setTemplateId(getTemplateId());
065                    journalTemplateAdapter.setStructureId(getStructureId());
066                    journalTemplateAdapter.setName(getName());
067                    journalTemplateAdapter.setDescription(getDescription());
068                    journalTemplateAdapter.setXsl(getXsl());
069                    journalTemplateAdapter.setLangType(getLangType());
070                    journalTemplateAdapter.setCacheable(getCacheable());
071                    journalTemplateAdapter.setSmallImage(getSmallImage());
072                    journalTemplateAdapter.setSmallImageId(getSmallImageId());
073                    journalTemplateAdapter.setSmallImageURL(getSmallImageURL());
074    
075                    return journalTemplateAdapter;
076            }
077    
078            @Override
079            public int compareTo(JournalTemplate journalTemplate) {
080                    String templateId = getTemplateId();
081    
082                    int value = templateId.compareTo(journalTemplate.getTemplateId());
083    
084                    if (value != 0) {
085                            return value;
086                    }
087    
088                    return 0;
089            }
090    
091            @Override
092            public boolean equals(Object obj) {
093                    if (this == obj) {
094                            return true;
095                    }
096    
097                    if (!(obj instanceof JournalTemplate)) {
098                            return false;
099                    }
100    
101                    JournalTemplate journalTemplate = (JournalTemplate)obj;
102    
103                    if (Validator.equals(getId(), journalTemplate.getId())) {
104                            return true;
105                    }
106    
107                    return false;
108            }
109    
110            @Override
111            public boolean getCacheable() {
112                    return _ddmTemplate.getCacheable();
113            }
114    
115            @Override
116            public long getCompanyId() {
117                    return _ddmTemplate.getCompanyId();
118            }
119    
120            @Override
121            public Date getCreateDate() {
122                    return _ddmTemplate.getCreateDate();
123            }
124    
125            @Override
126            public String getDescription() {
127                    return _ddmTemplate.getDescription();
128            }
129    
130            @Override
131            public String getDescription(Locale locale) {
132                    return _ddmTemplate.getDescription(locale);
133            }
134    
135            @Override
136            public String getDescription(Locale locale, boolean useDefault) {
137                    return _ddmTemplate.getDescription(locale, useDefault);
138            }
139    
140            @Override
141            public String getDescription(String languageId) {
142                    return _ddmTemplate.getDescription(languageId);
143            }
144    
145            @Override
146            public String getDescription(String languageId, boolean useDefault) {
147                    return _ddmTemplate.getDescription(languageId, useDefault);
148            }
149    
150            @Override
151            public String getDescriptionCurrentLanguageId() {
152                    return _ddmTemplate.getDescriptionCurrentLanguageId();
153            }
154    
155            @Override
156            public String getDescriptionCurrentValue() {
157                    return _ddmTemplate.getDescriptionCurrentValue();
158            }
159    
160            @Override
161            public Map<Locale, String> getDescriptionMap() {
162                    return _ddmTemplate.getDescriptionMap();
163            }
164    
165            @Override
166            public ExpandoBridge getExpandoBridge() {
167                    return _ddmTemplate.getExpandoBridge();
168            }
169    
170            @Override
171            public long getGroupId() {
172                    return _ddmTemplate.getGroupId();
173            }
174    
175            @Override
176            public long getId() {
177                    return _ddmTemplate.getTemplateId();
178            }
179    
180            @Override
181            public String getLangType() {
182                    return _ddmTemplate.getLanguage();
183            }
184    
185            @Override
186            public Map<String, Object> getModelAttributes() {
187                    Map<String, Object> attributes = new HashMap<String, Object>();
188    
189                    attributes.put("uuid", getUuid());
190                    attributes.put("id", getId());
191                    attributes.put("groupId", getGroupId());
192                    attributes.put("companyId", getCompanyId());
193                    attributes.put("userId", getUserId());
194                    attributes.put("userName", getUserName());
195                    attributes.put("createDate", getCreateDate());
196                    attributes.put("modifiedDate", getModifiedDate());
197                    attributes.put("templateId", getTemplateId());
198                    attributes.put("structureId", getStructureId());
199                    attributes.put("name", getName());
200                    attributes.put("description", getDescription());
201                    attributes.put("xsl", getXsl());
202                    attributes.put("langType", getLangType());
203                    attributes.put("cacheable", getCacheable());
204                    attributes.put("smallImage", getSmallImage());
205                    attributes.put("smallImageId", getSmallImageId());
206                    attributes.put("smallImageURL", getSmallImageURL());
207    
208                    return attributes;
209            }
210    
211            @Override
212            public Class<?> getModelClass() {
213                    return JournalTemplate.class;
214            }
215    
216            @Override
217            public String getModelClassName() {
218                    return JournalTemplate.class.getName();
219            }
220    
221            @Override
222            public Date getModifiedDate() {
223                    return _ddmTemplate.getModifiedDate();
224            }
225    
226            @Override
227            public String getName() {
228                    return _ddmTemplate.getName();
229            }
230    
231            @Override
232            public String getName(Locale locale) {
233                    return _ddmTemplate.getName(locale);
234            }
235    
236            @Override
237            public String getName(Locale locale, boolean useDefault) {
238                    return _ddmTemplate.getName(locale, useDefault);
239            }
240    
241            @Override
242            public String getName(String languageId) {
243                    return _ddmTemplate.getName(languageId);
244            }
245    
246            @Override
247            public String getName(String languageId, boolean useDefault) {
248                    return _ddmTemplate.getName(languageId, useDefault);
249            }
250    
251            @Override
252            public String getNameCurrentLanguageId() {
253                    return _ddmTemplate.getNameCurrentLanguageId();
254            }
255    
256            @Override
257            public String getNameCurrentValue() {
258                    return _ddmTemplate.getNameCurrentValue();
259            }
260    
261            @Override
262            public Map<Locale, String> getNameMap() {
263                    return _ddmTemplate.getNameMap();
264            }
265    
266            @Override
267            public long getPrimaryKey() {
268                    return _ddmTemplate.getPrimaryKey();
269            }
270    
271            @Override
272            public Serializable getPrimaryKeyObj() {
273                    return _ddmTemplate.getPrimaryKeyObj();
274            }
275    
276            @Override
277            public boolean getSmallImage() {
278                    return _ddmTemplate.getSmallImage();
279            }
280    
281            @Override
282            public File getSmallImageFile() throws PortalException, SystemException {
283                    if (_smallImageFile == null) {
284                            Image smallImage = fetchSmallImage();
285    
286                            if (smallImage != null) {
287                                    _smallImageFile = getSmallImageFile(smallImage.getTextObj());
288                            }
289                    }
290    
291                    return _smallImageFile;
292            }
293    
294            @Override
295            public long getSmallImageId() {
296                    return _ddmTemplate.getSmallImageId();
297            }
298    
299            @Override
300            public String getSmallImageType() throws PortalException, SystemException {
301                    if (_smallImageType == null) {
302                            Image smallImage = fetchSmallImage();
303    
304                            if (smallImage != null) {
305                                    _smallImageType = smallImage.getType();
306                            }
307                    }
308    
309                    return _smallImageType;
310            }
311    
312            @Override
313            public String getSmallImageURL() {
314                    return _ddmTemplate.getSmallImageURL();
315            }
316    
317            @Override
318            public String getStructureId() {
319                    if (_ddmTemplate.getClassPK() == 0) {
320                            return null;
321                    }
322    
323                    try {
324                            DDMStructure ddmStructure =
325                                    DDMStructureLocalServiceUtil.getStructure(
326                                            _ddmTemplate.getClassPK());
327    
328                            return ddmStructure.getStructureKey();
329                    }
330                    catch (Exception e) {
331                            _log.error(e, e);
332                    }
333    
334                    return null;
335            }
336    
337            @Override
338            public String getTemplateId() {
339                    return _ddmTemplate.getTemplateKey();
340            }
341    
342            @Override
343            public long getUserId() {
344                    return _ddmTemplate.getUserId();
345            }
346    
347            @Override
348            public String getUserName() {
349                    return _ddmTemplate.getUserName();
350            }
351    
352            @Override
353            public String getUserUuid() throws SystemException {
354                    return _ddmTemplate.getUserUuid();
355            }
356    
357            @Override
358            public String getUuid() {
359                    return _ddmTemplate.getUuid();
360            }
361    
362            @Override
363            public String getXsl() {
364                    return _ddmTemplate.getScript();
365            }
366    
367            @Override
368            public int hashCode() {
369                    return (int)getPrimaryKey();
370            }
371    
372            @Override
373            public boolean isCacheable() {
374                    return _ddmTemplate.isCacheable();
375            }
376    
377            @Override
378            public boolean isNew() {
379                    return _ddmTemplate.isNew();
380            }
381    
382            @Override
383            public boolean isSmallImage() {
384                    return _ddmTemplate.isSmallImage();
385            }
386    
387            @Override
388            public void prepareLocalizedFieldsForImport(Locale defaultImportLocale)
389                    throws LocaleException {
390    
391                    _ddmTemplate.prepareLocalizedFieldsForImport(defaultImportLocale);
392            }
393    
394            @Override
395            public void setCacheable(boolean cacheable) {
396                    _ddmTemplate.setCacheable(cacheable);
397            }
398    
399            @Override
400            public void setCompanyId(long companyId) {
401                    _ddmTemplate.setCompanyId(companyId);
402            }
403    
404            @Override
405            public void setCreateDate(Date createDate) {
406                    _ddmTemplate.setCreateDate(createDate);
407            }
408    
409            @Override
410            public void setDescription(String description) {
411                    _ddmTemplate.setDescription(description);
412            }
413    
414            @Override
415            public void setDescription(String description, Locale locale) {
416                    _ddmTemplate.setDescription(description, locale);
417            }
418    
419            @Override
420            public void setDescription(
421                    String description, Locale locale, Locale defaultLocale) {
422    
423                    _ddmTemplate.setDescription(description, locale, defaultLocale);
424            }
425    
426            @Override
427            public void setDescriptionCurrentLanguageId(String languageId) {
428                    _ddmTemplate.setDescriptionCurrentLanguageId(languageId);
429            }
430    
431            @Override
432            public void setDescriptionMap(Map<Locale, String> descriptionMap) {
433                    _ddmTemplate.setDescriptionMap(descriptionMap);
434            }
435    
436            @Override
437            public void setDescriptionMap(
438                    Map<Locale, String> descriptionMap, Locale defaultLocale) {
439    
440                    _ddmTemplate.setDescriptionMap(descriptionMap, defaultLocale);
441            }
442    
443            @Override
444            public void setExpandoBridgeAttributes(ExpandoBridge expandoBridge) {
445                    _ddmTemplate.setExpandoBridgeAttributes(expandoBridge);
446            }
447    
448            @Override
449            public void setExpandoBridgeAttributes(JournalTemplate journalTemplate) {
450                    ExpandoBridge expandoBridge = getExpandoBridge();
451    
452                    ExpandoBridge journalTemplateExpandoBridge =
453                            journalTemplate.getExpandoBridge();
454    
455                    expandoBridge.setAttributes(
456                            journalTemplateExpandoBridge.getAttributes());
457            }
458    
459            @Override
460            public void setExpandoBridgeAttributes(ServiceContext serviceContext) {
461                    _ddmTemplate.setExpandoBridgeAttributes(serviceContext);
462            }
463    
464            @Override
465            public void setGroupId(long groupId) {
466                    _ddmTemplate.setGroupId(groupId);
467            }
468    
469            @Override
470            public void setId(long id) {
471                    _ddmTemplate.setTemplateId(id);
472            }
473    
474            @Override
475            public void setLangType(String langType) {
476                    _ddmTemplate.setLanguage(langType);
477            }
478    
479            @Override
480            public void setModelAttributes(Map<String, Object> attributes) {
481                    String uuid = (String)attributes.get("uuid");
482    
483                    if (uuid != null) {
484                            setUuid(uuid);
485                    }
486    
487                    Long id = (Long)attributes.get("id");
488    
489                    if (id != null) {
490                            setId(id);
491                    }
492    
493                    Long groupId = (Long)attributes.get("groupId");
494    
495                    if (groupId != null) {
496                            setGroupId(groupId);
497                    }
498    
499                    Long companyId = (Long)attributes.get("companyId");
500    
501                    if (companyId != null) {
502                            setCompanyId(companyId);
503                    }
504    
505                    Long userId = (Long)attributes.get("userId");
506    
507                    if (userId != null) {
508                            setUserId(userId);
509                    }
510    
511                    String userName = (String)attributes.get("userName");
512    
513                    if (userName != null) {
514                            setUserName(userName);
515                    }
516    
517                    Date createDate = (Date)attributes.get("createDate");
518    
519                    if (createDate != null) {
520                            setCreateDate(createDate);
521                    }
522    
523                    Date modifiedDate = (Date)attributes.get("modifiedDate");
524    
525                    if (modifiedDate != null) {
526                            setModifiedDate(modifiedDate);
527                    }
528    
529                    String templateId = (String)attributes.get("templateId");
530    
531                    if (templateId != null) {
532                            setTemplateId(templateId);
533                    }
534    
535                    String structureId = (String)attributes.get("structureId");
536    
537                    if (structureId != null) {
538                            setStructureId(structureId);
539                    }
540    
541                    String name = (String)attributes.get("name");
542    
543                    if (name != null) {
544                            setName(name);
545                    }
546    
547                    String description = (String)attributes.get("description");
548    
549                    if (description != null) {
550                            setDescription(description);
551                    }
552    
553                    String xsl = (String)attributes.get("xsl");
554    
555                    if (xsl != null) {
556                            setXsl(xsl);
557                    }
558    
559                    String langType = (String)attributes.get("langType");
560    
561                    if (langType != null) {
562                            setLangType(langType);
563                    }
564    
565                    Boolean cacheable = (Boolean)attributes.get("cacheable");
566    
567                    if (cacheable != null) {
568                            setCacheable(cacheable);
569                    }
570    
571                    Boolean smallImage = (Boolean)attributes.get("smallImage");
572    
573                    if (smallImage != null) {
574                            setSmallImage(smallImage);
575                    }
576    
577                    Long smallImageId = (Long)attributes.get("smallImageId");
578    
579                    if (smallImageId != null) {
580                            setSmallImageId(smallImageId);
581                    }
582    
583                    String smallImageURL = (String)attributes.get("smallImageURL");
584    
585                    if (smallImageURL != null) {
586                            setSmallImageURL(smallImageURL);
587                    }
588            }
589    
590            @Override
591            public void setModifiedDate(Date modifiedDate) {
592                    _ddmTemplate.setModifiedDate(modifiedDate);
593            }
594    
595            @Override
596            public void setName(String name) {
597                    _ddmTemplate.setName(name);
598            }
599    
600            @Override
601            public void setName(String name, Locale locale) {
602                    _ddmTemplate.setName(name, locale);
603            }
604    
605            @Override
606            public void setName(String name, Locale locale, Locale defaultLocale) {
607                    _ddmTemplate.setName(name, locale, defaultLocale);
608            }
609    
610            @Override
611            public void setNameCurrentLanguageId(String languageId) {
612                    _ddmTemplate.setNameCurrentLanguageId(languageId);
613            }
614    
615            @Override
616            public void setNameMap(Map<Locale, String> nameMap) {
617                    _ddmTemplate.setNameMap(nameMap);
618            }
619    
620            @Override
621            public void setNameMap(Map<Locale, String> nameMap, Locale defaultLocale) {
622                    _ddmTemplate.setNameMap(nameMap, defaultLocale);
623            }
624    
625            @Override
626            public void setNew(boolean n) {
627                    _ddmTemplate.setNew(n);
628            }
629    
630            @Override
631            public void setPrimaryKey(long primaryKey) {
632                    _ddmTemplate.setPrimaryKey(primaryKey);
633            }
634    
635            @Override
636            public void setPrimaryKeyObj(Serializable primaryKeyObj) {
637                    _ddmTemplate.setPrimaryKeyObj(primaryKeyObj);
638            }
639    
640            @Override
641            public void setSmallImage(boolean smallImage) {
642                    _ddmTemplate.setSmallImage(smallImage);
643            }
644    
645            @Override
646            public void setSmallImageId(long smallImageId) {
647                    _ddmTemplate.setSmallImageId(smallImageId);
648            }
649    
650            @Override
651            public void setSmallImageType(String smallImageType) {
652                    _smallImageType = smallImageType;
653            }
654    
655            @Override
656            public void setSmallImageURL(String smallImageURL) {
657                    _ddmTemplate.setSmallImageURL(smallImageURL);
658            }
659    
660            @Override
661            public void setStructureId(String structureId) {
662                    if (Validator.isNull(structureId)) {
663                            _ddmTemplate.setClassPK(0);
664    
665                            return;
666                    }
667    
668                    try {
669                            DDMStructure ddmStructure =
670                                    DDMStructureLocalServiceUtil.getStructure(
671                                            getGroupId(),
672                                            PortalUtil.getClassNameId(JournalArticle.class),
673                                            structureId, true);
674    
675                            _ddmTemplate.setClassPK(ddmStructure.getPrimaryKey());
676                    }
677                    catch (Exception e) {
678                            _log.error(e, e);
679                    }
680            }
681    
682            @Override
683            public void setTemplateId(String templateId) {
684                    _ddmTemplate.setTemplateKey(templateId);
685            }
686    
687            @Override
688            public void setUserId(long userId) {
689                    _ddmTemplate.setUserId(userId);
690            }
691    
692            @Override
693            public void setUserName(String userName) {
694                    _ddmTemplate.setUserName(userName);
695            }
696    
697            @Override
698            public void setUserUuid(String userUuid) {
699                    _ddmTemplate.setUserUuid(userUuid);
700            }
701    
702            @Override
703            public void setUuid(String uuid) {
704                    _ddmTemplate.setUuid(uuid);
705            }
706    
707            @Override
708            public void setXsl(String xsl) {
709                    _ddmTemplate.setScript(xsl);
710            }
711    
712            @Override
713            public String toString() {
714                    StringBundler sb = new StringBundler(37);
715    
716                    sb.append("{uuid=");
717                    sb.append(getUuid());
718                    sb.append(", id=");
719                    sb.append(getId());
720                    sb.append(", groupId=");
721                    sb.append(getGroupId());
722                    sb.append(", companyId=");
723                    sb.append(getCompanyId());
724                    sb.append(", userId=");
725                    sb.append(getUserId());
726                    sb.append(", userName=");
727                    sb.append(getUserName());
728                    sb.append(", createDate=");
729                    sb.append(getCreateDate());
730                    sb.append(", modifiedDate=");
731                    sb.append(getModifiedDate());
732                    sb.append(", templateId=");
733                    sb.append(getTemplateId());
734                    sb.append(", structureId=");
735                    sb.append(getStructureId());
736                    sb.append(", name=");
737                    sb.append(getName());
738                    sb.append(", description=");
739                    sb.append(getDescription());
740                    sb.append(", xsl=");
741                    sb.append(getXsl());
742                    sb.append(", langType=");
743                    sb.append(getLangType());
744                    sb.append(", cacheable=");
745                    sb.append(getCacheable());
746                    sb.append(", smallImage=");
747                    sb.append(getSmallImage());
748                    sb.append(", smallImageId=");
749                    sb.append(getSmallImageId());
750                    sb.append(", smallImageURL=");
751                    sb.append(getSmallImageURL());
752                    sb.append("}");
753    
754                    return sb.toString();
755            }
756    
757            protected Image fetchSmallImage() throws PortalException, SystemException {
758                    if (isSmallImage()) {
759                            return ImageLocalServiceUtil.getImage(getSmallImageId());
760                    }
761    
762                    return null;
763            }
764    
765            protected File getSmallImageFile(byte[] bytes) {
766                    try {
767                            return FileUtil.createTempFile(bytes);
768                    }
769                    catch (Exception e) {
770                            _log.error(e, e);
771                    }
772    
773                    return null;
774            }
775    
776            private JournalTemplateAdapter() {
777            }
778    
779            private static Log _log = LogFactoryUtil.getLog(
780                    JournalTemplateAdapter.class);
781    
782            private DDMTemplate _ddmTemplate;
783            private File _smallImageFile;
784            private String _smallImageType;
785    
786    }